unable to fetch record based on a specific key in dynamo db and python

Question:

I have a child class where I am trying to fetch the dynamo db data based on a specific key, I tried multiple variations of passing a value to the key but somehow I am getting an error on the response.

class AdGroupDetailsRepository(Repository):
    def __init__(self, client, table_name):
        super().__init__(client)
        self.table_name = table_name

    def _exec_find_by_id(self, id: str):
        logger = get_provisioner_logger()
        logger.info("Table name is %s" % self.table_name)
        dynamo_table = self.client.Table(self.table_name)
        logger.info("Connected to the dynamo table...")
        logger.info("id is %s" % id)
        item = dynamo_table.get_item(Key={'ad_group': "bigdata"})
        logger.info("fetched account: ", item['Items'])
        return AdGroupDetails(id, item['account'], item['role'])

Error:

enter image description here

I am just trying to fetch the results which is nothing but the dictionary in dynamo’s case and out of that I need to fetch the specific column values.

Dynamo table schema: ad_group, account, role

I am using the dynamo resource to connect to the dynamo table. Also, I am running it via lambda functions for the APIs.

Asked By: whatsinthename

||

Answers:

A get_item does not have a key of Items:

logger.info("fetched account: ", item['Items'])

I believe your code is failing on this line, causing the exception to be returned.

    item = dynamo_table.get_item(Key={'ad_group': "bigdata"})['Item']
    logger.info("fetched account: " item)
    return AdGroupDetails(id, item['account'], item['role']) 

Try CLI:

aws dynamodb get-item 
--table-name <Your Table Name> 
--key '{"ad_group":{"S":"bigdata"}}' 
--region <Table Region Here>
Answered By: Lee Hannigan