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
2
  • 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: boto3.readthedocs.io/en/latest/reference/services/… Commented May 29, 2018 at 17:48
  • Maybe I'm confused, but I am still having trouble figuring out what parameter to use. Commented May 29, 2018 at 18:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.