DEV Community

Robert Slootjes
Robert Slootjes

Posted on

Creating a UUID in Step Functions

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!"
}
Enter fullscreen mode Exit fullscreen mode

Desired Output:

{
  "Comment": "Step Functions is awesome!",
  "UUID": "42e6272f-8f63-4154-9719-2845e0952b9f"
}
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)