Creating a UUID in Step Functions
Robert Slootjes
Posted on November 8, 2022
In a recent project I wanted to generate a UUID in a StepFunction. Using the new intrinsic functions this should be possible without using a Lambda! While this is pretty awesome I ran into an issue. I wanted to generate a UUID and set it in the root of the state while keeping the existing state.
Example Input:
{
"Comment": "Step Functions is awesome!"
}
Desired Output:
{
"Comment": "Step Functions is awesome!",
"UUID": "42e6272f-8f63-4154-9719-2845e0952b9f"
}
It is not possible to return a scalar value from a variable or function in a Pass type. I can trace this back to this sentence in the documentation:
The ResultSelector field lets you create a collection of key value pairs, where the values are static or selected from the state's result.
It clearly states ResultSelector can only create key value pairs, not a scalar (based on variable input).
During my quest I decided to ask around on the AWS NL User Group Slack channel. Pretty quickly friendly community member Tomoharu Ito started experimenting and came with a great solution. Unfortunately it requires 2 steps but it gets the job done. Funnily enough, this also uses one of the new intrinsic functions. After a small bit of optimizing on my end, this is the end result:
StartAt: GenerateUuid
States:
GenerateUuid:
Type: Pass
Next: Merge
Parameters:
Original.$: "$"
New:
UUID.$: States.UUID()
Merge:
Type: Pass
End: true
Parameters:
Merged.$: States.JsonMerge($.Original, $.New, false)
OutputPath: "$.Merged"
Even though 2 steps are required, I prefer this over a single Lambda function since we don't have any invocation cost and also it saves us the maintenance of this Lambda function.
Hopefully this is helpful to others running into the same issue.
Posted on November 8, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.