Search code examples
amazon-web-servicesamazon-ec2boto3redhatamazon-ami

How to list down all the AMI image IDs for Red Hat in AWS using boto3?


I'm working on a task where I need to validate if a user has created an EC2 instance with RHEL latest image using Cloudtrail logs for that user.

I was able to find the AMI image IDs used by that user using boto3.

I want to check whether the AMI image ID used by the user is present in the list of image IDs provided by RHEL.

How do I list down all the AMI image IDs for RedHat Inc.

I have gone through the boto3 documentation describe_images()

There's a param Owners in it. But not sure what should I pass in there to get all the image IDs for Redhat.

PS: Found that Redhat owner ID is - 309956199498 and we can query with it using ec2.describe_images()


Solution

  • The following code lists all RHEL-8 AMI IDs in the specified region:

    import boto3
    
    ec2 = boto3.resource('ec2', region_name='us-east-1')
    filters = [
        {'Name': 'owner-id', 'Values': ['309956199498']}, 
        {'Name':'name', 'Values':['RHEL-8*']}
    ]
    images = ec2.images.filter(Filters=filters).all()
    
    for image in images:
        print(image.id)
    

    Adapted from the CLI command in https://access.redhat.com/solutions/15356.