AWS CLI Windows Command to Terminate All EC2 Instances - amazon-web-services

I need a single Windows CMD command that terminate all instances from Ohio region. I found this commands but its not working.
aws ec2 terminate-instances \
--region us-east-2 \
--instance-ids (aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId]" --region us-east-2)

Try this out in powershell:
foreach ($id in (aws ec2 describe-instances --filters --query "Reservations[].Instances[].[Instance
Id]" --output text --region us-east-2)) { aws ec2 terminate-instances --instance-ids $id }
You can pass the --dry-run flag with terminate instances to confirm first if you'd like.

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.

Using AWS CLI to stop all instances in a region

How can I use the AWS CLI to stop every EC2 instance in a region (1 VPC)?
I did see an old thread saying that this should work:
aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=pending,running" --query "Reservations[].Instances[].[InstanceId]" --output text | tr '\n' ' ')
However, I get the following error when using the above command:
Unknown options: --filters, Name=instance-state-name,Values=pending,running

aws cli describe-instances without a VpcId

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