1
votes

I would like to know if it is possible to invoke a lambda function with an alias from a Step Function?

I currently have a few Lambda functions setup to work with alias. (one for each environment, such as DEV, UAT etc). This is convenient as it means I don't need to deploy separate Lambda functions for each environment.

I would like to achieve the same thing with Step Functions and start execution on the state machine with a value that represents the executing environment (Alias).

I would then like to use that value within the State Machine Language to pass that into the arn of the Lambda function.

Something like this.

"Comment": "DEV Send Email Notification",
"StartAt": "Send Email Through Mandrill",
"States": {
  "Send Email Through Mandrill": {
    "Type": "Task",
    "Resource": "arn:aws:states:::lambda:invoke",
    "Parameters": {
      "FunctionName": "arn:aws:lambda:{region}:{accountId}:function:Email-Notification:DEV",
      "Payload": {
        "Input.$": "$"
      }
    },
...

Instead of using arn:aws:lambda:{region}:{accountId}:function:Email-Notification:DEV as the FunctionName, can I use values from the state machine input such as

"FunctionName": "arn:aws:lambda:{region}:{accountId}:function:Email-Notification:$.Alias"

or can I use other "Parameters" for alias?

I am trying to avoid setting up the same Step Function per environment.

1

1 Answers

0
votes

Parameters use JSONPath which is a query language and therefore you will not be able to concatenate values as in your example. Additionally to reference data from your Input inside Parameters it needs to be in key-value format ("key.$": "$.Value").

However looking at the Lambda Task Integration we can see it supports the parameters FunctionName and Qualifier. Looking at the Lambda Invoke API we can see that FunctionName supports 3 name formats:

  1. Function ARN
  2. Function name (name or name:alias)
  3. Partial ARN (123456789012:function:my-function)

The Qualifier field is used to specify either a version or alias of the function to be invoked. So with that said you will be able to specify your Lambda function name (no need for account and region as Step Functions does not support cross region/account invocations) and the alias (dynamically from the input) for the function without having to construct the full ARN. Here's an example of what your State will look like:

"LambdaTask": {
    "Type": "Task",
    "Resource": "arn:aws:states:::lambda:invoke",
    "Parameters": {
        "FunctionName": "NameOfYourFunction",
        "Qualifier.$": "$.Alias",
        "Payload": {
            "Foo": "Bar"
        }
    },
    "End": true
}