Last active
March 11, 2023 04:58
-
-
Save thibautsacreste/5f23ab5ab846bab58b01c94db0acdfbd to your computer and use it in GitHub Desktop.
Bash: AWS security group uses
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
#!/usr/bin/env bash | |
# shows uses of an AWS security group: | |
# * lists all network interfaces it is attached to. | |
# * lists all other security groups referencing it in inbound rules. | |
# usage: aws.sg my-security-group-name | |
# requires aws-cli and jq. | |
group_name=$1 | |
group_id=`aws ec2 describe-security-groups --filters "Name=group-name,Values=$group_name" | \ | |
jq --raw-output '.SecurityGroups[0].GroupId'` | |
printf '* Network interfaces using this group:\n' | |
aws ec2 describe-network-interfaces --filter "Name=group-name,Values=$group_name" | \ | |
jq --raw-output '.NetworkInterfaces[] | [if .Attachment.InstanceId | |
then .Attachment.InstanceId | |
else .Attachment.InstanceOwnerId | |
end, | |
.PrivateIpAddress, | |
.PrivateDnsName, | |
.Description] | |
| @tsv' | |
printf "\n" | |
printf "* Other security groups referencing this group:\n" | |
aws ec2 describe-security-groups --filters Name=ip-permission.group-id,Values=$group_id | \ | |
jq --raw-output '.SecurityGroups[] | [.GroupId, .GroupName, .Description] | @tsv' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! Very useful.