1

I have some problem with using lambda and dynamodb.

this is my python code:

import json
import boto3

def lambda_handler(event, context):
    client = boto3.client('dynamodb', region_name='ap-northeast-2')
    dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-2')

    response = client.put_item(
        TableName='tablename',
        Item = {
            'key1': 'value1',
            'key2': 'value2',
            'key3': 'value3',
            'key4': 'value4'
                }
        )

and this is the error log:

{
  "errorMessage": "Parameter validation failed:\nInvalid type for parameter Item.key1, value: value1, type: <class 'int'>, valid types: <class 'dict'>\nInvalid type for parameter Item.key2, value: value2, type: <class 'str'>, valid types: <class 'dict'>\nInvalid type for parameter Item.key3, value: value3, type: <class 'str'>, valid types: <class 'dict'>\nInvalid type for parameter Item.key4, value: value4, type: <class 'int'>, valid types: <class 'dict'>",
  "errorType": "ParamValidationError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 8, in lambda_handler\n    response = client.put_item(\n",
    "  File \"/var/runtime/botocore/client.py\", line 316, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 598, in _make_api_call\n    request_dict = self._convert_to_request_dict(\n",
    "  File \"/var/runtime/botocore/client.py\", line 646, in _convert_to_request_dict\n    request_dict = self._serializer.serialize_to_request(\n",
    "  File \"/var/runtime/botocore/validate.py\", line 297, in serialize_to_request\n    raise ParamValidationError(report=report.generate_report())\n"
  ]
}

I had searched about parameter validation failed error, and aws said i have to download or upgrade pip3 and AWS CLI so i did. but it still makes error.

1
  • Thanks a lot! I was busy doing sth else, Sorry for late check Commented Apr 23, 2020 at 7:19

2 Answers 2

5

The boto3 SDK offers two put_item() methods, one is client-level and one is resource-level. They are different, in particular the way that you provide the item attributes is different. You are using the client-level API but passing attributes as if you were using the resource-level API.

Here is how you use the resource level API:

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-2')
    table = dynamodb.table('tablename')

    response = table.put_item(
        Item = {
            'key1': 'value1',
            'key2': 'value2',
            'key3': 'value3',
            'key4': 'value4'
        }
    )
Sign up to request clarification or add additional context in comments.

Comments

2

Your syntax doesn't seem to be correct. See example from https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item

response = client.put_item(
    Item={
        'AlbumTitle': {
            'S': 'Somewhat Famous',
        },
        'Artist': {
            'S': 'No One You Know',
        },
        'SongTitle': {
            'S': 'Call Me Today',
        },
    },
    ReturnConsumedCapacity='TOTAL',
    TableName='Music',
)

Values are not strings, but rather instances of AttributeValue type

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.