Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jackdpeterson/45e3b44a21095ebf9ae3e6b2979d84e5 to your computer and use it in GitHub Desktop.
Save jackdpeterson/45e3b44a21095ebf9ae3e6b2979d84e5 to your computer and use it in GitHub Desktop.
Copy an AWS EC2 security group to another region
#!/usr/bin/env bash
### inserts all CIDRs into a new security group.
### Doesn't set correct protocol or the description ... but at least it makes the data entry a bit quicker since you can't copy/pasta in the EC2 UI easily.
SOURCE_REGION=us-west-1
SOURCE_PROFILE=default
SOURCE_SG_ID=sg-id-goes-here
DESTINATION_REGION=us-east-2
DESTINATION_PROFILE=default
DESTINATION_SG_ID=sg-destination-goes-here
aws --profile $SOURCE_PROFILE --region=$SOURCE_REGION ec2 describe-security-groups --group-ids=$SOURCE_SG_ID | jq -r '.SecurityGroups[].IpPermissions[].IpRanges[] | [.CidrIp,.Description] | @tsv' | while IFS=$'\t' read -r ip description; do
aws ec2 --profile $DESTINATION_PROFILE --region $DESTINATION_REGION authorize-security-group-ingress --group-id $DESTINATION_SG_ID --protocol "-1" --cidr $ip
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment