dmn0972
dmn0972

Reputation: 373

DynamoDB scan not returning desired output

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

Upvotes: 2

Views: 1360

Answers (1)

jarmod
jarmod

Reputation: 78573

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.

Upvotes: 4

Related Questions