0

I have created a simple AWS state machine with lambda functions. Like below

{
  "Comment":"Validates data",
  "StartAt": "ChooseDocumentType",
  "States": {
    "ChooseDocumentType": {      
      "Type": "Choice",
      "Choices":[
        {
          "Variable":"$.documentType",
          "StringEquals":"RETURN",
          "Next":"ValidateReturn"
        },
        {
          "Variable":"$.documentType",
          "StringEquals":"ASSESSMENT",
          "Next":"ValidateAssessment"
        }        
      ],
      "Default":"DefaultState"
    },
    "ValidateReturn":{
      "Type":"Task",
      "Resource":"arn:aws:lambda:us-west-2:111111111:function:ValidateReturn",
      "Next":"DefaultState"
    },
     "ValidateAssessment":{
      "Type":"Task",
      "Resource":"arn:aws:lambda:us-west-2:111111111:function:ValidateAssessment",
      "Next":"DefaultState"
    },
    "DefaultState":{
      "Type":"Pass",      
      "End":true
    }
  }
}

Questions
1> How do i create stages for this state machine. (like production, development etc)?

2>Each lambda function has alias pointing to different versions. So development alias always point to $latest version and production alias point to, lets say, version 2. How do i dynamically associate state machine's stages with these lambda alias? So state machine in development stage should use lambda function with alias development and so on.

I am using AWS console to manage state machines and lambdas, and i don't see any action to create stages for state machine

LP13
  • 30,567
  • 53
  • 217
  • 400

2 Answers2

0

You can declare the alias and the version in the Lambda ARN:

# default, $LATEST
arn:aws:lambda:us-west-2:111111111:function:ValidateAssessment

# using alias
arn:aws:lambda:us-west-2:111111111:function:ValidateAssessment:development

# using version
arn:aws:lambda:us-west-2:111111111:function:ValidateAssessment:2

Use these in the Step Function definition according to your needs.

Milan Cermak
  • 7,476
  • 3
  • 44
  • 59
  • Yes, but alias or version is hardcoded here. how do you use variables for alias? So for example in AWS API Gateway you can invoke lambda using variable as `arn:aws:lambda:us-west-2:111111111:function:ValidateAssessment:${stageVariables.lambdaAlias}` where `lambdaAlias` is a variable defined in api stage. The value of `lambdaAlias` will different in each stage. How do i apply the same kind of concept in step functions? – LP13 Jan 18 '19 at 16:06
  • I don't think that's possible with Step Functions. I would create two (or more) separate Step Functions, one for development and one for production stage. Alternatively, if you want to have it all in a single SF, you could have flag in the payload that you start the SF with and build the state machine with two branches. Add a Choice state at the beginning that looks at the payload and decides based on that which branch to execute; similar to the `documentType` Choice in your SF. – Milan Cermak Jan 18 '19 at 16:35
0

Re: # 2, if your main concern is controlling which Lambda alias gets invoked, there is a way you can do that via a single step function.

Your step function state definition would be something like:

{  
   "Type": "Task",
   "Resource": "arn:aws:states:::lambda:invoke",
   "Parameters": {  
      "InvocationType": "RequestResponse",
      "FunctionName": "someFunction",
      "Qualifier.$": "$.lambdaAlias",
      "Payload": {}
   },
}

So where you execute the step function and would specify the stage if there was such a thing, you'd pass a lambdaAlias parameter. (There's nothing magical about that name, you can pull it from whatever step function input parameter you want.)

The request payload to your Lambda would go in Parameters.Payload.

https://docs.aws.amazon.com/step-functions/latest/dg/connect-lambda.html

JMM
  • 26,019
  • 3
  • 50
  • 55