AWS lambda 状态机和 api 网关

时间:2021-01-25 15:35:42

标签: aws-lambda aws-api-gateway state-machine aws-step-functions

可能是初学者的问题,我将我的 lambda 并发性设置为 1,一次只有一个,当我调用 lambda 两次时,我收到错误“内部服务器错误”,而我想要更精确的消息。

所以我设置了一个状态机,但我仍然收到“内部服务器错误”。我有什么:

api-gateways ==>(状态机?=> Lambda)

它可以这样工作吗?状态机json下面

{
    "Comment": "Example of a workflow which invokes your Lambda function, implements retries, and catches errors. Learn more at https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-creating-lambda-state-machine.html",
    "StartAt": "Call update lambda",
    "States": {
        "Call update lambda": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Parameters": {
                "FunctionName": "aem-update:$LATEST",
                "Payload": {
                    "Input.$": "$"
                }
            },
            "Catch": [
                {
                    "ErrorEquals": [
                        "States.ALL"
                    ],
                    "Next": "CatchFallback"
                }
            ],
            "End": true
        },
        "CatchFallback": {
            "Type": "Pass",
            "Result": "This is a fallback from a custom Lambda function exception",
            "End": true
        }
    }
}

1 个答案:

答案 0 :(得分:1)

API Gateway Integration with StepFunction 是与 StartExecution 的异步调用。

如果您已经创建了必要的资源,您可以通过提供一个 arn 来调用状态机,如下所示:

curl -X POST -d '{"input": "{}","name": "MyExecution","stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld"}' https://a1b2c3d4e5.execute-api.us-east-1.amazonaws.com/alpha/execution

返回执行 ARN 及其纪元日期,如下例所示。

{
"executionArn":"arn:aws:states:us-east-1:123456789012:execution:HelloWorld:MyExecution",
"startDate":1.486772644911E9
}

我假设您已经在 API 中为 DescribeExecution 创建了另一个端点,您可以在其中提供上述 executionArn 并获取执行结果。类似于以下内容:

$ curl -s -X POST -d '{"executionArn": "arn:aws:states:eu-central-1:1234567890:execution:mystatemachine:MyExecution10"}' https://1234abcdef.execute-api.eu-central-1.amazonaws.com/v1/getexecution|jq .
{
  "executionArn": "arn:aws:states:eu-central-1:1234567890:execution:mystatemachine:MyExecution10",
  "input": "{}",
  "inputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "name": "MyExecution10",
  "output": "\"This is a fallback from a custom Lambda function exception\"",
  "outputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "startDate": 1612006859.079,
  "stateMachineArn": "arn:aws:states:eu-central-1:1234567890:stateMachine:mystatemachine",
  "status": "SUCCEEDED",
  "stopDate": 1612006859.279,
  "traceHeader": "Root=1-601545cb-2ca62e87242d4cf21724f7e4;Sampled=1"
}

如您所见,我收到了来自 CatchFallback 的正确消息。

我的状态机执行和 CatchFallback 生成的相应输出

enter image description here