Not able to get_item from AWS dynamodb using python?

There are actual examples here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html

In your case, you need to do this:

response = table.get_item(TableName='Garbage_collector_table', Key={'topic': my_topic})

Assuming you only have partition key (aka hash key) in the table.

import boto3
dynamodb = boto3.resource('dynamodb',region_name='ap-southeast-2')
table = dynamodb.Table('my-table')
key = {}
key['key'] = 'my-key'
print(key)
response = table.get_item(Key=key)
print(response['Item'])

You can also query the database:

from boto3.dynamodb.conditions import Key

table = dynamodb.Table(table_name)
response = table.query(
    KeyConditionExpression=Key('topic').eq(my_topic)
)
items = response['Items']
if items:
    return items[0]
else:
    return []

Source: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html


You are mixing resource and client objects which have different methods. More info is here.

The correct syntax for a boto3 resource is:

response = table.get_item(Key={'topic': my_topic})

And the correct syntax for a boto3 client is:

client = boto3.client('dynamodb')
response = client.get_item(TableName='Garbage_collector_table', Key={'topic':{'S':str(my_topic)}})

Reference: Boto3 - DynamoDB