Instead of building a list of mapped and hard-coded AMI IDs into your template, you can query the SSM parameter store for getting the latest version in a very consistent way.
For Regular (free) Ubuntu LTS
From the AWS Documentation, “Each Amazon Linux AMI now has its own Parameter Store namespace that is public and describable. Upon querying, an AMI namespace returns only its regional ImageID value.”
If you want to use the regular Ubuntu server version in your CloudFormation template, add the following to your Parameters section. You can change the Ubuntu version (e.g. jammy, focal, bionic) and also the architecture (ARM64 or AMD64):
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/canonical/ubuntu/server/jammy/stable/current/amd64/hvm/ebs-gp2/ami-id'
When launching the instance, in the Resources section, you can specify the AMI ID as it follows:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmiId
For Ubuntu Pro
For Ubuntu Pro, it is slightly different, because since it is a marketplace product the querying pattern changes: you need to query marketplace with a specific product ID:
Name |
Architecture |
identifier |
Ubuntu Pro FIPS 16.04 LTS |
amd64 |
prod-hykkbajyverq4 |
Ubuntu Pro FIPS 18.04 LTS |
amd64 |
prod-7izp2xqnddwdc |
Ubuntu Pro FIPS 20.04 LTS |
amd64 |
prod-k6fgbnayirmrc |
Ubuntu Pro 14.04 LTS |
amd64 |
prod-7u42cjnp5pcuo |
Ubuntu Pro 16.04 LTS |
amd64 |
prod-f6ogoaqs7lwre |
Ubuntu Pro 18.04 LTS |
amd64 |
prod-jlgu4232gpnwa |
Ubuntu Pro 20.04 LTS |
amd64 |
prod-3sk4unyn4iwqu |
Ubuntu Pro 22.04 LTS |
amd64 |
prod-lfutkwiaknxsk |
So the parameter in your CloudFormation template will look like this (change <product-id>
with one from the table above):
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/marketplace/<product-id>/latest'
Note:
Remember that if you are launching a marketplace product, you will need to subscribe to it, even if the product is completely free of charge.
For EKS Ubuntu LTS
For Ubuntu-EKS AMI IDs, the search is as follows (you can replace Ubuntu version, EKS version and architecture):
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/canonical/ubuntu/eks/20.04/1.23/stable/current/amd64/hvm/ebs-gp2/ami-id'