Skip to content

Instantly share code, notes, and snippets.

@JPalmerGithub
Forked from coingraham/assume.py
Created December 15, 2021 13:28
Show Gist options
  • Save JPalmerGithub/8e34890b34350ad856c4a9159b61dd6a to your computer and use it in GitHub Desktop.
Save JPalmerGithub/8e34890b34350ad856c4a9159b61dd6a to your computer and use it in GitHub Desktop.

Revisions

  1. @coingraham coingraham created this gist Oct 24, 2019.
    27 changes: 27 additions & 0 deletions assume.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    import boto3
    import sys

    assume_role = sys.argv[1]

    # create an STS client object that represents a live connection to the
    # STS service
    sts_client = boto3.client('sts')

    # Call the assume_role method of the STSConnection object and pass the role
    # ARN and a role session name.
    assumed_role_object=sts_client.assume_role(
    RoleArn='assume_role'
    )

    # From the response that contains the assumed role, get the temporary
    # credentials that can be used to make subsequent API calls
    credentials=assumed_role_object['Credentials']

    # Use the temporary credentials that AssumeRole returns to make a
    # connection to Amazon S3
    glue_client=boto3.client(
    'glue',
    aws_access_key_id=credentials['AccessKeyId'],
    aws_secret_access_key=credentials['SecretAccessKey'],
    aws_session_token=credentials['SessionToken'],
    )