首页 > 解决方案 > AWS lambda 状态机和 api-gateway

问题描述

可能是一个初学者的问题,我将我的 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
        }
    }
}

标签: aws-lambdaaws-api-gatewaystate-machineaws-step-functions

解决方案


API Gateway 与 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

在此处输入图像描述


推荐阅读