Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One or more parameter values were invalid: Type mismatch for key xyz expected: S actual: M

My AWS Lambda calls a DynamoDB based on this answer: https://stackoverflow.com/a/33649402/495455

I get an error:

One or more parameter values were invalid: Type mismatch for key UserName expected: S actual: M

This is python code yet the error occurs with JavaScript & etc as well:

import json
import boto3
client = boto3.resource('dynamodb')
table = client.Table("DS-Users")

def lambda_handler(event, context):

    UserName = event['UserName']
    Email = event['Email']
    Score = event['Score']
    Mobile = event['Mobile']
    CountryId = event['CountryId']
    Level = event['Level']
    Magic = event['Magic']

    table.put_item(Item={'UserName':{'S':UserName},'Email':{'S':Email},'Score':{'N':Score},'Level':{'N':Level},'Mobile':{'S':Mobile},'Magic':{'S':Magic}})

    return {
        'statusCode': 200,
        'body': json.dumps(event)
    }

Researching the error results in a few answers that don't resolve the above error:

  1. https://github.com/awslabs/dynamodb-document-js-sdk/issues/17

  2. https://forums.aws.amazon.com/thread.jspa?threadID=248424

I'm stumped because UserName is a string, I enforce it in the Integration Request Mapping Template:

#set($inputRoot = $input.path('$'))
{
  "UserName" : "$inputRoot.UserName",

Any idea's? Why is it treating the string as a model data type?

like image 737
Jeremy Thompson Avatar asked Jul 22 '19 04:07

Jeremy Thompson


1 Answers

First note the version of the AWS SDK V1 vs V2!

Also note the syntax for the client vs resource API of boto3. {'UserName':{'S':UserName} is the syntax used for the boto3 client and this solution is for the boto3 resource variant.

boto3 client put_item reference – https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item

boto3 resource put_item reference - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.put_item

client syntax

{'UserName':{'S':UserName},

resource syntax

{'UserName':UserName,

For my specific case, this fixed it up:

table.put_item(Item={'UserName':UserName,'Email':Email,'Score':Score,'Level':Level,'Mobile':Mobile,'Magic':Magic})
like image 183
Jeremy Thompson Avatar answered Sep 22 '22 12:09

Jeremy Thompson