Skip to content

Instantly share code, notes, and snippets.

@daniyel
Created September 7, 2018 12:35
Show Gist options
  • Save daniyel/1a132a25f9373d8a0722765359d13e48 to your computer and use it in GitHub Desktop.
Save daniyel/1a132a25f9373d8a0722765359d13e48 to your computer and use it in GitHub Desktop.
Get list of Ubuntu 16.04 LTS latest AMIs for AWS CloudFormation
#!/bin/sh
remove_last_comma() { sed '
$x;$G;/\(.*\),/!H;//!{$!d
}; $!x;$s//\1/;s/^\n//'
}
response=$(curl -s "https://cloud-images.ubuntu.com/locator/ec2/releasesTable" | remove_last_comma)
regions=$(echo $response | jq -c '.aaData[] | select(contains(["16.04", "hvm:ebs"])) | .[0]' | sed 's/"//g')
images=$(echo $response | jq -c '.aaData[] | select(contains(["16.04", "hvm:ebs"])) | .[6]' | grep -o '>ami-[a-z0-9]\+' | sed 's/>//g')
regionsarr=(${regions//$'\n'/ })
imagesarr=(${images//$'\n'/ })
counter=0;
for region in "${regionsarr[@]}"
do
echo "$region:"
echo " US1604HVM: ${imagesarr[$counter]}"
counter=$((counter+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment