简体   繁体   中英

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

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?

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

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

You can create a state to extract the context details that are then accessible to all the other states, such as:

{
    "StartAt": "ExtractContextDetails",
    "States": {
      "ExtractContextDetails": {
        "Parameters": {
          "arn.$": "$$.Execution.Id"
        },
        "Type": "Pass",
        "ResultPath": "$.contextDetails",
        "Next": "NextStateName"
      }
    }
    ....
}

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.

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.

"States": {
    "Get Alter Query": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "FunctionName": "arn:aws:lambda:ap-northeast-2:1111111:function:test-stepfuction:$LATEST",
        "Payload": {
          "body.$": "$",
          "context.$": "$$"
        }
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "Next": "Alter S3 Location"
    }
}

I solved it by adding context to the payload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM