aws-lambda - 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
}
}
}
解决方案
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
推荐阅读
- swift - Swift Combine 从 func 返回 Publisher
- database - 有两个或多个具有相同属性的关系的主要原因是什么?
- java - 如何优化有关 if 条件的逻辑?
- javascript - SVG - 使用带有转换元素的 checkIntersection()
- react-native - 跨函数更改变量值,例如 React Native 中的全局变量?
- java - 如何在 Textview 中显示数据 2 秒(android Studio)?
- python-3.x - 在 Python Pandas 数据框中寻找更智能的方法来处理多个版本的值
- python - 如何在某个位置将数字从列表更改为另一个?
- angularjs - 为什么必须使用数组在 AngularJS 中使用控制器进行依赖注入,而不是用于其他函数和方法?
- ios - Xcode11 的设备支持文件 (iOS 14)