-
-
Save udhos/6ced9554173e105c3cf289b57a52ecb4 to your computer and use it in GitHub Desktop.
AWS Boto3 Assume Role example
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
import boto3 | |
from boto3.session import Session | |
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