aws cli search for latest ami

This simple oneliner gets you the latest ami in a given region for a specific search term.

# get the latest ecs-optimized amazon ami in eu-west-1
aws ec2 describe-images --region eu-west-1 --owners amazon --filters "Name=name,Values=*amazon-ecs-optimized" "Name=architecture,Values=x86_64" "Name=architecture,Values=x86_64" --query 'sort_by(Images, &CreationDate)[-1].ImageId'

So lets break it down to give you what you need to get your favorite ami.

# Get the full ami list for name search term "*amazon-ecs-optimized"
aws ec2 describe-images --region eu-west-1 --owners amazon --filters "Name=name,Values=*amazon-ecs-optimized" "Name=architecture,Values=x86_64"

# this is searching for just the name, you could of course replace or expand this with other atributes like:
aws ec2 describe-images --region eu-west-1 --owners amazon --filters "Name=name,Values=*amazon-ecs-optimized" "Name=architecture,Values=x86_64" "Name=block-device-mapping.volume-type,Values=gp2"

the full filter atributes list can be found in the aws docs

Now lets take a look at the structure of the returned json

aws ec2 describe-images --region eu-west-1 --owners amazon --filters "Name=name,Values=*amazon-ecs-optimized" "Name=architecture,Values=x86_64" | head

# example output:
    "Images": [
            "Architecture": "x86_64",
            "CreationDate": "2017-11-10T19:59:12.000Z",
            "ImageId": "ami-014ae578",
            "ImageLocation": "amazon/amzn-ami-2017.09.b-amazon-ecs-optimized",
            "ImageType": "machine",
            "Public": true,
            "OwnerId": "591542846629",

This returnes an Images array with your results (ami with all the juicy metadata)

I use the built in query tool to grok this data (the query is written in JMESPath), to get at the object that we want we take the following steps:

  • sort an array
  • which one: the Images array
  • what to sort that data by the value of CreationDate
  • return the last object
  • retrieve .ImageId from the returned object

return objects from the Images array sorted by value of date

--query 'sort_by(Images, &CreationDate)'

just get the -1 object (-1 last, 0 first)

--query 'sort_by(Images, &CreationDate)[-1]'

get the ami id from the returned object

--query 'sort_by(Images, &CreationDate)[-1].ImageId'

Testing these query’s can be done over here: JMESPath

Now put all this together and you get:

$ aws ec2 describe-images --owners amazon --filters "Name=name,Values=*amazon-ecs-optimized" "Name=architecture,Values=x86_64" --query 'sort_by(Images, &CreationDate)[-1].ImageId'

Elastic beanstalk latest PHP SolutionStackName finder

aws --region us-east-2 elasticbeanstalk list-available-solution-stacks --query 'SolutionStackDetails[?contains(SolutionStackName, `PHP`) == `true`].[SolutionStackName][-1]'

Figuring this out took me way too long so thought I’d share it here

More examples I found in a blogpost of OpenSource Connections

Cloud & Open-Source magician 🧙‍♂️

I try to find the KISS in complex systems and share it with the world.

comments powered by Disqus