简体   繁体   English

如何使用 Ansible 和 AWS 将重新驱动策略(死信队列/DLQ)添加到 SNS 订阅

[英]How to add a redrive policy (dead-letter queue / DLQ) to a SNS subscription, with Ansible and AWS

In an Ansible script I have:在 Ansible 脚本中,我有:

- name: Subscribe lambda to SNS topic example1
  sns_topic:
    name: "example1-{{env_name}}"
    purge_subscriptions: no
    subscriptions:
      - endpoint: "arn:aws:lambda:{{ aws.region }}:{{ aws.account }}:function:{{repo_name}}-{{env_name}}"
        protocol: "lambda"

It works, and the result is that my lambda is subscribed to my SNS topic.它有效,结果是我的 lambda 订阅了我的 SNS 主题。

Now, I would want to add a DLQ to this subscription.现在,我想为这个订阅添加一个 DLQ。 I already have a SQS and I want to state it as my DLQ.我已经有一个 SQS,我想把它 state 作为我的 DLQ。

So I rewrite my code like this:所以我像这样重写我的代码:

- name: Subscribe lambda to SNS topic example1
  sns_topic:
    name: "example1-{{env_name}}"
    purge_subscriptions: no
    subscriptions:
      - endpoint: "arn:aws:lambda:{{ aws.region }}:{{ aws.account }}:function:{{repo_name}}-{{env_name}}"
        protocol: "lambda"
        redrive_policy:
          dead_letter_target_arn: "arn:aws:{{ aws.region }}:{{ aws.account }}:dlq-for-example1"

This does not work and I didn't find anything in Ansible or by googling...这不起作用,我在 Ansible 或谷歌搜索中没有找到任何东西......

What am I doing wrong?我究竟做错了什么?

Looks like you are missing sqs between arn:aws:{{aws.region}} on the last line.看起来您在最后一行的arn:aws:{{aws.region}}之间缺少sqs

dead_letter_target_arn: "arn:aws:sqs:{{ aws.region }}:{{ aws.account }}:dlq-for-example1"

The problem is that the Subscription property that is embedded in the SNS Topic only has two properties: Endpoint and Protocol (See Subscription Property ).问题是嵌入在 SNS 主题中的订阅属性只有两个属性:端点和协议(请参阅订阅属性)。

For more advanced settings, like RedrivePolicy, you need to use the stand-alone AWS::SNS::Subscription resource (See Subscription Resource ).对于更高级的设置,例如 RedrivePolicy,您需要使用独立的AWS::SNS::Subscription资源(请参阅订阅资源)。

Since AWS::SNS::Subscription is stand-alone, you must include the TopicArn that the Subscription is bound to.由于 AWS::SNS::Subscription 是独立的,因此您必须包含 Subscription 绑定到的 TopicArn。 Also note that the RedrivePolicy is in Json format.另请注意,RedrivePolicy 采用 Json 格式。

Here's a simple example of the Cloud Formation syntax from Redrive Syntax :这是来自Redrive Syntax的 Cloud Formation 语法的一个简单示例:

{
  "Resources": {
    "mySubscription": {
      "Type" : "AWS::SNS::Subscription",
      "Properties" : {
        "Protocol": "sqs",
        "Endpoint": "arn:aws:sqs:us-east-2:123456789012:MyEndpoint",
        "TopicArn": "arn:aws:sns:us-east-2:123456789012:MyTopic",
        "RedrivePolicy": {
          "deadLetterTargetArn":
            "arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue"
        }
      }
    }
  }
}

But I don't know how Ansible makes these translations.但我不知道 Ansible 是如何进行这些翻译的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM