How to get recent Amazon Linux image in all regions? - amazon-web-services

#!/bin/bash
aws ec2 describe-images \
--owners self amazon \
--filters "Name=root-device-type,Values=ebs" \
--query 'Images[*].[ImageId,CreationDate]' \
| sort -k2 -r \
| head -n1
I have written a script to get the latest Amazon Linux Image using AWS CLI. When I run this script, I get the latest Amazon Linux Image in my default region eu-west-1. How can I modify the code to get the latest image in all the regions.

add --region <region_name> to your CLI command.
Something like this
aws ec2 describe-images --region eu-west-2 \
--owners self amazon \
--filters "Name=root-device-type,Values=ebs" \
--query 'Images[*].[ImageId,CreationDate]' \
| sort -k2 -r \
| head -n1
Instead of hardcoding region names, you can use aws ec2 describe-regions command and get the list of regions and run your query for each region.
https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-regions.html
Execute aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text which gives output as
ap-south-1
eu-west-3
eu-west-2
eu-west-1
ap-northeast-3
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2
Now loop thru each region and execute describe-images CLI command.

Related

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

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

AWS CLI Windows Command to Terminate All EC2 Instances

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.

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 command to list stopped instances

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"