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 OverflowI want to use the Amazon Linux 2 as my AMI ID, but I can't hard code it else the cloudformation will be region bound. But I can't find the information for where the AMI ids for each region is?
amazon web services - Get latest AMI ID for AWS instance - Stack Overflow
amazon ec2 - Is there a list of AMIs for every popular linux distribution and version - Stack Overflow
NEW Amazon Linux AMI - IMDSv2 as default
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
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
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'
]
)
Amazon provides a list on their website. https://aws.amazon.com/amis
Actually I was sure that it was also possible to grab and filter the list via the API but unfortunately I can't find any documentation about this (imaginary?) feature
You can get AMI ID from aws site
Below is how to
To find a Linux AMI using the Images page
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
From the navigation bar, select a region. You can select any region that's available to you, regardless of your location. This is the region in which you'll launch your instance.
In the navigation pane, choose AMIs.
(Optional) Use the Filter options to scope the list of displayed AMIs to see only the AMIs that interest you. For example, to list all Linux AMIs provided by AWS, select Public images. Choose the Search bar and select Owner from the menu, then select Amazon images. Choose the Search bar again to select Platform and then the operating system from the list provided.
(Optional) Choose the Show/Hide Columns icon to select which image attributes to display, such as the root device type. Alternatively, you can select an AMI from the list and view its properties in the Details tab.
The AMI ID for the platform and Distribution you have chosen would be in the AMI ID tab
More detail here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html
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 AMI. Rather than using a mapping (which can become outdated), a better way is to use AWS Systems Manager Parameter Store to automatically obtain the "latest" AMI. This works automatically for the region being used.
Here is an example from Query for the latest Amazon Linux AMI IDs using AWS Systems Manager Parameter Store | AWS Compute Blog that shows how to do this in an AWS 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
See also: Public parameters - AWS Systems Manager
I believe the simplest explanation is, than when people talk about AWS it implies that it is just one service.
In reality there are many AWS'es and it is one per region (originally they were completely disconnected, now it is a bit more complex). The global services, (for example IAM) typically live in us-east-1 (but that's abstracted from us).
This is why AMI you have different AMI IDs in different region. It is one image per AWS.