amazon-dynamodbjsonpathaws-step-functionsaws-lex

How to insert empty string in DynamoDB using the output of a Lambda in Step Functions?


I'm trying to save the output of a Lambda which calls Lex to DynamoDB using Step Functions.

The intentName in a Lex response is sometimes null (unknown). The problem is that in the state (task) that saves the response to DynamoDB, because of this empty string I get an error from DynamoDB.

Is there any workaround, maybe using JsonPath or the state machine diagram of the Step Function, in order to insert null or maybe no insert that specific property in DynamoDB?

Here is the JSON for the state machine:

{
  "StartAt": "ProcessLex",
  "States": {
    "ProcessLex": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:<Region>:<Account Id>:function:getIntent",
      "ResultPath": "$.lexResult",
      "Next": "ChoiceIfIntent"
    },
    "SaveToDynamo": {
      "Type": "Task",
      "Resource": "arn:aws:states:::dynamodb:putItem",
      "Parameters": {
        "TableName": "MyTable",
        "Item": {
          "dateTime": {
            "S.$": "$.dateTime"
          },
          "intentName": {
            "S.$": "$.lexResult.intentName"
          },
          "analysis": {
            "M.$": "$.lexResult.sentimentResponse"
          }
        }
      },
      "End": true
    },
    "Comprehend": {
      "Comment": "To be implemented later",
      "Type": "Pass",
      "End": true
    },
    "ChoiceIfIntent": {
      "Type": "Choice",
      "Choices": [
        {

            "Variable": "$.lexResult.intentName",
            "StringGreaterThanEquals": "",

          "Next": "SaveToDynamo"
        }        
      ],
        "Default": "Comprehend"
    }
  }
}

Solution

  • The problem is not the null value, the problem is that in DynamoDB with the PutItem Api you cannot insert empty strings.

    I know this is frustrating but the quickest solution is to replace "" with NULL.

    The solution that I prefer is to set the convertEmptyValue to true in your DynamoDb client settings.

    const dynamodb = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true })
    

    UPDATE

    Since yesterday, DynamoDB supports empty values for string! Take a look here.