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:

  • Parameter Store Prefix (tree): /aws/service/ami-amazon-linux-latest/
  • AMI name alias: (example) amzn-ami-hvm-x86_64-gp2

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
Answer from John Rotenstein on Stack Overflow
Discussions

amazon web services - Get latest AMI ID for AWS instance - Stack Overflow
Filters are used to narrow down results and to exclude unwanted results. Fortunately, our organization had named the AMIs in a way easy to differentiate with wither amzn2 or CentOS in the AMI name. We wanted the Amazon Linux 2 AMI More on stackoverflow.com
🌐 stackoverflow.com
amazon ec2 - Is there a list of AMIs for every popular linux distribution and version - Stack Overflow
I need to test a piece of software against every linux distribution/version. I'm going to use amazon AWS Is there a list of AMIs somewhere that I can just copy paste into my script to automate this More on stackoverflow.com
🌐 stackoverflow.com
NEW Amazon Linux AMI - IMDSv2 as default
In New Amazon Linux AMI AMI ID ami-02f3f602d23f1659d (al2023-ami-2023.0.20230315.0-kernel-6.1-x86_64), which they launched on 15th March,2023 the Instance Metadata Service comes with version 2 by default where HttpTokens is mandatory. Direct curl http://169.254.169.254/latest/meta-data/instance-id ... More on repost.aws
🌐 repost.aws
1
0
March 27, 2023
What is the main reason Amazon Machine Image (AMI)'s i.e. AMI IDs in AWS are different on region to region basis? - Stack Overflow
When you copy an AMI from one region ... AMI ID. ... Each AWS Region is independent. When an AMI is copied to another region, it is a "different AMI". For example, deleting an AMI in one region does not impact a copy of that AMI in another region. This equally applies to the AMIs that AWS supplies. I see that you are wanting an Amazon Linux 2 ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 79216425 › how-to-find-the-right-ami-id-for-amazon-linux-2
How to find the right ami id for amazon linux 2 - Stack Overflow
What is the correct way to find the AMI ID for the region in cloudformation? amazon-web-services · amazon-ec2 · Share · Improve this question · Follow · edited Nov 23, 2024 at 9:13 · John Rotenstein · 273k2828 gold badges457457 silver badges543543 bronze badges · asked Nov 22, 2024 at 19:54 · David · 3,17377 gold badges4343 silver badges9797 bronze badges 1 · This might help: Query for the latest Amazon Linux AMI IDs using AWS Systems Manager Parameter Store | AWS Compute Blog ·
🌐
Amazon Web Services
aws.amazon.com › amazon linux ami
AWS | Amazon Linux AMI
2 weeks ago - The Amazon Linux AMI is a supported and maintained Linux image provided by Amazon Web Services for use on Amazon Elastic Compute Cloud (Amazon EC2). It is designed to provide a stable, secure, and high performance execution environment for applications running on Amazon EC2.
Top answer
1 of 7
26

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:

  • Parameter Store Prefix (tree): /aws/service/ami-amazon-linux-latest/
  • AMI name alias: (example) amzn-ami-hvm-x86_64-gp2

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
2 of 7
12

AWS CLI

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:

  • owners : For images by amazon, use 'amazon'. To query your own images, use 'self'
  • filters : You can use a list of filters to filter out the instance that you are looking for. I prefer description because I found the name filter missing for some images. Values support wildcards. More on filters
  • query : Query can be used to filter only what is required from the output. You can also sort on fields that would be present in the output. I have sorted on the images and the creation date to get the last created image and filtered the ImageId
  • output : Output can be json or text based on the way you plan to consume it.

