尝试使用 SNS 和 SQS 创建云形成模板,但消息从未到达
-
26-12-2019 - |
题
浏览完文档后,我编写了以下云形成模板来创建 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"
}
]
}
}
}
}
云形成已成功创建,但每当我向 SNS 主题发布消息时,我只收到电子邮件,该消息永远不会到达 SQS 队列。
我在这里的政策中遗漏了什么吗?是否有其他方法可以使用云结构来绑定 SNS 和 SQS?
解决方案
一些可能有帮助的事情:
- 我有
"Resource": "*"
在我的队列政策中。 - 我有
ArnEquals
代替StringEquals
于Condition
(但我想这并不重要)。 - 你应该能够逃脱只允许
"Action": ["sqs:SendMessage"]
.
其他提示
"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}"
}
}
不隶属于 StackOverflow