Created
November 21, 2019 22:01
-
-
Save grahamlyons/0a9753c54fd1c2c439150dc9d8b70a31 to your computer and use it in GitHub Desktop.
Assume a role in an AWS account with Python
This file contains 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 | |
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