Ale*_*her 22 amazon-web-services aws-lambda
我知道当一个Lambda函数失败时(例如当有一个超时)时,它会再次尝试再运行该函数3次.有什么方法可以避免这种行为吗?我一直在阅读文档,但没有找到任何关于这个.
谢谢!
das*_*mug 21
当它是未处理的失败(你没有捕获到的错误)或者你是否处理它但仍然告诉Lambda重试时(例如在Node中,当你callback()使用非null的第一个参数调用时)它会重试.
要阻止它重试,您应该确保处理任何错误,并通过返回非错误(或在Node中调用,告诉Lambda您的调用成功完成)callback(null, <any>).
为此,您可以使用try-catch包含处理函数的整个主体.
module.exports.handler(event, context, callback) {
try {
// Do you what you want to do.
return callback(null, 'Success')
} catch (err) {
// You probably still want to log it.
console.error(err)
// Return happy despite all the hardships you went through.
return callback(null, 'Still success')
}
}
Run Code Online (Sandbox Code Playgroud)
对于因超时而导致的故障,您可以做些事情.
如果它超出了你的处理程序,你的代码通常会出现问题(例如,不正确的数据库配置等),你应该看一下.
如果在调用期间它在你的处理程序内超时,你可以做些什么.
context.getRemainingTimeInMillis()Lambda是否即将超时,以便您可以更早地处理它.如前所述,此功能是最近添加的。要使用以下方法实现此行为,cloudformation您将需要AWS::Lambda::Version和AWS::Lambda::EventInvokeConfig:
MyLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: my-lambda-dev
...
MyLambdaVersion:
Type: AWS::Lambda::Version
Properties:
FunctionName: !Ref MyLambda
MyLambdaAsyncConfig:
Type: AWS::Lambda::EventInvokeConfig
Properties:
FunctionName: !Ref MyLambda
MaximumRetryAttempts: 0
Qualifier: !GetAtt MyLambdaVersion.Version
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15262 次 |
| 最近记录: |