aws cli describe-instances without a VpcId - amazon-web-services

I want to run aws ec2 describe-instances looking for any instances without a VpcId property (those in ec2-classic)
How can I return ec2-classic instances using either the --query flag or JMESPath expression to get results without a VpcId?

This cli command will list all the instances which doesn't have VpcId.
aws ec2 describe-instances --region us-east-1 --query 'Reservations[*].Instances[?!not_null(VpcId)] | [].[InstanceId]' --output text
You can tweak the same to list all instances which has VpcId.
aws ec2 describe-instances --region us-east-1 --query 'Reservations[*].Instances[?not_null(VpcId)] | [].[InstanceId]' --output text

One approach is to query all instances and look for entries that do not have a SubnetId. The following CLI lists the EC2 classic instances. You can change the --query option to get the attributes you want.
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value | [0], State.Name, SubnetId]' --output text | grep -v subnet
Output
i-123456789abcdef01 MyClassicRunning running None
i-123456789abcdef23 MyClassicStopped stopped None

Related

How to Start AWS instance if it has Tag exists

I would like to run AWS CLI command to start the instance if it has relevant Tags exist for Eg "MigratedBy". If Tag doesnt exists, it shouldnt start the instance at all
I tried running below command but it didnt work out
aws ec2 start-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:MigratedBy,Values=my-super-tag" --query 'Reservations[].Instances[].InstanceId' --outpu t text`
It seems this command would run all the instance with the tag "MigratedBy" but this is wrong in my case. I would like to just start the particular instance if tag exists otherwise not
If you want to filter based only on the existence of the tag you can include it in the query and then pipe to show only the InstanceId:
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key=='MigratedBy'],InstanceId] | [*][1]' --output text
Finally, to start those instances the whole command will be:
aws ec2 start-instances --instance-ids `aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key=='MigratedBy'],InstanceId] | [*][1]' --output text`
Reference:
Filtering AWS CLI output

Get AllocationID in aws cli for powershell

How to filter out AllocationId for Elastic IP address in AWS cli 2.0 in Powershell.
aws ec2 describe-addresses --query 'Addresses[?AssociationId!=`null`].{AllocId:AllocationId} | [0]' --output text --region ap-southeast-2 --filters "Name=tag:xxx,Values=xxx"
tried this, returns Null. Need help especially with TAG filter.
I am trying to automate the process of EIP allocation to my newly generated Instance from autoscaling group.
Try this as it working for me, I just reorder the command query after filter.
aws ec2 describe-addresses --region ap-southeast-2 --filters "Name=tag:Name,Values=xxx" --query 'Addresses[?AssociationId!=`null`] | [0]' --output table

How to display ec2 instances details from multiple regions using single command, in my case from us-east-1 and us-west-1?

I am trying to get instance-id, availability zone, instance status, instance name(from tags) of ec2 instances hosted in us-east-1 and us-west-1 region in a single AWS CLI command so that I can export the output in an excel file.
I can get the output from one region at a time using the below commands but couldn't find a way to get the output from two regions using one single command.
aws ec2 describe-instances --region us-east-1 --query Reservations[].Instances[].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value} --output text>C:\Users\PiyushVermaVerma\Desktop\testfile.xls
and:
aws ec2 describe-instances --region us-west-1 --query Reservations[].Instances[].[InstanceId,Tags[0].Value,Placement.AvailabilityZone,State.Name] --output text>C:\Users\PiyushVermaVerma\Desktop\testfile.xls
In Bash, it is not possible to specify more than one region but you can always do this:
for region in us-east-1 us-west-1 ; do
aws ec2 describe-instances --query \
'Reservations[*].Instances[*].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value}' \
--output text --region $region
done > C:\Users\PiyushVermaVerma\Desktop\testfile.xls
For Windows Batch, you are probably best off just running the two commands in sequence, and using the append >> operator:
aws ec2 describe-instances --region us-east-1 --query Reservations[].Instances[].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value} --output text > C:\Users\PiyushVermaVerma\Desktop\testfile.xls
aws ec2 describe-instances --region us-east-1 --query Reservations[].Instances[].{ID:InstanceId,State:State.Name,AZ:Placement.AvailabilityZone,TagName:Tags[0].Value} --output text >> C:\Users\PiyushVermaVerma\Desktop\testfile.xls
You can only list instances in one region at a time.
Each region is a collection of zones. You are connecting to each region to list instances in the zones within that region. These zones are data centers and most are fairly large.
You will need to iterate thru each region.

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

How to get a list of all public IPs and their instance names on Amazon EC2

Anyone know how to get a list of all public IPs and their instance names on Amazon EC2 using aws CLI?
This got me the list of public IPs, but not their associated instance names.
aws ec2 describe-instances --query "Reservations[].Instances[].PublicIpAddress" --output text
Thanks in advance.
http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html
Update: The CLI now supports filters:
aws ec2 describe-instances --query "Reservations[].Instances[].[PublicIpAddress,InstanceId,Tags[?Key=='Name'].Value]"
Got it working using this:
aws ec2 describe-instances --output table --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, PublicIpAddress]'
Adding this for folks that will commonly find this post when searching for how to get your instance info.
In powershell you can use the following:
(Get-EC2Instance -ProfileName Profile).Instances | select InstanceId,PrivateIPAddress,PublicIpAddress #{Name="Servername";
Expression={$_.tags | where key -eq "Name" | select Value -expand Value}} | Format-Table.
With the Python AWS CLI you can use:
aws ec2 describe-instances --region=us-east-1 --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],PrivateIpAddress,PublicIpAddress]' --output text --profile ProfileName