Skip to content

Commit

Permalink
Script to audit instances and sgs
Browse files Browse the repository at this point in the history
This script gets all your instances, their tags, and the attached security groups and rules.  Useful for auditing and compliance reporting
  • Loading branch information
ozmodiar192 authored May 14, 2021
1 parent 939fc93 commit a9a4f25
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions getInstancesAndSGs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python
import boto3
import json

client = boto3.client('ec2')
instancedata = {"Instances": []}

try:
response = client.describe_instances()
for reservation in response['Reservations']:
for instance in reservation['Instances']:
instancetags = {"InstanceTags": {}}
securitygroups = {"SecurityGroup": {}}
instanceid = instance['InstanceId']
instancetags["InstanceTags"] = instance['Tags']
combined = {instanceid: {}}
combined[instanceid] = instancetags
for securityGroup in instance['SecurityGroups']:
response = client.describe_security_groups(
GroupIds=[
securityGroup["GroupId"]
],
)
sgname = response['SecurityGroups'][0]['GroupName']
currentsg = {sgname: {}}
currentsg[sgname]["SGDesc"] = response['SecurityGroups'][0]['Description']
currentsg[sgname ]["IPPermissions"] = response['SecurityGroups'][0]['IpPermissions']
securitygroups["SecurityGroup"].update(currentsg)
combined[instanceid].update(securitygroups)
instancedata["Instances"].append(combined)
print(json.dumps(instancedata, indent=2))

except Exception as E:
print(E)

0 comments on commit a9a4f25

Please sign in to comment.