foa771
foa771

Reputation: 27

What method should I use in Boto3 to create an entry in DynamoDB using AWS API gateway as a proxy service?

I am new to coding. I appreciate the help. I already create a DynamoDB table, API Gateway, child resource, child method(POST), integration type: AWS service proxy. I also created an IAM role to allow access to DynamoDB putitem. I tried to add something to the table with postman and it worked well. However, now I want to add an item to the table with boto3 and I'm finding it hard to identify the method that I should be using.

Upvotes: 0

Views: 108

Answers (2)

Jonathan Leon
Jonathan Leon

Reputation: 5648

dydbsr = boto3.resource('dynamodb')
dbtable = 'myTable'
table = dydbsr.Table(dbtable)

If you have a partition key only:

partion key is: mypartitionkey

json_dictionary = {"mypartitionkey": "myvalue"}
table.put_item(Item=json_dictionary)

If you have a partition key and sort key. If sort key is defined it MUST be included

partion key is: mypartitionkey
sort key is: mysortkey

json_dictionary = {"mypartitionkey": "myvalue", "mysortkey": "myothervalue"}
table.put_item(Item=json_dictionary)

After adding partition and sort keys, then your attributes are just key:value pairs in the same json dictionary

Upvotes: 0

dossani
dossani

Reputation: 1948

Refer to the AWS documentation here for examples to create, read, update, and delete an item with Python.

Upvotes: 0

Related Questions