AWS: Boto3: AssumeRole example which includes role usage

前端 未结 7 1402
梦毁少年i
梦毁少年i 2020-12-14 18:28

I\'m trying to use the AssumeRole in such a way that i\'m traversing multiple accounts and retrieving assets for those accounts. I\'ve made it to this point:



        
7条回答
  •  隐瞒了意图╮
    2020-12-14 19:00

    import json
    import boto3
    
    
    roleARN = 'arn:aws:iam::account-of-role-to-assume:role/name-of-role'
    client = boto3.client('sts')
    response = client.assume_role(RoleArn=roleARN, 
                                  RoleSessionName='RoleSessionName', 
                                  DurationSeconds=900)
    
    dynamodb_client = boto3.client('dynamodb', region_name='us-east-1',
                        aws_access_key_id=response['Credentials']['AccessKeyId'],
                        aws_secret_access_key=response['Credentials']['SecretAccessKey'],
                        aws_session_token = response['Credentials']['SessionToken'])
    
    response = dynamodb_client.get_item(
    Key={
        'key1': {
            'S': '1',
        },
        'key2': {
            'S': '2',
        },
    },
    TableName='TestTable')
    print(response)
    

提交回复
热议问题