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

前端 未结 3 992
闹比i
闹比i 2021-02-01 01:50

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\": {\"f         


        
3条回答
  •  时光取名叫无心
    2021-02-01 02:19

    full example:

    import boto3
    
    def lambda_handler(event, context):
    
        client = boto3.client('dynamodb')
    
        for record in event['Records']:
            # your logic here...
            try:
                client.update_item(TableName='dynamo_table_name', Key={'hash_key':{'N':'value'}}, AttributeUpdates={"some_key":{"Action":"PUT","Value":{"N":'value'}}}) 
            except Exception, e:
                print (e)
    

    please note that you need to decide when to use 'update_item' or 'put_item'.

    with 'update_item' you ensure to have only one record with the same hash/range. if the record exists it can update this record, else it will create it

    http://boto3.readthedocs.org/en/latest/reference/services/dynamodb.html#DynamoDB.Client.update_item

提交回复
热议问题