Question

After going through the docs, I wrote the following cloud-formation template to create en SNS topic, an SQS topic and subscribe the topic to the SQS queue:

{
    "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"
                    }
                ]
            }
        }
    }
}

The cloud-formation is successfully created, but whenever I publish a message to the SNS topic, I only get the email, the message never arrives at the SQS queue.

Am I missing something at the policy here? Is there some other way to use cloud-formations to tie SNS and SQS?

Was it helpful?

Solution

A few things that might help:

  • I have "Resource": "*" in my queue policy.
  • I have ArnEquals instead of StringEquals on the Condition (but I guess that doesn't matter).
  • You should be able to get away with allowing just "Action": ["sqs:SendMessage"].

OTHER TIPS

"Resource": {
   "Ref": "CustomResourcesTopic"
 },

It is incorrect, because here you must use the Queue Arn

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

And this code also I think it is wrong

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

Because you should use de Topic Arn and using "Ref": "CustomResourcesTopic" you will get de TopicName

So, it will be

"Condition": {
         "StringEquals": {
              "aws:SourceArn": "arn:aws:sns:{REGION}:{ACCOUNT_ID}:{TOPIC_NAME}"
               }
          }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top