skip to Main Content

In AWS, I have a Step Function Workflow:

                    "GetAccountAlias": {
                      "Type": "Task",
                      "Resource": "arn:aws:states:::aws-sdk:iam:listAccountAliases",
                      "Parameters": {
                        "MaxItems": 1
                      },
                      "ResultSelector": {
                        "name.$": "States.ArrayGetItem($.AccountAliases, 0)"
                      },
                      "ResultPath": "$.name",
                      "Next": "Update"
                    },

It gets an input like this:

{
  "my_object_key": "123"
}

Next I retrieve the alias. Now the output is:

{
  "my_object_key": "123",
  "name": {
    "name": "<account-alias>"
  }
}

When I update "ResultPath": "$.name" to "ResultPath": "$" then the output is:

  {
    "name": "<account-alias>"
  }

What I want is:

{
  "my_object_key": "123",
  "name": "<account-alias>"
}

How can I achieve this? (Preferably without using an additional task/stage)

2

Answers


  1. Unfortunately, you cannot accomplish this within the same state as the input is not available in the ResultSelector. You need to do this in a subsequent Pass state (see example below).

      "Format Output": {
          "Type": "Pass",
          "Parameters": {
            "my_object_key.$": "$.my_object_key",
            "name.$": "$.name.name"
          },
          "Next": "My Next State"
        }
    
    Login or Signup to reply.
  2. Yes, if my_object_key is in the *execution* input. If not, you’ll need a Pass state, as @JustinCallison says.

    ResultSelector can reference (1) the lambda task output (as $) and (2) the step function’s execution input (as $$.Execution.Input). If my_object_key was passed into the step function, you are in luck:

    "ResultSelector": {
      "merged.$": "States.JsonMerge(States.StringToJson(States.Format('\{"name":"{}"\}', States.ArrayGetItem($.AccountAliases, 0))), States.StringToJson(States.Format('\{"my_object_key":"{}"\}', $$.Execution.Input.my_object_key)), false)"
    },
    "OutputPath": "$.merged"
    

    What’s happening? We’re merging two JSON objects with the States.JsonMerge intrinsic function. Easy enough, but first we must coerce name and my_object_key from values to JSON key-value pairs. We manually create a stringified object with States.Format and then parse it with States.StringToJson.

    Here’s ResultSelector formatted to make it easier to see what’s going on:

    States.JsonMerge(
        States.StringToJson(
            States.Format('\{"name":"{}"\}', States.ArrayGetItem($.AccountAliases, 0))
        ),
        States.StringToJson(
            States.Format('\{"my_object_key":"{}"\}', $$.Execution.Input.my_object_key)
        ),
        false
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search