velocityaws-step-functions

How to obtain the Alias of an invoked StepFunction (from within the step function)?


I have an aliased Step Function (so happy that that support was recently added) where the step function logic needs to know the alias, eg Prod or Dev. I can invoke the step function via an alias but the context object shows the arn without the alias. Specifically I invoke an arn of "arn:aws:states:us-west-2:************:stateMachine:UpdateLocation:Development" and the context object contains;

{
      "Execution": {
        "Id": "arn:aws:states:us-west-2:**********:express:UpdateLocation:57154f70-22c4-440a-9b3a-cf8445bea4ff:33e2e32e-db05-48f1-abe5-ff2d7b6a5d44",
        "Input": {
          "here": "there"
        },
        "Name": "57154f70-22c4-440a-9b3a-cf8445bea4ff",
        "RoleArn": "arn:aws:iam::**********:role/stepfunction-function-role",
        "StartTime": "2023-08-10T18:19:36.028Z"
      },
      "StateMachine": {
        "Id": "arn:aws:states:us-west-2:**********:stateMachine:UpdateLocation",
        "Name": "UpdateLocation"
      },
      "State": {
        "Name": "Pass (1)",
        "EnteredTime": "2023-08-10T18:19:36.033Z"
      }
}

Ultimately I'm invoking a Step Function from API Gateway (via a POST) and I'm using the following VTL to invoke the step function.

    "input": "$util.escapeJavaScript($input.json('$'))",
    "stateMachineArn": "arn:aws:states:us-west-2:**********:stateMachine:UpdateLocation:Development"
}

I was thinking I could add the API Gateway stage to the input payload but I can't that to work. I'm trying the following

    #set ($newInput=$input.json('$'))
    #set($newInput.Environment='Development')
    "input": "$util.escapeJavaScript($newInput)",
    "stateMachineArn": "arn:aws:states:us-west-2:*:stateMachine:UpdateLocation:Development"
}

But I have something wrong here because when I test it via API Gateway it seems to ignore my attempts to update the input option. It logs;

Thu Aug 10 18:28:47 UTC 2023 : Endpoint request body after transformations: {
        "input": "{\"here\":\"there\"}",
    "stateMachineArn": "arn:aws:states:us-west-2:**********:stateMachine:UpdateLocation:Development"
}

Does anyone have any suggestions on how to get the alias or on how to add a new key/value to the input object?

Thanks!


Solution

  • The state machine version and alias name aren't currently available in the context object. So, your only options that I can think of are:

    1. Call DescribeExecution within the execution on itself to get the version and/or alias ARN
    2. Pass the version and/or alias ARN to the execution as input