Skip to content

Instantly share code, notes, and snippets.

@xcsrz
Created February 25, 2022 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcsrz/c9a6834b5c5d03597ace2152f6b8a531 to your computer and use it in GitHub Desktop.
Save xcsrz/c9a6834b5c5d03597ace2152f6b8a531 to your computer and use it in GitHub Desktop.
This script prints out the current list of Amazon2 Linux AMI's by region (kernel 5.10, HVM, x86_64, gp2). Changing the image variable and the Path filter make this capable of searching for any public/shared image.
import boto3
from json import dumps
from yaml import dump
image = 'amzn2-ami-kernel-5.10-hvm-x86_64-gp2'
regions = [
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-south-1',
'ap-southeast-1',
'ap-southeast-2',
'ca-central-1',
'eu-central-1',
'eu-north-1',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'sa-east-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
]
amis = {}
for region in regions:
client = boto3.client('ssm', region)
paginator = client.get_paginator('get_parameters_by_path')
iterator = paginator.paginate(
Path='/aws/service/ami-amazon-linux-latest'
)
matches = []
for batch in iterator:
matches.extend(list(filter(lambda det: image in det['Name'], batch['Parameters'])))
if len(matches) < 1:
print('no matches found in', region)
continue
elif len(matches) > 1:
print('multiple matches found in', region, '- perhaps you should narrow your results')
for match in matches:
print("\t-", match)
# grab the first one if we have more than one
print('found', matches[0]['Name'], matches[0]['Value'], 'in', region)
amis[region] = {'AMI': matches[0]['Value']}
print(dumps(amis, indent=4))
print(dump(amis))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment