Search code examples
amazon-web-servicesaws-step-functions

How do I format the headers field in step function API invoke


I am trying to execute a AWS API via step function. I need to pass the authorizationToken value in the header field.

{
  "ApiEndpoint": "xyz.execute-api.ap-southeast-2.amazonaws.com",
  "Method": "POST",
  "Headers": {
    "authorizationToken.$": [
      "$.InputToken"
    ]
  },
  "Stage": "test",
  "Path": "/",
  "RequestBody": {
    "productType": [],
    "xxx.$": "$.xxx",
    "yyy.$": "$.yyy",
    "zzz.$": "$.zzz"
  },
  "AuthType": "IAM_ROLE"
}

I am getting the following error -

The value for the field 'authorizationToken.$' must be a STRING that contains a JSONPath but was an ARRAY (at /States/GetDeclarations/Parameters)

This is the default syntax for the API invoke -

"Headers": {
    "Header1": [
      "HeaderValue1"
    ],
    "Header2": [
      "HeaderValue2",
      "HeaderValue3"
    ]
  }

When I modify this to

"Headers": {
    "authorizationToken": [
      "1234"
    ],
    "Header2": [
      "HeaderValue2",
      "HeaderValue3"
    ]
  }

It works fine.

I need to make the value of authorizationToken a variable that takes its value from the input.

My Input data looks like this

{
  "xxx": "123",
  "yyy": "123",
  "zzz": "123",
  "InputToken": "123",
  "aaa": "123"
}

Solution

  • You need to use the States.Array Intrinsic Function as I've shown below. This allows you to inject an array into a node in your Parameters block. In this case, you just want a single item in the array, but you can include multiple items as well (e.g., States.Array($.item1,$.item2,$.item3)).

    Check out the other Intrinsic Functions as well, as they are handy for overcoming challenges like this.

    {
      "ApiEndpoint": "ccqk9ijm0h.execute-api.ap-southeast-2.amazonaws.com",
      "Method": "POST",
      "Headers": {
        "authorizationToken.$": "States.Array($.InputToken)"
      },
      "Stage": "test",
      "Path": "/",
      "RequestBody": {
        "productType": [],
        "xxx.$": "$.xxx",
        "yyy.$": "$.yyy",
        "zzz.$": "$.zzz"
      },
      "AuthType": "IAM_ROLE"
    }