Skip to content

Instantly share code, notes, and snippets.

@DaisukeMiyamoto
Created September 12, 2018 05:00
Show Gist options
  • Save DaisukeMiyamoto/2d9ed49dc7625adc41835beab7aad58e to your computer and use it in GitHub Desktop.
Save DaisukeMiyamoto/2d9ed49dc7625adc41835beab7aad58e 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