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

Jeremy Thompson picture Jeremy Thompson · Jul 22, 2019 · Viewed 7.7k times · Source

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

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)
    }

I get an error:

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

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

The error is saying that DynamoDB expected a string for the uuid, but it is getting something else. According to this from the dynamodb docs, S is dynamodb's string, and, as raymolin said, M is a map (like an object or model). Thus, the uuid being sent must not be a string, but a model/object/map. Once that's fixed, this should work fine.

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

The issue was with the permissions on the IAM role I had assigned to the lambda for execution.

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?

Answer

Jeremy Thompson picture Jeremy Thompson · Jul 22, 2019

You might be using the old syntax for AWS SDK v1:

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

Solution

The new syntax is:

{'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})