0
votes

I'm new to coding and trying to create a modifiable database using boto3, python 2.7, dynamodb and aws lambda. It's unclear to me how to characterize the event parameters for some expected JSON (possibly dict) input. Do you need to create some sort of Key in another area. I'm able to hard code some data and pass the information to dynamodb, but for a generic type of update_item/put_item, have not been successful.

import json
import boto3

# Get service resource
dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('MyTable')

def lambda_handler(event,context):
   
    response = table.put_item(
        
        Item={
             json.dumps(event)
        }
    )

    return{
        'statusCode': 200,
        'body': {}
        }
}

The error is:

ParamValidationError: Parameter validation failed:
Invalid type for parameter Item, value: set(['{"key3": "value3", "key2": "value2", "key1": "value1"}']), type: <type 'set'>, valid types: <type 'dict'>
1
These are validation errors so basically something is mismatching and also the syntax which you have used is not correct. So can you input this information also: did you create the dynamodb? If yes then what is the table name and primary key (partition key/ sort key) and it's type? - Sai Sreenivas
table = dynamodb.create_table( TableName = 'Mikeys Shit', KeySchema = [ { 'AttributeName': 'supplier_name', 'KeyType': 'HASH' }, { 'AttributeName': 'supplier_id', 'KeyType': 'RANGE' } ], AttributeDefinition = [ { 'AttributeName': 'supplier_name', 'AttributeType': 'S' }, { 'AttributeName': 'supplier_id' 'AttributeType': 'N' }, ], ) - nyquilholic
Sorry about the format, I've never really used stack. There is also provisioned throughput parameters in the original, but it was too many characters. - nyquilholic

1 Answers

1
votes

The code retrieving the contents of event, and sending it straight through to DynamoDB:

json.dumps(event)

It appears that you have clicked Test in the AWS Lambda console. This uses a test event that is configured through the "Configure test event" pull-down. The default test event is:

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

This default test does not match the schema of your DynamoDB table, so it is generating an error.

You should either:

  • Update the test event to contain data in the correct format, and test it again, or
  • Call the Lambda function from your intended code, that is hopefully passing along the correct data (That is, do whatever you were going to do "for real" rather than using "Test")

If you simply wish to store "whatever data has been provided", you will at minimum need to have supplier_name and supplier_id as part of the incoming JSON, since they are the primary keys in your table.