2

I am using python with the AWS S3, lambda and DynamoDB. I have my lambda function set up as a trigger. When I drop a .json file into my S3 bucket, it will activate.

When my function activates, it errors once it reaches my put_item function call that is supposed to be storing the json object in my dynamodb table.

The error text:

[ERROR] ClientError: An error occurred (ValidationException)
when calling the PutItem operation: One or more parameter values
were invalid: Missing the key test in the item

I have tried to change the arguments within the table.put_item(TableName='testTable', Item = jsonDict), but the documents I was following before only passed one argument to this function.

Any advice or assistance would be greatly appreciated.

My Code states:

import boto3
import json

s3_client = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')

def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    json_file_name = event['Records'][0]['s3']['object']['key']
    json_object = s3_client.get_object(Bucket=bucket,Key=json_file_name)
    jsonFileReader = json_object['Body'].read()
    jsonDict = json.loads(jsonFileReader)
    table = dynamodb.Table('test')
    table.put_item(Item = jsonDict)

    return 'Hello from lambda'

CLOUD WATCH LOGS:

[ERROR] ClientError: An error occurred (ValidationException) when
calling the PutItem operation: One or more parameter values were
invalid: Missing the key test in the item
Traceback (most recent call last):

  File "/var/task/lambda_function.py", line 14, in lambda_handler
    response = table.put_item(Item = jsonDict)
  File "/var/runtime/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "/var/runtime/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/var/runtime/botocore/client.py", line 320, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 623, in _make_api_call
    raise error_class(parsed_response, operation_name)

EDITS:

I created my table with the 'test' primary key and the 'test' table name, leaving all of the other settings to default within the AWS dynamo table creation GUI.

json file contents:

{ "test": { "name": "Cody", "age": 27, "car": true } }

1
  • The error is saying that the JSON you are putting into DynamoDB does not have a key of test. Please edit your question to show the design of the DynamoDB table (does it have a hash key of test?) and a sample of the data you are loading into jsonDict. Commented Jan 22, 2019 at 3:57

1 Answer 1

1

You're trying to use {"name": "Cody", "age": 27, "car": true} as the value of the primary key. In DynamoDB, a primary (partition) key can only be of type string, binary or number.

As an example, using {"test": "Cody", "age": 27, "car": True} as the Item argument to put_item would work.

Or if you would change the partition key name in the table to name, calling table.put_item(Item=jsonDict['test']) would also do the trick.

Sign up to request clarification or add additional context in comments.

3 Comments

I reformatted my JSON to include a primary key of test just like you said. This worked perfectly. Thank you Milan.
Yay, I'm glad it helped. Feel free to upvote and accept the answer so that others can recognize it as helpful too.
I need two more reputation and I will then be able to begin upvoting. Once I get it, I will upvote your answer immediately. I promise.

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.