服务
关于
CloudProse博客
无服务器

2分钟提示:无服务器中的死信配置

 安迪·沃宗(Andy Warzon)Trek10
安迪·沃宗(Andy Warzon) | 2017年11月15日

2017年11月15日,星期三

死信配置 are a 预先tty slick part of the 江苏体彩 无服务器 ecosystem. The basic idea is this: There are some events which trigger Lambda functions synchronously (such as API Gateway) and others which trigger Lambda asynchronously (such as an S3 event). For the latter group, 江苏体彩 automatically retries if Lambda fails. This means that you can usually ignore transient Lambda errors. But what if it fails permanently? That’s where dead letter queues come in. You can assign a DeadLetterConfig to your Lambda function and it will push this failed event to an SQS队列 SNS主题 。在这里,您可以设置一个自动化系统来处理这些事件,或者只是警告它们以进行手动检查。

的好人 无服务器框架 添加 an OnError attribute so you could define a dead letter config easily in your serverless.yml. However, this requires that you create the SNS主题 upfront and it does not support SQS. Creating the resources separately may make sense if you want a centralized queue, but we typically 预先fer that the dead letter resources (SNS or SQS) are part of the same stack to reduce dependencies and increase automation and portability.

现在是个好消息!

There is a straightforward way to create a full dead letter config, including SQS/SNS resources, right in your 无服务器 project. It uses a less-well-known feature of 无服务器框架: the ability to define your function in the main part of serverless.yml and then add custom attributes to that in the Resources: (a.k.a. arbitrary Cloudformation) section of serverless.yml. It looks something like this:

<pre><code>iamRoleStatements:-效果:允许操作:-sns:Publish资源:-{“ Fn :: Sub”:“ $ {DeadLetterSNSTopic}”} .... functions:myFunction:.... resources:资源:MyFunctionLogGroup:DependsOn: IamRoleLambdaExecution MyFunctionLambdaFunction:类型:江苏体彩 :: Lambda :: Function属性:DeadLetterConfig:TargetArn:参考:DeadLetterSNSTopic DeadLetterSNSTopic:类型:江苏体彩 :: SNS :: Topic</code></pre>

请注意此处使用的一些技巧:

  • In iamRoleStatements we give the function permission to publish to the SNS主题 that is created as part of the stack.
  • When you declare the function myFunction, 无服务器 will actually create a Cloudformation resource with the logical name MyFunctionLambdaFunction… note both the upper-casing of the first letter and the suffix 添加 . This is the function name you need to use when adding attributes in the Resources section.
  • We add DependsOn so that the IAM policy is created first, which 所有 ows the dead letter config to be successfully applied later.

玩得开心!

有疑问/意见吗?欢迎随时与我们联系 [email protected].

作者
 安迪·沃宗(Andy Warzon)Trek10
安迪·沃宗(Andy Warzon)

创办人& CTO

创办人&CTO Andy一直在江苏体彩上进行开发已有十多年,并且是江苏体彩认证解决方案架构师-专业人士。