AWS lambda 状态机和 api-gateway

AWS lambda state machine and api-gateway

可能是初学者的问题,我将我的lambda并发设置为1,一次只有一个,当我调用lambda两次时,我得到错误“Internal Server Error”,相反我想有一个更精确的留言。

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

api-网关 ==>(状态机?=> 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
        }
    }
}

API Gateway Integration with StepFunction is an asynchronous call with 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

生成