Home > Software design >  In AWS Step Functions how do I pass an integer input to DynamoDB
In AWS Step Functions how do I pass an integer input to DynamoDB

Time:04-13

I have an AWS Step function, and I need to insert items into DynamoDB. I'm passing the following input to the Step Function execution:

{
   "uuid": "dd10a857-3711-451e-91ee-d0b3ab621b2e",
   "item_id": "0D98C2F77",
   "item_count": 3,
   "order_id": "IO-98255AX"
}

I have a DynamoDB PutItem Step, set up like so: All of the attributes are strings apart from item_count

Since item_count is a numeric value, I specified "N.$": "$.item_count" - I specified N at the beginning because that maps to the number type in DynamoDB. Since all of the other fields are strings, I started their keys with S.

I then tried to test the PutItem step with the above payload, and I got the following error:

{
  "error": "States.Runtime",
  "cause": "An error occurred while executing the state 'DynamoDB PutItem' (entered at the event id #2). The Parameters '{\"TableName\":\"test_item_table\",\"Item\":{\"uuid\":{\"S\":\"dd10a857-3711-451e-91ee-d0b3ab621b2e\"},\"item_id\":{\"S\":\"0D98C2F77\"},\"item_count\":{\"N\":3},\"order_id\":{\"S\":\"IO-98255AX\"}}}' could not be used to start the Task: [The value for the field 'N' must be a STRING]"
}

I looked up the The value for the field 'N' must be a STRING error, and I found two relevant results:

  1. A post on AWS where the OP decided to just change the format of the data that gets passed to the Dynamo step
  2. A post on Github, where the OP was using CDK - and he ends up using a numberFromString() function that's available in CDK

In my case, I have an integer value, and I'd prefer to pass in into Dynamo as an integer - but based on the first link, it seems that Step Functions can only pass string values to DynamoDB. This means that my only option is to convert the integer value to a string, but I'm not sure how to do this. I know that Step Functions have intrinsic functions, but I don't think that this is applicable to JSON paths.

What's the best way to handle storing this numeric data to DynamoDB?

CodePudding user response:

TL;DR "item_count": {"N.$": "States.JsonToString($.item_count)"}

it seems that Step Functions can only pass string values to DynamoDB

Yes, although technically it's a constraint of the DynamoDB API. DynamoDB accepts numbers as strings to maximalize compatability, but the underlying data type remains numeric.

This means that my only option is to convert the integer value to a string, but I'm not sure how to do this.

The JsonToString intrinsic function can stringify a number value from the State Machine execution input.

  • Related