如何在lambda函数中创建和插入JSON中的Amazon Web Services Dynamo DB


dynamo = boto3.resource('dynamodb').Table('Audit')
param = event.get('item')
res = dynamo.put_item(Item = param)

我想插入来自dynamodb

的API Gateway AWS中的事件的JSON

如果您的JSON我喜欢这个

{
  "item": {
    "item_name": "laddu",
    "short_title": "DAP",
    "description": "This is to create an doctor appointment",
    "cost": "300"
  }
}

和python dynamoDB插入在

以下
import boto3
import json
def lambda_handler(event, context):
    #getting the instance of dynamodb table
    dynamo = boto3.resource('dynamodb').Table(TABLE_NAME)
    #checking weather any record is exist or not
    if (len(dynamo.get_item(Key={'item_name':event.get('item')['item_name']})) > 1):
        result = "Failure"
        msg = "Item Already exist with same name"
    else :
        #inserting the item from the responce
        res = dynamo.put_item(Item = event.get('item'))
        if res["ResponseMetadata"]["HTTPStatusCode"] == 200 :
            result = "Success"
            msg = "Item created successfuly"
        else:
            result = "Failure"
            msg = "Failed to create Item"
    #for responce to the user
    data = {}
    data['result'] = result
    data['msg'] = msg
    #construct json object
    json_data = json.dumps(data)
    return json_data

这对我有用....

最新更新