Skip to content

Instantly share code, notes, and snippets.

@antimius
Last active March 19, 2018 05:04
Show Gist options
  • Save antimius/6248477b363d0655a32a63e27af7e219 to your computer and use it in GitHub Desktop.
Save antimius/6248477b363d0655a32a63e27af7e219 to your computer and use it in GitHub Desktop.
Script to find and print the latest stable, HVM, EBS GP2 backed EC2 AMIs from major OS vendors. Requires aws-cli to be installed. Takes a long time to complete due to AWS describe-images call being slow.
#!/bin/bash
# prints latest, stable, HVM, EBS GP2 backed AMIs for major OSs
# amazon, redhat 309956199498, ubuntu 099720109477, freebsd 118940168514, CentOS marketplace code
REDHAT=309956199498
UBUNTU=099720109477
FREEBSD=118940168514
MICROSOFT=801119661308
REGION=${REGION:-"us-east-1"}
aws ec2 describe-images --region=$REGION --owners amazon --query 'Images[?Name!=`null`]|[?starts_with(Name,`amzn-ami-hvm-2016`)]|[?ends_with(Name, `gp2`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners $UBUNTU --query 'Images[?Name!=`null`]|[?starts_with(Name,`ubuntu/images/hvm-ssd/ubuntu-xenial`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners $UBUNTU --query 'Images[?Name!=`null`]|[?starts_with(Name,`ubuntu/images/hvm-ssd/ubuntu-trusty`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners $REDHAT --query 'Images[?Name!=`null`]|[?starts_with(Name,`RHEL-6`)]|[?contains(Name,`GA`)]|[?ends_with(Name, `GP2`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners $REDHAT --query 'Images[?Name!=`null`]|[?starts_with(Name,`RHEL-7`)]|[?contains(Name,`GA`)]|[?ends_with(Name, `GP2`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners $FREEBSD --query 'Images[?Name!=`null`]|[?starts_with(Name,`FreeBSD 11.0-STABLE`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners aws-marketplace --filters Name=product-code,Values=6x5jmcajty9edm3f211pqjfn2 --query 'Images[*].[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners aws-marketplace --filters Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce --query 'Images[*].[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners $MICROSOFT --query 'Images[?Name!=`null`]|[?starts_with(Name,`Windows_Server-2012-R2_RTM-English-64Bit-Base`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
aws ec2 describe-images --region=$REGION --owners $MICROSOFT --query 'Images[?Name!=`null`]|[?starts_with(Name,`Windows_Server-2016-English-Full-Base`)]|max_by(@, &CreationDate).[CreationDate,ImageId,Name]' --output text
@antimius
Copy link
Author

antimius commented Feb 2, 2017

Sample output:

2017-01-14T02:34:00.000Z        ami-6edd3078    ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170113
2017-01-11T05:23:19.000Z        ami-49c9295f    ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170110
2016-05-03T20:14:12.000Z        ami-ef14f582    RHEL-6.8_HVM_GA-20160503-x86_64-1-Hourly2-GP2
2016-10-26T22:32:29.000Z        ami-b63769a1    RHEL-7.3_HVM_GA-20161026-x86_64-1-Hourly2-GP2
2017-01-27T07:04:35.000Z        ami-b96d9eaf    FreeBSD 11.0-STABLE-amd64-2017-01-27
2016-02-26T21:11:12.000Z        ami-1c221e76    CentOS Linux 6 x86_64 HVM EBS 1602-74e73035-3435-48d6-88e0-89cc02ad83ee-ami-21e6d54b.3
2016-02-26T21:11:52.000Z        ami-6d1c2007    CentOS Linux 7 x86_64 HVM EBS 1602-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-d7e1d2bd.3
2017-01-11T04:31:13.000Z        ami-abf616bd    Windows_Server-2012-R2_RTM-English-64Bit-Base-2017.01.11
2017-01-12T00:05:03.000Z        ami-188d6e0e    Windows_Server-2016-English-Full-Base-2017.01.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment