2016-08-03 120 views
1

我创建了一个SNS主题,该主题通过cli发布来自Cloudformation的所有信息。但是,当我检查队列时,它没有收到任何SNS消息。我通过订阅我的电子邮件来验证SNS正在工作,所以这个问题似乎是在队列和SNS之间的连接。但是,我的语法找不到任何问题。据我所知,我已经精确地跟踪了亚马逊的文档。AWS SQS未收到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} 
+0

,能得到任何错误? – error2007s

回答

1

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

  1. 有各种错误试图将json直接传递给--attributes标志中的aws sqs set-queue-attributes命令。出于某种原因,它需要将修改json放在由cli引用的.json文档中。
  2. 在提供给cli的.json文件中,"Policy"值(嵌套的json)中的所有双引号必须转义(即{ \"Statement\": \"HelloWorld\" })。如果没有遵循,它会验证错误。我最终需要使用ascii转义字符才能正确格式化输出(\x5C)。
  3. 必须使用--attributes标志中的file://local-location来引用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\"}}}]}" } 
2

它看起来并不像你给了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。

+0

有没有办法通过CLI来做到这一点,或者它是否必须在云形成模板中? – asdf

+0

当然,您可以使用CLI。你并不是被迫使用Cloud Formation来做任何事情。这是您需要查看的CLI命令http://docs.aws.amazon.com/cli/latest/reference/sqs/set-queue-attributes.html –

+0

解决了它的奇妙。不过,我将添加我自己的答案,以清除在这种情况下使用bash + cli的问题,因为他们的文档中没有涉及很多fun​​kiness。 – asdf