Skip to content

Instantly share code, notes, and snippets.

@jojees
Forked from gene1wood/role_arn_to_session.py
Created January 7, 2019 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jojees/57c32ea9db0c969e69e20342eb239ce2 to your computer and use it in GitHub Desktop.
Save jojees/57c32ea9db0c969e69e20342eb239ce2 to your computer and use it in GitHub Desktop.
Simple python function to assume an AWS IAM Role from a role ARN and return a boto3 session object
import boto3
def role_arn_to_session(**args):
"""
Usage :
session = role_arn_to_session(
RoleArn='arn:aws:iam::012345678901:role/example-role',
RoleSessionName='ExampleSessionName')
client = session.client('sqs')
"""
client = boto3.client('sts')
response = client.assume_role(**args)
return boto3.Session(
aws_access_key_id=response['Credentials']['AccessKeyId'],
aws_secret_access_key=response['Credentials']['SecretAccessKey'],
aws_session_token=response['Credentials']['SessionToken'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment