Skip to content

Instantly share code, notes, and snippets.

@shivam-tripathi
Last active June 4, 2021 05:31
Show Gist options
  • Save shivam-tripathi/641c57a0655f7baa88e466936a2c5cd2 to your computer and use it in GitHub Desktop.
Save shivam-tripathi/641c57a0655f7baa88e466936a2c5cd2 to your computer and use it in GitHub Desktop.
Bash script to white list your ip for a particular security group in AWS
export desc="shivam";
export sg_id="SG_ID"; # update this
export AWS_PAGER="";
# Revoke earlier white listed ip (compares it via desc)
aws ec2 describe-security-groups --group-ids $sg_id --output text | grep $desc | awk '{ print $2 }' | xargs -I {} aws ec2 revoke-security-group-ingress --group-id $sg_id --protocol tcp --port 0-65535 --cidr {};
# Give permission to current public ip
wget -qO- ifconfig.me | xargs -I {} aws ec2 authorize-security-group-ingress --group-id $sg_id --ip-permissions IpProtocol=tcp,FromPort=0,ToPort=65535,IpRanges='[{CidrIp={}/32,Description="'$desc'"}]';
@shivam-tripathi
Copy link
Author

Usage:

 wget -qO- https://<url_to_raw_gist> | sed 's/SG_ID/<your_sg_id>/g;s/shivam/<your_name>/g;'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment