Skip to content

Instantly share code, notes, and snippets.

@radeksimko
Last active August 29, 2015 14:15
Show Gist options
  • Save radeksimko/052450dffee1c231d59b to your computer and use it in GitHub Desktop.
Save radeksimko/052450dffee1c231d59b to your computer and use it in GitHub Desktop.
Search same AMI in different regions, output in Terraform format
AMI_ID=$1
REGION=$2
output=$(aws --region $REGION ec2 describe-images \
--filter "Name=image-id,Values=$AMI_ID" --output=text | head -1)
ORIGINAL_AMI_NAME=$(echo "$output" | awk -F\t '{print $11}')
ORIGINAL_AMI_OWNER=$(echo "$output" | awk -F\t '{print $12}')
if [[ ! $ORIGINAL_AMI_OWNER =~ ^[0-9]{10,}$ ]]; then
ORIGINAL_AMI_NAME=$(echo "$output" | awk -F\t '{print $9}')
ORIGINAL_AMI_OWNER=$(echo "$output" | awk -F\t '{print $10}')
fi
REGIONS=$(aws --region $REGION ec2 describe-regions --output=text \
| awk -F\t '{print $3}' | sort)
for region in $REGIONS; do
AMI_ID=$(aws --region $region ec2 describe-images \
--filter "Name=name,Values=$ORIGINAL_AMI_NAME" \
--owners $ORIGINAL_AMI_OWNER --output=text | head -1 \
| awk -F\t '{print $6}')
echo "$region = \"$AMI_ID\""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment