Search code examples
amazon-web-servicesamazon-dynamodbaws-step-functionsdynamodb-queries

AWS Stepfunction, ValidationException


i got the error "The provided key element does not match the schema", while getting data from AWS dynamoDB using stepfunction.

stepfunction Defination

{
  "Comment": "This is your state machine",
  "StartAt": "Choice",
  "States": {
    "Choice": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.data.Type",
          "StringEquals": "GET",
          "Next": "DynamoDB GetItem"
        },
        {
          "Variable": "$.data.Type",
          "StringEquals": "PUT",
          "Next": "DynamoDB PutItem"
        }
      ]
    },
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::dynamodb:getItem",
      "Parameters": {
        "TableName": "KeshavDev",
        "Key": {
          "Email": {
            "S": "$.Email"
          }
        }
      },
      "End": true
    },
    "DynamoDB PutItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::dynamodb:putItem",
      "Parameters": {
        "TableName": "KeshavDev",
        "Item": {
          "City": {
            "S.$": "$.City"
          },
          "Email": {
            "S.$": "$.Email"
          },
          "Address": {
            "S.$": "$.Address"
          }
        }
      },
      "InputPath": "$.data",
      "End": true
    }
  }
}

Input

{
  "data": {
    "Type": "GET",
     "Email": "demo@gmail.com"
  }
}

Error

{ "resourceType": "dynamodb", "resource": "getItem", "error": "DynamoDB.AmazonDynamoDBException", "cause": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: a78c3d7a-ca3f-4483-b986-1735201d4ef2; Proxy: null)" }


Solution

  • I see some potential issues with the getItem task when compared to AWS documentation.

    • I think the Key field needs to be S.$ similar to what you have in your putItem task.
    • There is no ResultPath attribute to tell the state machine where to put the results.
    • Your path may not be correct, try $.data.Email
        "DynamoDB GetItem": {
          "Type": "Task",
          "Resource": "arn:aws:states:::dynamodb:getItem",
          "Parameters": {
            "TableName": "KeshavDev",
            "Key": {
              "Email": {
                "S.$": "$.data.Email"
              }
            }
          },
          "ResultPath": "$.DynamoDB",
          "End": true
        },
    

    To be honest, I'm not sure if one of all of these are contributing to the validation error those are some things to experiment with.

    On another note, there are some open source validators for Amazon State Language but for this case, they were not very helpful and said that your code was valid.