2

I have a simple python script that is scanning a DynamoDB table. The table holds ARNs for all the accounts I own. There is one primary key "ARNs" of data type string. When I scan the table, I would like to only get the ARN string returned. I am having trouble finding anything in the boto3 documentation that can accomplish this. Below is my code, the returned output, and the desired output.

CODE:

import boto3

dynamo = boto3.client('dynamodb')

# Scans Dynamo for all account role ARNs 
def get_arns():

    response = dynamo.scan(TableName='AllAccountARNs')

    print(response)

get_arns()

OUTPUT:

{'ARNs': {'S': 'arn:aws:iam::xxxxxxx:role/custom_role'}},
{'ARNs': {'S': 'arn:aws:iam::yyyyyyy:role/custom_role'}},
{'ARNs': {'S': 'arn:aws:iam::zzzzzzz:role/custom_role'}}

DESIRED OUPUT:

arn:aws:iam::xxxxxxx:role/custom_role
arn:aws:iam::yyyyyyy:role/custom_role
arn:aws:iam::zzzzzzz:role/custom_role
dmn0972
  • 363
  • 2
  • 8
  • 21
  • 1
    You need to parse the response to get what you are expecting. If you need to remove the 'S', use the dynamodb table resource: http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#table – krishna_mee2004 May 29 '18 at 17:48
  • Maybe I'm confused, but I am still having trouble figuring out what parameter to use. – dmn0972 May 29 '18 at 18:19

1 Answers1

4

Here's an example of how to do this with a boto3 DynamoDB Client:

import boto3

ddb = boto3.client('dynamodb')

rsp = ddb.scan(TableName='AllAccountARNs')

for item in rsp['Items']:
  print(item['ARNs']['S'])

Here's the same thing, but using a boto3 DynamoDB Table Resource:

import boto3

dynamodb = boto3.resource('dynamodb')
tbl = dynamodb.Table('AllAccountARNs')

rsp = tbl.scan()

for item in rsp['Items']:
  print(item['ARNs'])

Note that these examples do not handle large result sets. If LastEvaluatedKey is present in the response, you will need to paginate the result set. See the boto3 documentation.

For more information on Client vs. Resource, see here.

jarmod
  • 71,565
  • 16
  • 115
  • 122