Last active
April 3, 2020 19:48
-
-
Save praveenc/57020665bb14f81dd99ac00b79dfa382 to your computer and use it in GitHub Desktop.
STS assume role using boto3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def assume_role(arn, session_name): | |
| """aws sts assume-role --role-arn arn:aws:iam::00000000000000:role/example-role --role-session-name example-role""" | |
| client = boto3.client('sts') | |
| account_id = client.get_caller_identity()["Account"] | |
| print(account_id) | |
| response = client.assume_role(RoleArn=arn, RoleSessionName=session_name) | |
| session = Session(aws_access_key_id=response['Credentials']['AccessKeyId'], | |
| aws_secret_access_key=response['Credentials']['SecretAccessKey'], | |
| aws_session_token=response['Credentials']['SessionToken']) | |
| client = session.client('sts') | |
| account_id = client.get_caller_identity()["Account"] | |
| print(account_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment