15

For a lambda executed within a step function, I kind of expected that I could get the name of the current step from the lambda context, but it doesn't seem to be that simple.

Is there any way to get the name of the current step in a lambda that is executed within a Step Function?

Free Willaert
  • 1,139
  • 4
  • 12
  • 24

4 Answers4

16

UPDATE: as of 05/23/2019 this answer is outdated, since AWS introduced a way to access current step within a step function, see the accepted answer.


Looks like you are right, the current step doesn't get exposed through the context variable.

So, the information that would allow you to identify what stage is the state machine currently in, should be passed from the previous step (i.e. from the previous lambda). This seems to be the most correct option.

Or, as a workaround, you could try inserting pass states before calling your lambda functions to pass an id that could help you to identify the current stage.

Suppose you have two steps in your state machine:

"Step1Test": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:us-east-1:xxxxxxxxxx:function:step1test",
  "Next": "Step2Test"
},

"Step2Test": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:us-east-1:xxxxxxxxxx:function:step2test",
  "End": true
}

Here is how you can provide your lambda functions with current step id passed via event.stepId

"Step1TestEnter": {
  "Type": "Pass",
  "Next": "Step1Test",
  "Result": "Step1Test",
  "ResultPath": "$.stepId"    
},

"Step1Test": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:us-east-1:xxxxxxxxxx:function:step1test",
  "Next": "Step2TestEnter"
},

"Step2TestEnter": {
  "Type": "Pass",
  "Next": "Step2Test",
  "Result": "Step2Test",
  "ResultPath": "$.stepId"     
},

"Step2Test": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:us-east-1:xxxxxxxxxx:function:step2test",
  "End": true
}
xtx
  • 4,306
  • 4
  • 28
  • 30
  • 2
    Thanks for your answer! Inserting pass states is indeed what I've been doing, but since I want to apply this generally (so that the same lambda can occur multiple times but uses a different config depending on the step), it gets really ugly. I'll give it some time, if nothing else comes up I'll mark this as best answer. – Free Willaert May 12 '17 at 08:10
  • Bah beat me to it ^.^ – Mrk Fldig May 12 '17 at 10:32
  • Is there any other way to this . Looks like i ll have to add Pass state dummy all across the code if i have multiple steps . Also is this an anti-pattern ? @FreeWillaert – Balaji V Jul 10 '18 at 11:29
  • As I said, it gets ugly. I haven't been working with this lately though, perhaps there are improvements – Free Willaert Jul 10 '18 at 12:56
12

AWS Step Functions released Context Object where you can access information about your execution.

You can use it to send the execution arn to your lambda.

https://docs.aws.amazon.com/step-functions/latest/dg/input-output-contextobject.html

diegosantiviago
  • 988
  • 8
  • 10
  • Great, at last! :) – Free Willaert May 24 '19 at 05:46
  • 2
    Is this also included somewhere in the "context" object that gets sent to your handler function with the event? (ie: def lambda_handler(event, context): ) or do I have to explicitly access it in the Parameters section of my state definition? – sql_knievel Dec 22 '20 at 19:47
  • Seems like it needs to be passed using the `Parameters` section and the `$$` notation. E.g. `state.$: $$.State` – Jani Siivola Dec 09 '21 at 17:08
  • 1
    Can you elaborate I received `could not be used to start the Task: [The field \"Context\" is not supported by Step Functions]` ``` Error Notification Lambda: Next: Failed OutputPath: $.Payload Parameters: FunctionName: ... Payload.$: $ Context.$: $$ ``` – vfrank66 May 10 '22 at 19:52
4

Based on the documentation, I was using Parameters and $$ to pass the step function context object into my lambda function. To test and see if it was working, I thought I could go to the step function console, start a new execution and see the context object being passed into the step function on the "Step Input" tab. To my dismay, it wasn't displayed there. I added some diagnostic logging to the lambda function serializing the input to JSON and logging out to CloudWatch. Cloudwatch logs showed that the context object was being passed in.

Anyway, thought I would post this here to maybe help someone avoid the time I spent trying to figure this one out. It gets passed in, just doesn't show up in the step function console.

Daryl
  • 125
  • 8
  • 1
    Does it get passed as a separate parameter to your lambda? Can you please update your answer and provide complete code samples? Thanks! – Daniel Feb 24 '22 at 15:28
1

I highly recommend when using step functions to specify some sort of key in the step function configuration. For my step functions I always provide:

"ResultPath": "$",
"Parameters": {
  "source": "StepFunction",
  "type": "LAMBDA_METHOD_SWITCH_VALUE",
  "payload.$": "$"
},

And have each call to lambda use the type field to determine what code to call. I have found this to be much easier to implement.

Warren Parad
  • 3,910
  • 1
  • 20
  • 29
  • Are you able to give more details or an example of this? If I'm interpretting you correctly, then all Tasks in a StepFunc call the same entry Lambda, which then called the correct lambda. How are you passing variables to the individual lambdas like this? Does Parameters go with each individual state? – Dragick Apr 08 '19 at 22:02
  • 1
    I see it now! Parameters go onto individual State's, and they get passed to the Task (lambda) in the event. The StepFunction doesn't show them in the Input, but looking at the event object in the Lambda it's all there. You can even have the Parameter get populated from the StepFunc's JSON for dynamic parameters passed into the Lambda. Nice! – Dragick Apr 09 '19 at 01:45