Reputation: 1149
I'm looking to find a way in Boto3 to get the latest Ubuntu image from Canonical. Regular describe_images() doesn't have a parameter for the Canonical.
TIA
Upvotes: 0
Views: 663
Reputation: 17285
The AWS Systems Manager (SSM) can be used to query the latest Ubuntu AMI ID from Canonical.
import boto3
ssm_client = boto3.client('ssm')
name = "/aws/service/canonical/ubuntu/server/18.04/stable/current/amd64/hvm/ebs-gp2/ami-id"
image_id = ssm_client.get_parameter(Name=name)['Parameter']['Value']
The path follows this format:
ubuntu/$PRODUCT/$RELEASE/stable/current/$ARCH/$VIRT_TYPE/$VOL_TYPE/ami-id
server
or server-minimal
focal
, 20.04
, bionic
, 18.04
, xenial
, or 16.04
amd64
or arm64
pv
or hvm
ebs-gp2
, ebs-io1
, ebs-standard
, or instance-store
From Canonical's Cloud images - Amazon EC2
Upvotes: 0
Reputation: 1149
#Canonical id = 099720109477
images = ec2.describe_images(
Filters=[
{
'Name': 'architecture',
'Values': ['x86_64',]
},
{ 'Name': 'root-device-type',
'Values': ['ebs']
},
{
'Name': 'name',
'Values': ['ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server*']
}
],
Owners=['099720109477']
)
sortedAmis = sorted(images['Images'],
key=lambda x: x['CreationDate'],
reverse=True)
lastestAMI = sortedAmis[0]['ImageId']
#returns: ami-01c132a30955dafbb
Upvotes: 3