Last active
July 10, 2020 09:40
-
-
Save gemmadlou/6505c27b56c0b47f5f8277bb2ead7ecc to your computer and use it in GitHub Desktop.
AWS reassigning IP addresses automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function renew_aws_ip { | |
SECURITY_GROUP=$1 | |
NAME_OF_RULE=$2 | |
REGION="--region=eu-west-1" | |
if [ ! -z "$3" ]; then | |
REGION="--region=$3" | |
fi | |
echo "Remove security group rule for $SECURITY_GROUP" | |
aws ec2 describe-security-groups $REGION \ | |
--group-ids $SECURITY_GROUP \ | |
--query "SecurityGroups[0].IpPermissions[*].IpRanges" \ | |
| jq 'flatten | .[] | select(.Description == "'$NAME_OF_RULE'") | .CidrIp ' \ | |
| xargs -I {} aws ec2 revoke-security-group-ingress $REGION --group-id $SECURITY_GROUP --protocol -1 --port 0-65535 --cidr {} | |
echo "Add security group rule for $SECURITY_GROUP" | |
curl ifconfig.me | xargs -I {} aws ec2 authorize-security-group-ingress $REGION --group-id $SECURITY_GROUP --ip-permissions IpProtocol=-1,FromPort=0,ToPort=65535,IpRanges='[{CidrIp={}/32,Description="'$NAME_OF_RULE'"}]' | |
} | |
function renew_my_aws_ip { | |
renew_aws_ip "sg-xxxxxx" "Gemma" | |
renew_aws_ip "sg-xxxxxx" "Gemma" "us-east-1" | |
} | |
alias ipup='renew_my_aws_ip' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment