Skip to content
Advertisement

how to put an Item in aws DynamoDb using aws Lambda with python

Using python in AWS Lambda, how do I put/get an item from a DynamoDB table?

In Node.js this would be something like:

dynamodb.getItem({
    "Key": {"fruitName" : 'banana'},
    "TableName": "fruitSalad"
}, function(err, data) {
    if (err) {
        context.fail('Incorrect username or password');
    } else {
        context.succeed('yay it works');
    }
});

All I need is the python equivalent.

Advertisement

Answer

Using Boto3 (Latest AWS SDK for python)

You import it with

import boto3

Then call the client via

dynamodb = boto3.client('dynamodb')

Get item example

dynamodb.get_item(TableName='fruitSalad', Key={'fruitName':{'S':'Banana'}})

Put item example

dynamodb.put_item(TableName='fruitSalad', Item={'fruitName':{'S':'Banana'},'key2':{'N':'value2'}})

‘S’ indicates a String value, ‘N’ is a numeric value

For other data types refer http://boto3.readthedocs.org/en/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item

Advertisement