I am trying to create a new Item in my table and every time I run the following code:
from __future__ import print_function
from decimal import *
import boto3
import json
def my_handler(event, context):
marker = event['m']
latitude = Decimal(event['lat'])
longitude = Decimal(event['lon'])
tableminfo = 'minfo'
client = boto3.client('dynamodb')
client.put_item(
TableName = tableminfo, Item = {
'marker':{'N' : marker},
'latitude':{'N' : latitude},
'longitude':{'N' : longitude},
}
)
success = "Success"
return {'success' : success}
with the following test parameters in Lambda
{
"m": 1,
"lat": 52.489505,
"lon": 13.389687
}
I receive an error on the following lines: 17, "my_handler", "'longitude':{'N' : longitude},"
you must update values as string:
client.put_item(
TableName = tableminfo, Item = {
'marker':{'N' : str(marker)},
'latitude':{'N' : str(latitude)},
'longitude':{'N' : str(longitude)},
}
)
You can also use the higher-level resource interface that boto3 provides for DynamoDB. It handles a lot of the low-level details for you. Here's a version of your code using the resource layer.
Assuming your event looks like this:
{
"m": 1,
"lat": 52.489505,
"lon": 13.389687
}
This code would persist the data to DynamoDB
import boto3
client = boto3.resource('dynamodb')
table = client.Table('minfo')
def my_handler(event, context):
item = {
'marker': event['m'],
'latitude': event['lat'],
'longitude': event['lon']}
table.put_item(Item=item)
return {'success' : "Success"}
Creating the client and Table at the module level is more efficient because then you won't pay the price of creating them every time your Lambda function is called.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.