Skip to content

Instantly share code, notes, and snippets.

@grahamlyons
Created November 21, 2019 22:01
Show Gist options
  • Save grahamlyons/0a9753c54fd1c2c439150dc9d8b70a31 to your computer and use it in GitHub Desktop.
Save grahamlyons/0a9753c54fd1c2c439150dc9d8b70a31 to your computer and use it in GitHub Desktop.
Assume a role in an AWS account with Python
import boto3
def assume_role(account_id: str, role_name: str) -> boto3.Session:
sts = boto3.client('sts')
response = sts.assume_role(
RoleArn=f'arn:aws:iam::{account_id}:role/{role_name}',
RoleSessionName='account_creation',
)
return boto3.Session(
response['Credentials']['AccessKeyId'],
response['Credentials']['SecretAccessKey'],
response['Credentials']['SessionToken'],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment