I wanted to fetch the latest ami id for AWS Linux machine while creating an ec2 instance for an autoscaling architecture.
I was trying the aws
cli to get the images types, but it would print out a lot of information if I used the describe-images
command.
My requirement was to get only the image id, so that I could use it to create a launch configuration or launch an instance with the latest ami-id.
AWS provides updated and fully-patched Windows AMIs within five business days of Microsoft's patch Tuesday (the second Tuesday of each month). The AWS Windows AMIs contain the latest security updates available at the time they were created.
An Amazon Machine Image (AMI) is a supported and maintained image provided by AWS that provides the information required to launch an instance. You must specify an AMI when you launch an instance. You can launch multiple instances from a single AMI when you require multiple instances with the same configuration.
A little-known recent feature is the ability to Query for the latest Amazon Linux AMI IDs using AWS Systems Manager Parameter Store | AWS Compute Blog.
The namespace is made up of two parts:
/aws/service/ami-amazon-linux-latest/
These:
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn*" --query 'sort_by(Images, &CreationDate)[].Name'
Get-EC2ImageByName -Name amzn* | Sort-Object CreationDate | Select-Object Name
can be changed into:
aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --region us-east-1
Get-SSMParameter -Name /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 -region us-east-1
Plus, it can be used in a CloudFormation template:
# Use public Systems Manager Parameter
Parameters :
LatestAmiId :
Type : 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: ‘/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2’
Resources :
Instance :
Type : 'AWS::EC2::Instance'
Properties :
ImageId : !Ref LatestAmiId
A way to filter the output and get the only the required attributes is using a combination of filters,queries on the aws describe-images
command as below:
aws ec2 describe-images \
--owners 'amazon' \
--filters 'Name=description,Values=Amazon Linux AMI*' \
--query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \
--output 'text'
Command Explanation:
You can do the same using the below python script:
import boto3
from operator import itemgetter
client = boto3.client('ec2')
response = client.describe_images(
Filters=[
{
'Name': 'description',
'Values': [
'Amazon Linux AMI*',
]
},
],
Owners=[
'amazon'
]
)
# Sort on Creation date Desc
image_details = sorted(response['Images'],key=itemgetter('CreationDate'),reverse=True)
ami_id = image_details[0]['ImageId']
Update:
You can use fine-grain filters to get a quicker response. The filters mentioned in @Jack's answer work.
filters = [ {
'Name': 'name',
'Values': ['amzn-ami-hvm-*']
},{
'Name': 'description',
'Values': ['Amazon Linux AMI*']
},{
'Name': 'architecture',
'Values': ['x86_64']
},{
'Name': 'owner-alias',
'Values': ['amazon']
},{
'Name': 'owner-id',
'Values': ['137112412989']
},{
'Name': 'state',
'Values': ['available']
},{
'Name': 'root-device-type',
'Values': ['ebs']
},{
'Name': 'virtualization-type',
'Values': ['hvm']
},{
'Name': 'hypervisor',
'Values': ['xen']
},{
'Name': 'image-type',
'Values': ['machine']
} ]
# Use above filters
response = client.describe_images(
Filters=filters,
Owners=[
'amazon'
]
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With