Skip to content

Instantly share code, notes, and snippets.

@mamonu
Forked from daveadams/find-latest-ami.sh
Created July 29, 2022 17:40
Show Gist options
  • Save mamonu/de8f473ac3f6e302ba00030bf1a33491 to your computer and use it in GitHub Desktop.
Save mamonu/de8f473ac3f6e302ba00030bf1a33491 to your computer and use it in GitHub Desktop.
Find latest Amazon deep learning AMI
#!/usr/bin/env bash
# Find the latest Amazon-created "Deep Learning AMI (Ubuntu 18.04)" AMI image ID
#
# args explanation:
# --region us-east-1
# Specifies the AWS region (you can also specify it in your
# ~/.aws/config or via the `AWS_REGION` or `AWS_DEFAULT_REGION`
# env vars)
#
# --filters
# After this argument, each following argument in the form:
# Name=<name>,Values=<value1>[,<value2>...]
# will be interpreted as a filter for narrowing the search, as
# documented in `aws ec2 describe-images help`.
#
# Name=owner-id,Values=898082745236
# Only show results from the AWS account 898082745236, which is
# Amazon's image distribution account. Adding this filter prevents
# getting random other public AMIs that match the Name field.
#
# Name=name,Values="Deep Learning AMI (Ubuntu 18.04) *"
# This will match any AMI with a `Name` property beginning with
# "Deep Learning AMI (Ubuntu 18.04) ". The "*" means to match any
# remaining characters (for these AMIs, the remaining string is always
# "Version NN.N").
#
# --query 'reverse(sort_by(Images, &CreationDate))[0].ImageId'
# This is a JMESPath (https://jmespath.org/examples.html) query over
# the results of the filters (about 50 AMI images dating back to 2020).
# `sort_by(Images, &CreationDate)` sorts the images by the CreationDate
# field. `reverse()` reverses that list so the newest is first, then
# `[0]` takes the first result only, and `.ImageId` prints the AMI ID.
#
# --output text
# This just ensures the output of the command won't come with extraneous
# quotes, depending on your setting for AWS CLI output formatting.
#
aws ec2 describe-images \
--region us-east-1 \
--filters \
Name=owner-id,Values=898082745236 \
Name=name,Values="Deep Learning AMI (Ubuntu 18.04) *" \
--query 'reverse(sort_by(Images, &CreationDate))[0].ImageId' \
--output text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment