Skip to content

Instantly share code, notes, and snippets.

@thibautsacreste
Last active March 11, 2023 04:58
Show Gist options
  • Save thibautsacreste/5f23ab5ab846bab58b01c94db0acdfbd to your computer and use it in GitHub Desktop.
Save thibautsacreste/5f23ab5ab846bab58b01c94db0acdfbd to your computer and use it in GitHub Desktop.
Bash: AWS security group uses
#!/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'
@lab-cheung-arcadia
Copy link

Thank you! Very useful.

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