Skip to content

Instantly share code, notes, and snippets.

@dongsupark
Created December 10, 2021 09:58
Show Gist options
  • Save dongsupark/bf5ad50279d4789050ec9a6f29ad0882 to your computer and use it in GitHub Desktop.
Save dongsupark/bf5ad50279d4789050ec9a6f29ad0882 to your computer and use it in GitHub Desktop.
make AMIs public in all AWS regions
#!/bin/bash
# Run with a filter for image names like:
#
# ./make-amis-public.sh capa-ami-flatcar*
#
if [ -z "$1" ] ; then
echo "Please pass the name of the AMI"
exit 1
fi
IMAGE_FILTER="${1}"
declare -a REGIONS=($(aws ec2 describe-regions --output json | jq -r '[.Regions[].RegionName] | join(" ")'))
for REGION in "${REGIONS[@]}" ; do
AMI_IDS=$(aws ec2 describe-images --query 'Images[*].[ImageId]' --filters "Name=name,Values=${IMAGE_FILTER}" --region "${REGION}" --output text)
for AMI_ID in ${AMI_IDS}; do
echo "region ${REGION}: making public AMI ${AMI_ID} ..."
aws ec2 modify-image-attribute --region "${REGION}" --image-id "${AMI_ID}" \
--launch-permission "Add=[{Group=all}]"
echo "image attributes of AMI ${AMI_ID} ..."
aws ec2 describe-image-attribute --region "${REGION}" --image-id "${AMI_ID}" \
--attribute launchPermission
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment