Skip to content

Instantly share code, notes, and snippets.

@sengstacken
Forked from DaisukeMiyamoto/assume_role.py
Created August 20, 2020 22:01
Show Gist options
  • Save sengstacken/051d0feb90f7278a1fae2c8d689ad965 to your computer and use it in GitHub Desktop.
Save sengstacken/051d0feb90f7278a1fae2c8d689ad965 to your computer and use it in GitHub Desktop.
AWS Boto3 Assume Role example
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