Skip to content

Instantly share code, notes, and snippets.

@ashvayka
Last active October 11, 2020 00:34
Show Gist options
  • Save ashvayka/4f05f23db7ff68721f467570a9f44df4 to your computer and use it in GitHub Desktop.
Save ashvayka/4f05f23db7ff68721f467570a9f44df4 to your computer and use it in GitHub Desktop.
Copy Thingsboard AWS AMIs to all regions
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: copy-ami.sh AMI_ID"
return 1
fi
SRC_REGION=us-east-1
SRC_AMI=$1
NEW_AMI_NAME="Thingsboard 1.2.2 AMI for micro instances"
NEW_AMI_DESC="Thingsboard is an open-source IoT platform for data collection, processing, visualization, and device management."
declare -a REGION_NAMES=( "Ohio" "N. California" "Oregon" "Canada" "Ireland" "Frankfurt" "London" "Singapore" "Sydney" "Seoul" "Tokyo" "Mumbai" "São Paulo" )
declare -a REGION_IDS=( "us-east-2" "us-west-1" "us-west-2" "ca-central-1" "eu-west-1" "eu-central-1" "eu-west-2" "ap-southeast-1" "ap-southeast-2" "ap-northeast-2" "ap-northeast-1" "ap-south-1" "sa-east-1" )
declare -a AMI_IDS=()
for (( i=0; i<= ${#REGION_IDS[*]} -1; i++ ))
do
REGION_AMI_ID=$(aws ec2 copy-image --source-image-id $SRC_AMI --source-region $SRC_REGION --region ${REGION_IDS[$i]} --name "$NEW_AMI_NAME" --description "$NEW_AMI_DESC" --query 'ImageId' --output text)
AMI_IDS[$i]=$REGION_AMI_ID
done
for (( i=0; i<= ${#AMI_IDS[*]} -1; i++ ))
do
REGION_AMI_STATE=$(aws ec2 describe-images --region ${REGION_IDS[$i]} --image-ids ${AMI_IDS[$i]} --query 'Images[*].State' --output text)
while [ $REGION_AMI_STATE != "available" ]; do
echo "Waiting for ${REGION_IDS[$i]} ${AMI_IDS[$i]} to become available for 5 sec"
sleep 5
REGION_AMI_STATE=$(aws ec2 describe-images --region ${REGION_IDS[$i]} --image-ids ${AMI_IDS[$i]} --query 'Images[*].State' --output text)
done
done
for (( i=0; i<= ${#AMI_IDS[*]} -1; i++ ))
do
aws ec2 modify-image-attribute --region ${REGION_IDS[$i]} --image-id ${AMI_IDS[$i]} --launch-permission "{\"Add\": [{\"Group\":\"all\"}]}"
echo " - [${REGION_NAMES[$i]} (${REGION_IDS[$i]})](https://console.aws.amazon.com/ec2/v2/home?region=${REGION_IDS[$i]}#LaunchInstanceWizard:ami=${AMI_IDS[$i]})"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment