Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Last active July 10, 2020 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gemmadlou/6505c27b56c0b47f5f8277bb2ead7ecc to your computer and use it in GitHub Desktop.
Save gemmadlou/6505c27b56c0b47f5f8277bb2ead7ecc to your computer and use it in GitHub Desktop.
AWS reassigning IP addresses automatically
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