Skip to content

Instantly share code, notes, and snippets.

@ardeshireshghi
Last active January 22, 2018 16:36
Show Gist options
  • Select an option

  • Save ardeshireshghi/c5b4d088672c0b983460df8940551757 to your computer and use it in GitHub Desktop.

Select an option

Save ardeshireshghi/c5b4d088672c0b983460df8940551757 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
printUsage() {
cat <<EOT
Usage: $0 aws-region-name instance-type
aws-region-name: Name of the AWS region (e.g. eu-west-1)
instance-type: Either "ec2" or "ecs"
EOT
}
[ -z "$1" ] && echo "Error: Missing aws region parameter" && printUsage && exit 1
[ -z "$2" ] && echo "Error: Missing aws instance-type parameter" && printUsage && exit 1
AWS_REGION=$1
AWS_INSTANCE_TYPE=$2
ECS_INSTANCE_SUFFIX="*-amazon-ecs-optimized"
EC2_INSTANCE_SUFFIX="hvm-*-gp2"
if [ $AWS_INSTANCE_TYPE == "ec2" ]; then
NAME_FILTER=$EC2_INSTANCE_SUFFIX
else
NAME_FILTER=$ECS_INSTANCE_SUFFIX
fi
aws ec2 describe-images --region ${AWS_REGION} --owners amazon --filters "Name=name,Values=amzn-ami-${NAME_FILTER}" \
'Name=state,Values=available' | jq -r '.Images | sort_by(.CreationDate) | last(.[]).ImageId'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment