Last active
February 3, 2021 15:23
-
-
Save antonmry/243778e9db1225aea808c33b16bb76f0 to your computer and use it in GitHub Desktop.
Simple bash script to update a Security Group matched by description and port in AWS with your Public IP
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
#! /bin/bash | |
publicIP=`dig +short myip.opendns.com @resolver1.opendns.com` | |
## TODO: move port, description and group-id to variables | |
## Consult previous one | |
cidrIP=`aws ec2 describe-security-groups --group-ids sg-XXXXXX | jq -r '.SecurityGroups[0].IpPermissions[] | select(.ToPort==80) | .IpRanges[] | select(.Description == "antonmry") | .CidrIp' | tail -1f` | |
## Delete the previous one | |
if [ -n "${cidrIP}" ]; then | |
aws ec2 revoke-security-group-ingress --group-id sg-XXXXXX --ip-permissions '[{"IpProtocol": "tcp", "FromPort": 80, "ToPort": 80, "IpRanges": [{"CidrIp":"'$cidrIP'"}]}]' | |
fi | |
## Add the new one | |
aws ec2 authorize-security-group-ingress --group-id sg-XXXXXX --ip-permissions '[{"IpProtocol": "tcp", "FromPort": 80, "ToPort": 80, "IpRanges": [{"CidrIp": "'$publicIP'/32", "Description": "antonmry"}]}]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment