Best Python code snippet using localstack_python
create_instance_profile.py
Source:create_instance_profile.py
...66 cfnresponse.send(event, context, cfnresponse.FAILED, {}, resource_id)67def delete_all(iam, name):68 clean_policies(iam, name)69 clean_instance_profile(iam, name)70 delete_instance_profile(iam, name)71 delete_role(iam, name)72def clean_policies(iam, name):73 try:74 attached = iam.list_attached_role_policies(RoleName=name)75 for policy in attached["AttachedPolicies"]:76 iam.detach_role_policy(RoleName=name, PolicyArn=policy["PolicyArn"])77 except Exception as e:78 print str(e)79def clean_instance_profile(iam, name):80 try:81 instance_profile = iam.get_instance_profile(InstanceProfileName=name)82 for role in instance_profile["InstanceProfile"].get("Roles", []):83 iam.remove_role_from_instance_profile(84 InstanceProfileName=name,85 RoleName=role["RoleName"]86 )87 except Exception as e:88 print str(e)89def delete_instance_profile(iam, name):90 try:91 iam.delete_instance_profile(InstanceProfileName=name)92 except Exception as e:93 print str(e)94def delete_role(iam, name):95 try:96 iam.delete_role(RoleName=name)97 except Exception as e:98 print str(e)99def handler(event, context):100 if event["RequestType"] == "Create":101 handler_create(event, context)102 elif event["RequestType"] == "Update":103 handler_update(event, context)104 else:105 handler_delete(event, context)
ec2.py
Source:ec2.py
...40 41 return to_terminate42def create_instance_profile(name, policy):43 # Remove it before starting44 delete_instance_profile(name)45 46 # Next, you need to create an Instance Profile in IAM.47 c = boto.connect_iam()48 instance_profile = c.create_instance_profile(name)49 # Once you have the instance profile, you need to create the role, 50 # add the role to the instance profile and associate the policy with the role.51 role = c.create_role(name)52 c.add_role_to_instance_profile(name, name)53 c.put_role_policy(name, name, policy)54 # Wait for it to be available55 for _ in xrange(10):56 57 # We want to wait in all cases, because I've seen the roles created58 # and available via the API but not to EC2 instances. So we better wait59 # for at least 1min60 logging.debug('Waiting for role %s to be available...' % name)61 time.sleep(60)62 63 try:64 c.get_role(name)65 c.get_instance_profile(name)66 break67 except:68 pass69 else:70 raise RuntimeError('Role creation for instance profile failed.')71 # Now, you can use that instance profile when you launch an instance72 return name73def delete_instance_profile(name):74 iam = boto.connect_iam()75 76 logging.warn('Removing IAM instance profile "%s"' % name)77 78 try:79 iam.remove_role_from_instance_profile(name, name)80 except Exception, e:81 pass82 83 try:84 iam.get_instance_profile(name)85 iam.delete_instance_profile(name)86 except Exception, e:87 pass88 89 try:90 iam.get_role_policy(name, name)91 iam.delete_role_policy(name, name)92 except Exception, e:93 pass94 try:95 iam.get_role(name)96 iam.delete_role(name)97 except Exception, e:...
teardown.py
Source:teardown.py
...15 else:16 logging.debug('Removing security group "%s"' % NAME)17 conn.delete_security_group(SG_NAME)18 ...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!