Skip to content

Instantly share code, notes, and snippets.

@antonmry
Last active February 3, 2021 15:23
Show Gist options
  • Save antonmry/243778e9db1225aea808c33b16bb76f0 to your computer and use it in GitHub Desktop.
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
#! /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