Home > Enterprise >  What counts as an object vs a string in Typescript?
What counts as an object vs a string in Typescript?

Time:01-28

I am creating a Infrastructure-as-Code for a Step Functions Machine. One of these states is of type 'Task' which performs a DynamoUpdateItem on a DynamoDB table.

The code looks as following:

    const updateDDB = new tasks.DynamoUpdateItem(this, 'Update item into DynamoDB', {
      key: { ['Task'] : tasks.DynamoAttributeValue.fromString('Processing') },
      table: table,
      updateExpression: 'SET LastRun = :label',
      expressionAttributeValues: {
        ':label.$': DynamoAttributeValue.fromString(JsonPath.stringAt('$$.Execution.StartTime')), 
      },
      resultPath: JsonPath.DISCARD,
    });

However, I keep getting an error saying the schema validation failed, and that

"The value for the field ':label.$' must be a STRING that contains a JSONPath but was an OBJECT at /States/Update item into DynamoDB/Parameters'"

How the heck is it not a string?!

I have tried writing it as [':label.$'], or even writing a .toString() function at the end of the JsonPath method

      expressionAttributeValues: {
        ':label.$': (DynamoAttributeValue.fromString(JsonPath.stringAt('$$.Execution.StartTime').toString())), 

      },

But nothing seems to work. I keep getting the same issue claiming that it's not a string.

Using something like JSON.stringify() doesn't work either because expressionAttributeValues takes a key and matches it with a DynamoAttributeValue.

CodePudding user response:

TL;DR Drop the .$ suffix from ':label.$': DynamoAttributeValue(...) in the expression attribute value definition. The key should be simply ':label'.


As @TobiasS says in the comments, the problem is with your State Machine task syntax. The .$ suffix on :label.$ tells AWS to expect a JSONPath string representing a path to a value. But a string is not valid State Machine syntax for this field. A key-value pair is required. The CDK docs have an example with the correct syntax.

❌ What your CDK code synthesizes to:

{
    "ExpressionAttributeValues": {
      ":label.$": { "S.$": "$$.Execution.StartTime" }
    },
}

✅ AWS expects the synthesized State Machine definition to be:

{
    "ExpressionAttributeValues": {
      ":label": { "S.$": "$$.Execution.StartTime" }
    },
}
  • Related