Skip to content

Instantly share code, notes, and snippets.

@developerck
Created September 16, 2022 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developerck/664031cb59889704145cfb3f92e6985d to your computer and use it in GitHub Desktop.
Save developerck/664031cb59889704145cfb3f92e6985d to your computer and use it in GitHub Desktop.
check security groups in a region if linked to a instance or not
#!/usr/local/bin/python3
import re
from collections import OrderedDict
from pprint import pprint
import boto3
ec2 = None
# define your region and run in cloud shell
# python3 ./sg-check.py
ec2 = boto3.client('ec2', 'us-east-1')
linked = []
notlinked =[]
for sg in ec2.describe_security_groups()['SecurityGroups']:
nw = ec2.describe_network_interfaces( Filters=[{'Name': 'group-id','Values': [sg['GroupId']]}]);
nw = nw['NetworkInterfaces']
if not nw :
str = sg['GroupName']+' | '+sg['GroupId']+ ': Not Linked '
notlinked.append(str);
else:
str = sg['GroupName']+' | '+sg['GroupId']+ ': Linked '
linked.append(str)
print("===Linked===");
print("\n".join(linked));
print("===Not Linked===");
print("\n".join(notlinked));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment