【问题标题】:Trying to create a cloud-formation template with SNS and SQS works, but messages never arrive尝试使用 SNS 和 SQS 创建云形成模板,但消息永远不会到达
【发布时间】:2014-09-24 05:23:28
【问题描述】:

在浏览完文档后,我编写了以下 cloud-formation 模板来创建 en SNS 主题、一个 SQS 主题并将该主题订阅到 SQS 队列:

{
    "AWSTemplateFormatVersion": "2010-09-09",

    "Description": "Creates the SNS topic, SQS queue and instance that will service the custom resources queue",

    "Parameters": {
        "Environment": {
            "Description": "Environment in which to manage queues",
            "Type": "String",
            "Default": "qa",
            "AllowedValues": [ "development", "qa", "staging", "production"]
        },    
        "EmailAddress": {
            "Description": "Email to where notifications will be sent",
            "Type": "String",
            "Default": "example@email.com"
        }
    },

    "Resources": {
        "CustomResourcesQueue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
                "ReceiveMessageWaitTimeSeconds": 20,
                "VisibilityTimeout": 60,
                "QueueName": {
                    "Fn::Join": ["-", ["cloud_formation_custom_resources", {
                        "Ref": "Environment"
                    }]]
                }
            }
        },
        "CustomResourcesTopic": {
            "Type": "AWS::SNS::Topic",
            "Properties": {
                "Subscription": [
                    {
                        "Endpoint": {
                            "Ref": "EmailAddress"
                        },
                        "Protocol": "email"
                    },
                    {
                        "Endpoint": {
                            "Fn::GetAtt": ["CustomResourcesQueue", "Arn"]
                        },
                        "Protocol": "sqs"
                    }
                ]
            }
        },
        "SNSToSQSPolicy": {
            "Type": "AWS::SQS::QueuePolicy",
            "Properties": {
                "PolicyDocument": {
                    "Id": "PushMessageToSQSPolicy",
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Sid": "allow-sns-to-send-message-to-sqs",
                            "Effect": "Allow",
                            "Action": [ "sqs:*" ],
                            "Principal": {
                                "AWS": "*"
                            },
                            "Resource": {
                                "Ref": "CustomResourcesTopic"
                            },
                            "Condition": {
                                "StringEquals": {
                                    "aws:SourceArn": {
                                        "Ref": "CustomResourcesTopic"
                                    }
                                }
                            }
                        }
                    ]
                },
                "Queues": [
                    {
                        "Ref": "CustomResourcesQueue"
                    }
                ]
            }
        }
    }
}

cloud-formation 已成功创建,但每当我向 SNS 主题发布消息时,我只收到电子邮件,消息从未到达 SQS 队列。

我在这里的政策是否遗漏了什么?有没有其他方法可以使用云形成来绑定 SNS 和 SQS?

【问题讨论】:

    标签: amazon-web-services amazon-sqs amazon-sns amazon-cloudformation


    【解决方案1】:

    一些可能有帮助的事情:

    • 我的队列策略中有"Resource": "*"
    • 我在Condition 上有ArnEquals 而不是StringEquals(但我想这并不重要)。
    • 您应该可以只允许"Action": ["sqs:SendMessage"] 逃脱。

    【讨论】:

      【解决方案2】:
      "Resource": {
         "Ref": "CustomResourcesTopic"
       },
      

      不正确,因为这里必须使用Queue Arn

      "Resource": {
                "Fn::GetAtt": [
                  "CustomResourcesQueue",
                  "Arn"
                ]
              },
      

      而且这段代码我也认为是错误的

      "Condition": {
           "StringEquals": {
                "aws:SourceArn": {
                      "Ref": "CustomResourcesTopic"
                     }
                 }
            }
      

      因为你应该使用 de Topic Arn 并且使用 "Ref": "CustomResourcesTopic" 你会得到 de TopicName

      原来如此

      "Condition": {
               "StringEquals": {
                    "aws:SourceArn": "arn:aws:sns:{REGION}:{ACCOUNT_ID}:{TOPIC_NAME}"
                     }
                }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2015-07-24
      • 2021-10-04
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2016-10-27
      相关资源
      最近更新 更多