How to use describe_stale_security_groups method in localstack

Best Python code snippet using localstack_python

ec2.py

Source:ec2.py Github

copy

Full Screen

...113 sec_group['GroupId'], e)114 return security_groups if security_groups else None115 def get_stale_security_groups(self, vpc_id):116 results = []117 response = self.client.describe_stale_security_groups(VpcId=vpc_id)118 results.extend(response['StaleSecurityGroupSet'])119 while 'NextToken' in response:120 next_token = response['NextToken']121 response = self.client.describe_stale_security_groups(VpcId=vpc_id, NextToken=next_token)122 results.extend(response['StaleSecurityGroupSet'])123 next_token = response['NextToken'] if response.get('NextToken') else False124 stale_sg_list = []125 for stale_sg in results:126 stale_sg_list.append({'id': stale_sg['GroupId'], 'name': stale_sg['GroupName'], 'region': self.region_name})127 return stale_sg_list128 def get_volume_details(self):129 results = []130 response = self.client.describe_volumes()131 results.extend(response['Volumes'])132 while 'NextToken' in response:133 next_token = response['NextToken']134 response = self.client.describe_volumes(NextToken=next_token)135 results.extend(response['Volumes'])...

Full Screen

Full Screen

cleanup-packer-aws-resources.py

Source:cleanup-packer-aws-resources.py Github

copy

Full Screen

...218 # # Get our VPCs219 # vpcs = ec2.describe_vpcs()['Vpcs']220 # # Get all stale sgs in every vpc221 # for vpc in vpcs:222 # stale = ec2.describe_stale_security_groups(VpcId=vpc['VpcId'])['StaleSecurityGroupSet']223 # pprint(stale)224 225 226 for pair in response['SecurityGroups']:227 regionoutput.append(pair['GroupName'])228 229 #if len(regionoutput) > 0:230 # stalegroups = ec2.describe_security_groups231 # for groupname in regionoutput:232 233 output[region] = regionoutput234 return output235def lambda_handler(event, context):236 global regions, max_age...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

1from itertools import islice2import boto33import texttable4ec2_client = boto3.client('ec2')5lambda_client = boto3.client('lambda')6elb_client = boto3.client('elb')7elbv2_client = boto3.client('elbv2')8rds_client = boto3.client('rds')9ecs_client = boto3.client('ecs')10print('Retrieving all security groups ...')11paginator = ec2_client.get_paginator('describe_security_groups')12page_iterator = paginator.paginate()13security_groups = []14for page in page_iterator:15 security_groups.extend(page['SecurityGroups'])16print('\nExternally Referenced Security Groups:')17list_length = 20018external_sgs = set()19for sublist in [security_groups[i:i+list_length] for i in range(0, len(security_groups), list_length)]:20 response = ec2_client.describe_security_group_references(21 GroupId=[sg['GroupId'] for sg in sublist]22 )23 external_sgs.update(set(response['SecurityGroupReferenceSet']))24print(external_sgs)25print('\nStale Security Groups')26stale_sgs = set()27paginator = ec2_client.get_paginator('describe_stale_security_groups')28page_iterator = paginator.paginate(VpcId=security_groups[0]['VpcId'])29for page in page_iterator:30 stale_sgs.update(set(page['StaleSecurityGroupSet']))31print('Lambda Security Groups:')32paginator = lambda_client.get_paginator('list_functions')33page_iterator = paginator.paginate()34lambda_functions = []35for page in page_iterator:36 lambda_functions.extend(page['Functions'])37lambda_sgs = set()38for function in lambda_functions:39 if 'VpcConfig' in function:40 lambda_sgs.update(set(function['VpcConfig']['SecurityGroupIds']))41print(lambda_sgs)42print('\nELB Classic Security Groups:')43paginator = elb_client.get_paginator('describe_load_balancers')44page_iterator = paginator.paginate()45elbs = []46for page in page_iterator:47 elbs.extend(page['LoadBalancerDescriptions'])48elb_sgs = set()49for elb in elbs:50 elb_sgs.update(set(elb['SecurityGroups']))51print(elb_sgs)52print('\nELBv2 Security Groups:')53paginator = elbv2_client.get_paginator('describe_load_balancers')54page_iterator = paginator.paginate()55elbv2s = []56for page in page_iterator:57 elbv2s.extend(page['LoadBalancers'])58elbv2_sgs = set()59for elbv2 in elbv2s:60 if 'SecurityGroups' in elbv2:61 elbv2_sgs.update(set(elbv2['SecurityGroups']))62print(elbv2_sgs)63print('\nRDS Security Groups:')64paginator = rds_client.get_paginator('describe_db_security_groups')65page_iterator = paginator.paginate()66rds_db_securitygroups = []67for page in page_iterator:68 rds_db_securitygroups.extend(page['DBSecurityGroups'])69rds_sgs = set()70for rds_db_sg in rds_db_securitygroups:71 rds_sgs.update(set([sg['EC2SecurityGroupId'] for sg in rds_db_sg['EC2SecurityGroups']]))72paginator = rds_client.get_paginator('describe_db_instances')73page_iterator = paginator.paginate()74rds_db_instances = []75for page in page_iterator:76 rds_db_instances.extend(page['DBInstances'])77for rds_db_instance in rds_db_instances:78 rds_sgs.update(set([sg['VpcSecurityGroupId'] for sg in rds_db_instance['VpcSecurityGroups']]))79print(rds_sgs)80print('\nECS Security Groups:')81fargate_services = []82fargate_sgs = set()83for cluster_arn in ecs_client.list_clusters().get('clusterArns', None):84 paginator = ecs_client.get_paginator('list_services')85 page_iterator = paginator.paginate(cluster=cluster_arn, launchType='FARGATE')86 service_arns = []87 for page in page_iterator:88 service_arns.extend(page['serviceArns'])89 list_length = 1090 for sublist in [service_arns[i:i+list_length] for i in range(0, len(service_arns), list_length)]:91 response = ecs_client.describe_services(cluster=cluster_arn, services=sublist)92 for service in response['services']:93 fargate_services.append(service)94 if 'networkConfiguration' in service and 'awsvpcConfiguration' in service['networkConfiguration'] and 'securityGroups' in service['networkConfiguration']['awsvpcConfiguration']:95 fargate_sgs.update(set(service['networkConfiguration']['awsvpcConfiguration']['securityGroups']))96print(fargate_sgs)97print('\n=================\n')98print('\n All SGS:')99all_sgs = set([sg['GroupId'] for sg in security_groups])100print(all_sgs)101print('\n Used SGS:')102used_sgs = lambda_sgs.union(fargate_sgs, elb_sgs, elbv2_sgs, rds_sgs)103print(used_sgs)104print('\nStale SGS:')105print(stale_sgs)106print('\n Unused SGS:')107unused_sgs = all_sgs.difference(used_sgs)108print(unused_sgs)109table = texttable.Texttable()110table.add_row(['id', 'name'])111items = [[sg['GroupId'], sg['GroupName']] for sg in security_groups if sg['GroupId'] in unused_sgs]112table.add_rows( items)113print(table.draw())114# print(all_sgs.difference(used_sgs))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful