is it possible to list out all security groups associated with an EC2 instance with boto3
? if so how is this done?
I have tried the following methods but they are not doing what I want:
for region in regions:
client = boto3.client('ec2', region_name=region)
try:
payload = client.describe_security_groups(Filters=[{'Name': 'vpc-id', 'Values': ['vpc-*']}])
for sg in payload["SecurityGroups"]:
if sg["Description"] != "default VPC security group":
resp = client.describe_security_group_references(DryRun=False, GroupId=[sg["GroupId"]])
print resp
except Exception as E:
print region, E
continue
for region in regions:
client = boto3.client('ec2', region_name=region)
try:
payload = client.describe_security_groups(Filters=[{'Name': 'vpc-id', 'Values': ['vpc-*']}])
for sg in payload["SecurityGroups"]:
if sg["Description"] != "default VPC security group":
sg = json.dumps(sg)
pp(sg)
# x = requests.post(url=sumocollector, data=sg)
# print x.status_code
except Exception as E:
print region, E
continue
If you're looking for a list of SGs for each instance, then I'd do describe_instances
instead:
for region in regions:
client = boto3.client('ec2', region_name=region)
try:
response = client.describe_instances()
for reservation in response['Reservations']:
for instance in reservation['Instances']:
print("Instance: " + instance['InstanceId'])
for securityGroup in instance['SecurityGroups']:
print("SG ID: {}, Name: {}".format(securityGroup['GroupId'], securityGroup['GroupName']))
except Exception as E:
print(region, E)
continue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.