How to use get_iam_role method in localstack

Best Python code snippet using localstack_python

iam_role_get.py

Source:iam_role_get.py Github

copy

Full Screen

1import boto32import sys3def get_iam_role(profile, region):4 session = boto3.Session(profile_name=profile, region_name=region)5 iam = session.client('iam')6 available_instance_profiles = []7 all_instance_profiles = iam.list_instance_profiles()8 for instance_profile in all_instance_profiles['InstanceProfiles']:9 available_instance_profiles.append(instance_profile['InstanceProfileName'])10 default_profile_name = "CloudBuster-InstanceProfile"11 if instance_profile['InstanceProfileName'] == default_profile_name:12 print(f"\nDefault instance profile found. \nWould you like to use {default_profile_name}?\n")13 if input("Hit Y to see other profiles, or any key to use default: ").lower() != 'y':14 # print(default_profile_name)15 return default_profile_name16 counter = 117 print("\nIAM INSTANCE ROLE")18 print("=================")19 print("Also called Instance Profiles")20 print("\nAvailable Instance Roles:\n")21 for role in available_instance_profiles:22 print(f"{counter} - {role}")23 counter += 124 25 try:26 index = int(input("\nChoose an Instance Profile (IAM Instance Role): ")) -127 if -1 < index < counter:28 # print(available_instance_profiles[index])29 return available_instance_profiles[index]30 31 except:32 print("\n##########\nWARNING!!!\n##########")33 print("That is not a valid profile/role")34 print("You will be required to enter a profile as a parameter in CF\n")35 36 37 38 39 # available_iam_roles = []40 # all_iam_roles = iam.list_roles()41 # for role in all_iam_roles['Roles']:42 # # print(role['RoleName'])43 # for statement in role['AssumeRolePolicyDocument']['Statement']:44 # # print(statement['Principal'])45 # # print(statement['Principal']['Service'])46 # for service in statement['Principal']:47 # # print(statement['Principal']['Service'])48 # if 'ec2' in statement['Principal']['Service']:49 # available_iam_roles.append(role['RoleName'])50 # available_iam_roles.append(role['RoleName'])51 # print(available_iam_roles)52 ...

Full Screen

Full Screen

ebsiam.py

Source:ebsiam.py Github

copy

Full Screen

...3import json4import boto.ec25import optparse6DEFAULT_REGION = 'eu-west-1'7def get_iam_role():8 """Read IAM role from AWS metadata store."""9 return urllib2.urlopen("http://169.254.169.254/latest/meta-data/iam/security-credentials/").read()10def get_credentials():11 """Read IAM credentials from AWS metadata store."""12 url = "http://169.254.169.254/latest/meta-data/iam/security-credentials/%s" % (get_iam_role(), )13 data = json.loads(urllib2.urlopen(url).read())14 return (data['AccessKeyId'], data['SecretAccessKey'], data['Token'])15def create_volume(size, device_name):16 (access_key, secret_key, token) = get_credentials()17 instance_id = urllib2.urlopen("http://169.254.169.254/latest/meta-data/instance-id").read()18 zone = urllib2.urlopen("http://169.254.169.254/latest/meta-data/placement/availability-zone").read()19 conn = boto.ec2.connect_to_region(20 region_name=DEFAULT_REGION,21 aws_access_key_id=access_key,22 aws_secret_access_key=secret_key,23 security_token=token24 )25 vol = conn.create_volume(size, zone)26 vol.attach(instance_id, device_name)...

Full Screen

Full Screen

elbiam.py

Source:elbiam.py Github

copy

Full Screen

...6import optparse7 8DEFAULT_REGION = 'eu-west-1'9 10def get_iam_role():11 """Read IAM role from AWS metadata store."""12 return urllib2.urlopen("http://169.254.169.254/latest/meta-data/iam/security-credentials/").read()13 14def get_credentials():15 """Read IAM credentials from AWS metadata store."""16 url = "http://169.254.169.254/latest/meta-data/iam/security-credentials/%s" % (get_iam_role(), )17 data = json.loads(urllib2.urlopen(url).read())18 return (data['AccessKeyId'], data['SecretAccessKey'], data['Token'])19 20def register_instance(load_balancer_name):21 (access_key, secret_key, token) = get_credentials()22 instance_id = urllib2.urlopen("http://169.254.169.254/latest/meta-data/instance-id").read()23 24 elb = boto.ec2.elb.connect_to_region(25 region_name=DEFAULT_REGION,26 aws_access_key_id=access_key,27 aws_secret_access_key=secret_key,28 security_token=token29 )30 ...

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