AWS SQS未收到SNS消息

时间:2016-08-03 22:55:52

标签: bash amazon-web-services aws-cli

我创建了一个SNS主题,通过cli发布来自Cloudformation的所有信息。但是,当我检查队列时,它没有收到任何SNS消息。我通过订阅我的电子邮件来验证SNS正在工作,所以问题似乎在于队列和SNS之间的连接。但是,我发现我的语法没有任何问题。据我所知,我确切地遵循了亚马逊的文件。

击:

#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}

6 个答案:

答案 0 :(得分:4)

您似乎没有将SNS主题权限发布到SQS队列。请看this walkthrough中的第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替换为主题和队列的ARN。

答案 1 :(得分:4)

感谢Mark B的回答。它为这项工作提供了开始。但是,为了使策略文档通过CLI工作,文档中没有涉及一些怪癖。

  1. 尝试将json直接传递给--attributes命令中的aws sqs set-queue-attributes标志有各种错误。由于某种原因,它需要修改json在cli引用的.json文档中。
  2. 在提供给cli的.json文件中,必须对"Policy"值(嵌套json)中的所有双引号进行转义(即{ \"Statement\": \"HelloWorld\" })。如果不遵循,则验证错误。我最终需要使用ascii转义字符才能正确格式化输出(\x5C)。
  3. 必须使用file://local-location标志中的--attributes引用json文件。如果不遵循,则会抛出错误。
  4. 请参阅以下用于参考的元素:

    <强> 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\"}}}]}" }
    

答案 2 :(得分:4)

在我的情况下,SQS不接收来自SNS的消息,因为SQS已打开加密。当我在SQS上关闭加密时它就开始工作了!

此AWS文档说明了如何启用SNS与加密SQS队列的兼容性:

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-server-side-encryption.html#compatibility-with-aws-services

SNS需要额外的权限才能使用KMS密钥加密队列的消息。

答案 3 :(得分:0)

我遇到了这个问题,我要做的就是更改SQS队列中的权限。

所以这是步骤:

  1. 打开SQS管理控制台
  2. 选择您的SQS队列
  3. 转到“权限”标签
  4. 单击按钮添加权限
  5. 设置如下图所示

enter image description here

  1. 点击添加权限

我希望这可以帮助任何人。

答案 4 :(得分:0)

如果已加密SQS,则将消息推送到队列的事件必须遵循以下步骤

几个AWS服务会将事件发送到Amazon SQS队列。要允许这些事件源与加密队列一起使用,必须执行以下步骤。

  • 使用客户管理的CMK。

    要允许AWS服务具有kms:GenerateDataKey *和 kms:解密权限,将以下语句添加到CMK 政策。

    { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": { "Service": "service.amazonaws.com" }, "Action": [ "kms:GenerateDataKey*", "kms:Decrypt" ], "Resource": "*" }] }

  • 使用以下命令创建新的SSE队列或配置现有的SSE队列 您的CMK的ARN。

    将加密队列的ARN提供给事件源。

  

对于sns,请使用sns.amazonaws.com替换服务部分

"Principal": { "Service": "sns.amazonaws.com" }

答案 5 :(得分:-1)

使用AWS控制台。

  1. 转到此处https://console.aws.amazon.com/sqs
  2. 选择您的队列
  3. 点击队列操作
  4. 将队列订阅到SNS主题。
  5. 选择主题。
  6. 点击订阅
  7. 做完,微笑! =)
相关问题