AWS CLI command to list stopped instances - amazon-web-services

ec2-describe-instances --filter "instance-state-name=stopped"
This helps me list all stopped instances with all its details.
How should I modify the command that it only gives me the names of the stopped instances?

You are using old style commands. Use AWS CLI to get what you want.
aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped" --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value[]'

you can use aws cli combined with other tools like jq
aws ec2 describe-instances \
--filter Name=instance-state-name,Values=stopped \
--query 'Reservations[].Instances[].{ID: InstanceId,Hostname: PublicDnsName,Name: Tags[?Key==`Name`].Value }' \
| jq '.[] | .Name[]'
this will produce output in form:
"instance2"
"instance1"

Related

How to fetch second last AMI ID using AWS CLI

Using below AWS CLI command, I am able to fetch recently created AMI ID by sorting CreationDate.
aws ec2 describe-images --owners 1234567890 --filters 'Name=name,Values=*AMI*' 'Name=state,Values=available' --output json | jq -r '.Images | sort_by(.CreationDate) | last(.[]).ImageId'
I also wanted to get the second last AMI ID using AWS CLI. Could someone help me out on this?
You can use indices, like in python ([-2]):
aws ec2 describe-images --owners 1234567890 --filters 'Name=name,Values=*AMI*' 'Name=state,Values=available' --output json | jq -r '.Images | sort_by(.CreationDate)[-2].ImageId'

AWS EC2 CLI : Passing Environment Variables in the filter criteria

I am trying to pass an environment variable in the filter criteria while running CLI for AWS EC2.
For example I want to pass environment variables for vpc-id and cidr-block in this code snippet
$ aws ec2 describe-subnets --filter 'Name=vpc-id,Values=vpc-0ce822f7ef200f28k',"Name=cidr-block,Values=10.0.0.0/24" --query "Subnets[].SubnetId" --output=text --region=us-west-1
subnet-0ec2d15eda8f20484
and I am trying this:
aws ec2 describe-subnets --filter 'Name=vpc-id,Values=$EC2_VPC_ID','Name=cidr-block,Values=$EC2_CIDR_BLOCK' --query "Subnets[].SubnetId" --output=text --region=us-west-1
But it doesn't work. It tried multiple combinations such as -
aws ec2 describe-subnets --filter "Name=vpc-id,Values='$EC2_VPC_ID'","Name=cidr-block,Values='$EC2_CIDR_BLOCK'" --query "Subnets[].SubnetId" --output=text --region=us-west-1
aws ec2 describe-subnets --filter "Name=vpc-id,Values=$EC2_VPC_ID","Name=cidr-block,Values=$EC2_CIDR_BLOCK" --query "Subnets[].SubnetId" --output=text --region=us-west-1
aws ec2 describe-subnets --filter 'Name=vpc-id,Values='$EC2_VPC_ID'','Name=cidr-block,Values='$EC2_CIDR_BLOCK'' --query "Subnets[].SubnetId" --output=text --region=us-west-1
None of them work!!!
BTW, this was working before and it stopped working recently.
aws ec2 describe-subnets --filter 'Name=vpc-id,Values='$EC2_VPC_ID'','Name=cidr-block,Values='$EC2_CIDR_BLOCK'' --query "Subnets[].SubnetId" --output=text --region=us-west-1
This works (on Amazon Linux):
aws ec2 describe-subnets --filter "Name=vpc-id,Values=$EC2_VPC_ID,Name=cidr-block,Values=$EC2_CIDR_BLOCK" --query "Subnets[].SubnetId" --output text
You cannot pass variables in single quotes in BASH or shell script. This is the basic 1-0-1 of Shell Scripting. It needs to have double quotes around variables.
For example:
a=2
echo $a
echo "$a"
echo '$a'
The output will be:
2
2
$a

How do I find all Linux EC2 instances

I can use the following to list all Amazon Windows EC2 instances. How do I list Linux instances?
aws ec2 describe-images --owners self amazon --filters "Name=root-device-type,Values=ebs" "Name=platform,Values!=windows"
I think you can use the below to list all instances with platform type, then filter by platform type.
for region in `aws ec2 describe-regions --output text | cut -f2|awk -F. '{print $2}'`; do echo -e "\nInstances in: '$region':"; aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text --region $region; done;
Platform type "None" indicates the Linux.

AWS CLI - JMESPath query to find vpc-id by tag

I want to list the VPC id's which have a particular tag (Name=MyVPC).
I am aware that I can use --filter and run:
aws ec2 describe-vpcs --filters Name=tag:Name,Values=MyVPC --query 'Vpcs[].VpcId'
This works completely fine.
Is there a way I can achieve this without using --filter and only use JMESPath?
aws ec2 describe-vpcs --query 'Vpcs[?Tags[?Key==`Name`]|[?Value==`MyVPC`]].VpcId' --output text
Try this command:
aws ec2 describe-vpcs --query 'Vpcs[?contains(Tags[?Key==`Name`].Value[], `MyVPC`) == `true`].[VpcId]' --output text

Possible: AWS CLI Describe Not Equal To

I am currently using the AWS CLI to select instances and I have the following query:
aws ec2 describe-instances --filter "Name=instance.group-name,Values=my-cluster" "Name=instance-state-name,Values=running,pending,stopped" 'Name=tag:Name, Values=someInstance*'
This works and selects all the instances that start with someInstance.
However, I want to do the opposite, select all other instances that do NOT match this. I have tried using a regex but this doesn't work:
aws ec2 describe-instances --filter "Name=instance.group-name,Values=my-cluster" "Name=instance-state-name,Values=running,pending,stopped" 'Name=tag:Creater, Values=^(?!someInstance).*$'
Is this possible?
This can be accomplished by using JQ -
aws ec2 describe-instances --filter "Name=instance.group-name,Values=my-group" "Name=instance-state-name,Values=running,pending,stopped" | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Creator"}^C{Value: "myExclusion"}]}) | not)'