0

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},"

2
  • Do you have any more of the error info returned? Can you check the logs? There should be some description of the exact error encountered that would help debug this. Commented Jan 27, 2016 at 11:35
  • 1
    Actually after you sent this I realized that the logs go much deeper then I realized that Lambda and Dynamo only except strings So I changed my code to add items as strings Commented Jan 27, 2016 at 11:37

2 Answers 2

1

you must update values as string:

client.put_item(
      TableName = tableminfo, Item = {
      'marker':{'N' : str(marker)},
      'latitude':{'N' : str(latitude)},
      'longitude':{'N' : str(longitude)},
        }
    )
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.