How to add and delete security group rules in AWS via the CLI (and list them too!)
erics, September 13th, 2023
Add inbound rule(s) for a security group ID:
1 |
shell> aws ec2 authorize-security-group-ingress --group-id sg-NNNNNNNN --protocol tcp --port 80 --cidr '0.0.0.0/0' |
## Delete inbound rule(s) for a security group ID
1 |
shell> aws ec2 revoke-security-group-ingress --group-id sg-NNNNNNNN --protocol tcp --port 80 --cidr '0.0.0.0/0' |
## List security groups by security group ID
1 |
shell> aws ec2 describe-security-groups --output json | jq -r '.SecurityGroups[]|.GroupId+" "+.GroupName' |
## List inbound rules for a specific security group ID
1 |
shell> aws ec2 describe-security-groups --group-ids sg-NNNNNNNN --output json | jq -r '.SecurityGroups[].IpPermissions[]|. as $parent|(.IpRanges[].CidrIp+" "+($parent.ToPort|tostring))' |
Thanks to: https://www.bluematador.com/learn/aws-cli-cheatsheet