mumbles
mumbles

Reputation: 1149

How can someone do a describe_images to find the latest Ubuntu image?

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

Answers (2)

Jon-Eric
Jon-Eric

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
  • PRODUCT: server or server-minimal
  • RELEASE: focal, 20.04, bionic, 18.04, xenial, or 16.04
  • ARCH: amd64 or arm64
  • VIRT_TYPE: pv or hvm
  • VOL_TYPE: ebs-gp2, ebs-io1, ebs-standard, or instance-store

From Canonical's Cloud images - Amazon EC2

Upvotes: 0

mumbles
mumbles

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

Related Questions