Can a lambda in an AWS Step Function know the “execution name” of the step function that launched it?

纵饮孤独 提交于 2019-12-10 15:17:54

问题


I have this step function that can sometimes fail and I'd like to record this in a (dynamo) DB. What would be handy is if I could just create a new error handling step and that guy would just pick up the "execution name" from somewhere (didn't find it in the context) and record this as a failure.

Is that possible?


回答1:


No. Unless you pass that information in the event, Lambda doesn't know whether or not it's part of a step function. Step functions orchestrate lambdas and maintain state between lambdas.




回答2:


AWS Step Functions released recently a feature called context object.

Using $$ notation inside the Parameters block you can access information regarding your execution, including execution name, arn, state machine name, arn and others.

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




回答3:


Yes, it can, but it is not as straight-forward as you might hope.

You are right to expect that a Lambda should be able to get the name of the calling state machine. Lambdas are passed in a context object that returns information on the caller. However, that object is null when a state machine calls your Lambda. This means two things. You will have to work harder to get what you need, and that this might be implemented in the future.

Right now, the only way I know of achieving this is by starting the execution of the state machine from within another Lambda and passing in the name in the input Json. Here is my code in Java...

String executionName = //generate a unique name...

StartExecutionRequest startExecutionRequest = new StartExecutionRequest()
.withStateMachineArn(stateMachineArn)
.withInput(" {"executionName" : executionName} ") //make sure you escape the quotes
.withName(executionName);

StartExecutionResult startExecutionResult = sf.startExecution(startExecutionRequest);

String executionArn = startExecutionResult.getExecutionArn();

If you do this, you will now have the name of your execution in the input JSON of your first step. If you want to use it in other steps, you should pass it around.

You might also want the ARN of the the execution so you can call state machine methods from within your activities or tasks. You can construct the ARN yourself by using the executionName...

arn:aws:states:us-east-1:acountid:execution:statemachinename:executionName




回答4:


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. When your code fails, wrap it in a try/catch and explicitly use the passed in type which can be the name of the step to determine what to do next.



来源:https://stackoverflow.com/questions/48854398/can-a-lambda-in-an-aws-step-function-know-the-execution-name-of-the-step-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!