AWS SQS未接收到SNS消息

28

我创建了一个SNS主题,通过cli发布所有来自Cloudformation的信息。然而,当我检查队列时,它没有接收到任何SNS消息。我通过订阅我的电子邮件来验证SNS是否工作,因此问题似乎在队列和SNS之间的连接上。但是,我找不到我语法方面的任何问题。据我所知,我已经精确地遵循了Amazon的文档。

Bash:

#SNS parameters
SNS_NAME="${NAME}_SNS"
SQS_NAME="${NAME}_SQS"

#Create SNS topic to send cloudformation notifications to
SNS_ARN=`aws sns create-topic --name ${SNS_NAME} | jq -r '.TopicArn'`

#Create SQS to send SNS to (holding SNS messages for lambda -^ up)
SQS_URL=`aws sqs create-queue --queue-name ${SQS_NAME} | jq -r '.QueueUrl'`
SQS_ARN=`aws sqs get-queue-attributes --queue-url ${SQS_URL} --attribute-names QueueArn | jq -r '.Attributes .QueueArn'`

#subscribe the queue to the notifications
aws sns subscribe --topic-arn ${SNS_ARN} --protocol sqs --notification-endpoint ${SQS_ARN}
aws sns subscribe --topic-arn ${SNS_ARN} --protocol email-json --notification-endpoint ${EMAIL}

#Create the stack which kicks everything else off-
aws cloudformation create-stack $REGIONTEXT $ITYPETEXT --capabilities CAPABILITY_IAM --template-url https://${BUCKETNAME}.s3.amazonaws.com/${TEMPLATE} --notification-arns ${SNS_ARN} --stack-name $NAME --parameters ParameterKey=SNSARN,ParameterValue=${SNS_ARN} ParameterKey=Bucket,ParameterValue=${BUCKETNAME} ${PARAMTEXT} ${EXTRAARGS}

你有收到任何错误吗? - error2007s
4个回答

38

зңӢиө·жқҘдҪ жІЎжңүз»ҷSNSдё»йўҳеҸ‘еёғеҲ°SQSйҳҹеҲ—зҡ„жқғйҷҗгҖӮиҜ·жҹҘзңӢиҝҷдёӘж•ҷзЁӢдёӯзҡ„第2жӯҘгҖӮжӮЁйңҖиҰҒеҗ‘SQSйҳҹеҲ—ж·»еҠ зұ»дјјдәҺд»ҘдёӢзҡ„зӯ–з•Ҙпјҡ

{
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"MySQSPolicy001",
      "Effect":"Allow",
      "Principal":"*",
      "Action":"sqs:SendMessage",
      "Resource":"arn:aws:sqs:us-east-1:123456789012:MyQueue",
      "Condition":{
        "ArnEquals":{
          "aws:SourceArn":"arn:aws:sns:us-east-1:123456789012:MyTopic"
        }
      }
    }
  ]
}

将ARN替换为您主题和队列的相应值。


有没有办法通过CLI来完成,还是必须使用云形成模板? - asdf
当然可以使用CLI。你不必强制使用Cloud Formation。这是你需要查看的CLI命令:http://docs.aws.amazon.com/cli/latest/reference/sqs/set-queue-attributes.html - Mark B
解决得非常出色。然而,我将添加自己的答案以澄清在此情况下使用bash + cli时未涵盖的许多棘手问题。 - asdf
帮我找到了 Terraform 代码中的一个笔误...谢谢。 - weaveoftheride
我目前正在使用类似的SQS策略,它运行良好。但是我想将主体设置为账户ID并删除条件部分,如果这样做,我就无法获取消息。有什么具体原因吗? - Prashanna
是的,对我来说也是权限问题。 - CodeConnoisseur

19

8
如果SQS被加密了,那么将消息推送到队列的事件必须按照以下步骤进行:
多个AWS服务将事件发送到Amazon SQS队列。为使这些事件源能够与加密队列一起使用,您必须执行以下步骤。
- 使用客户托管的CMK。 为了让AWS服务具有kms:GenerateDataKey *和kms:Decrypt权限,请将以下语句添加到CMK策略中。 ``` { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": { "Service": "service.amazonaws.com" }, "Action": [ "kms:GenerateDataKey*", "kms:Decrypt" ], "Resource": "*" }] } ``` - 创建新的SSE队列或使用您的CMK的ARN配置现有的SSE队列。 向事件源提供加密队列的ARN。
对于sns,将服务部分替换为sns.amazonaws.com
``` "Principal": { "Service": "sns.amazonaws.com" } ```

6
感谢Mark B的回答。这为让此工作开始提供了一个很好的起点。然而,通过CLI使策略文档工作需要一些在文档中未涉及的技巧。
  1. 尝试直接将JSON传递给aws sqs set-queue-attributes命令中的--attributes标志会出现各种各样的错误。出于某种原因,它需要引用CLI中的一个.json文档来修改JSON。
  2. 在提供给CLI的.json文件中,必须转义"Policy"值(嵌套的JSON)内的所有双引号(即{ \"Statement\": \"HelloWorld\" })。如果不遵循这个规则,将会出现验证错误。我最终需要使用ASCII转义字符才能正确格式化输出(\x5C)。
  3. 必须使用file://local-location--attributes标志中引用JSON文件。如果不遵循这个规则,将会出现错误。

请参考以下元素:

load_sqs.sh:

SQS_POLICY=
sqs-policy()
{
#First param is the queue arn, second param is the topic arn
SQS_POLICY=`printf '{ "Policy": "{\x5C\"Version\x5C\":\x5C\"2012-10-17\x5C\",\x5C\"Statement\x5C\":[{\x5C\"Sid\x5C\":\x5C\"CloudformationLambdaSQSPolicy\x5C\",\x5C\"Effect\x5C\":\x5C\"Allow\x5C\",\x5C\"Principal\x5C\":\x5C\"*\x5C\",\x5C\"Action\x5C\":\x5C\"sqs:SendMessage\x5C\",\x5C\"Resource\x5C\":\x5C\"%s\x5C\",\x5C\"Condition\x5C\":{\x5C\"ArnEquals\x5C\":{\x5C\"aws:SourceArn\x5C\":\x5C\"%s\x5C\"}}}]}" }' "$1" "$2"`
`echo $SQS_POLICY > $PWD/sqs-policy.json`
}

#SNS parameters
SNS_NAME="${NAME}_SNS"
SQS_NAME="${NAME}_SQS"

#Create SNS topic to send cloudformation notifications to
SNS_ARN=`aws sns create-topic --name ${SNS_NAME} | jq -r '.TopicArn'`

#Create SQS to send SNS to (holding SNS messages for lambda -^ up)
SQS_URL=`aws sqs create-queue --queue-name ${SQS_NAME} | jq -r '.QueueUrl'`
SQS_ARN=`aws sqs get-queue-attributes --queue-url ${SQS_URL} --attribute-names QueueArn | jq -r '.Attributes .QueueArn'`

#Add necessary SQS <--> SNS permissions
sqs-policy ${SQS_ARN} ${SNS_ARN}
`aws sqs set-queue-attributes --queue-url ${SQS_URL} --attributes file://sqs-policy.json`

#subscribe the queue to the notifications
aws sns subscribe --topic-arn ${SNS_ARN} --protocol sqs --notification-endpoint ${SQS_ARN}

sqs-policy.json:

{ "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"CloudformationLambdaSQSPolicy\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"sqs:SendMessage\",\"Resource\":\"ResourceARN\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"SourceARN\"}}}]}" }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接