Skip to content

Instantly share code, notes, and snippets.

@l1ahim
Forked from DaisukeMiyamoto/assume_role.py
Created April 24, 2020 12:18
Show Gist options
  • Save l1ahim/c4904554be908e3ed8e4aab16874f9bc to your computer and use it in GitHub Desktop.
Save l1ahim/c4904554be908e3ed8e4aab16874f9bc 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