Using Python

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'
  ]
)
🌐
GitHub
gist.github.com › revmischa › 89a7b9f273b407cedafb0bacd08940a2
Amazon Linux 2 AMI ID mapping for cloudformation · GitHub
Amazon Linux 2 AMI ID mapping for cloudformation. GitHub Gist: instantly share code, notes, and snippets.
🌐
AWS
aws.amazon.com › blogs › compute › query-for-the-latest-amazon-linux-ami-ids-using-aws-systems-manager-parameter-store
Query for the latest Amazon Linux AMI IDs using AWS Systems Manager Parameter Store | Amazon Web Services
September 14, 2018 - This post is courtesy of Arend Castelein, Software Development Engineer – AWS Want a simpler way to query for the latest Amazon Linux AMI? AWS Systems Manager Parameter Store already allows for querying the latest Windows AMI. Now, support has been expanded to include the latest Amazon Linux AMI.
🌐
AWS
docs.aws.amazon.com › amazon linux › user guide › al2 on amazon ec2
AL2 on Amazon EC2 - Amazon Linux 2
aws ec2 run-instances --image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-default-hvm-x86_64-gp2 --instance-type m5.xlarge --key-name MyKeyPair · For more information, see Using public parameters in the AWS Systems Manager User Guide. Amazon Linux 2 AMI names use the ...
Find elsewhere
🌐
Medium
moosakhalid.medium.com › fetching-amazon-linux-ami-id-using-ssm-and-terraform-or-aws-cloudformation-9963c9555a24
Fetching Amazon Linux AMI-ID using SSM and Terraform or AWS CloudFormation | by Moosa Khalid | Medium
January 24, 2021 - Beyond this SSM parameter path above you’ll need to pass in the actual identifier for the AMI in question, but if you choose to query up to this path it should return you a list of available AMI ID’s , from which you can pick and choose however you can provide a more specific path to get an exact image and the best part is that the string remains the same regardless if the AMI ID it is referencing changes in the back end. Here’s an example command for getting the exact value of an Amazon Linux 2 AMI in us-west-2 region.
🌐
AWS
docs.aws.amazon.com › amazon linux › user guide › using al2023 on aws › al2023 on amazon ec2
AL2023 on Amazon EC2 - Amazon Linux 2023
1 month ago - Start using AL2023 by setting up a new instance in the AWS CLI using the CloudFormation AMI ID parameter.
🌐
AWS
docs.aws.amazon.com › amazon ecs › developer guide › amazon ecs clusters › amazon ecs capacity providers for ec2 workloads › amazon ec2 container instances for amazon ecs › amazon ecs-optimized linux amis › retrieving amazon ecs-optimized linux ami metadata
Retrieving Amazon ECS-optimized Linux AMI metadata - Amazon Elastic Container Service
The following is the format of the parameter name for each Amazon ECS-optimized AMI variant. ... The following parameter name format retrieves the image ID of the latest recommended Amazon ECS-optimized Amazon Linux 2 AMI by using the sub-parameter image_id.
🌐
GitHub
github.com › hashicorp › terraform-aws-vault › blob › master › _docs › amazon-linux-2-ami-list.md
terraform-aws-vault/_docs/amazon-linux-2-ami-list.md at master · hashicorp/terraform-aws-vault
AMI ID · eu-north-1 · ami-0bc89781b6ac5abaa · ap-south-1 · ami-04568bde0d686e039 · eu-west-3 · ami-029b6e21d09465678 · eu-west-2 · ami-07464f35be338dac0 · eu-west-1 · ami-0b770cc0b1bb4b47b · ap-northeast-2 · ami-093d04d11e05d572c · ap-northeast-1 ·
Author   hashicorp
🌐
AWS re:Post
repost.aws › questions › QUkI6n7F9gRzG_eBjZ-00xew › new-amazon-linux-ami-imdsv2-as-default
NEW Amazon Linux AMI - IMDSv2 as default | AWS re:Post
March 27, 2023 - In New Amazon Linux AMI AMI ID ami-02f3f602d23f1659d (al2023-ami-2023.0.20230315.0-kernel-6.1-x86_64), which they launched on 15th March,2023 the Instance Metadata Service comes with version 2 by default where HttpTokens is mandatory.
🌐
Fundamentals-of-devops
fundamentals-of-devops.com › resources › 2025 › 02 › 24 › finding-ami-ids
How to find AMI IDs for Amazon Linux and Ubuntu
February 24, 2025 - To use this functionality, you set the AMI ID to resolve:ssm:<AMI_NAME>, where <AMI_NAME> is /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 for Amazon Linux and /aws/service/canonical/ubuntu/server/24.04/stable/curren...
🌐
LinuxVox
linuxvox.com › blog › amazon-linux-2-ami
Amazon Linux 2 AMI: A Comprehensive Guide — linuxvox.com
In the "Choose an Amazon Machine Image (AMI)" step, select "Amazon Linux 2 AMI". Choose an instance type based on your requirements (e.g., t2.micro for a small test instance). Configure other settings such as storage, security groups, and key pairs.