Search code examples
aws-cloudformationamazon-snslocalstack

Cannot create SQS subscription to an SNS topic through Cloudformation in LocalStack


Using localstack I am trying to create a template that does the following:

  1. Create an SNS topic
  2. Create an SQS queue
  3. Create a subscription that subscribes the SQS queue to the SNS topic.

My docker-compose file looks like this:

version: '3'

services:

  localstack:
    image: localstack/localstack
    container_name: localstack
    environment:
      - SERVICES=sns,sqs,cloudformation
      - DEBUG=1
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - HOSTNAME=localstack
      - AWS_DEFAULT_REGION=eu-west-2
      - AWS_ACCESS_KEY_ID=XX
      - AWS_SECRET_ACCESS_KEY=XX
    ports:
      - "4575:4575"
      - "4576:4576"
      - "4581:4581"
      - "8080:8080"
    volumes:
      - ./config/formation.yml:/usr/stuff/formation.yml
      - ./config/init.sh:/docker-entrypoint-initaws.d/init.sh

My init.sh file looks like this:

#!/bin/bash
aws cloudformation create-stack --stack-name fincorestack --template-body file:///usr/stuff/formation.yml --endpoint-url=http://localstack:4581

And finally my Cloudformation file looks like this:

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Test'
Resources:
  MySnsTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: MySnsTopic
  MySnsTopicSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: sqs
      TopicArn: !Ref MySnsTopic
      Endpoint: !GetAtt
        - MySqsQueue
        - QueueArn
  MySqsQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: MySqsQueue

This produces a very vague 500 Internal Server Error. Since this is localstack and I know that the arns are quite static I tried replacing the contents of the yml file for the subscriptions TopcArn and Endpoint with the following:

TopicArn: arn:aws:sns:eu-west-2:123456789012:MySnsTopic
Endpoint: arn:aws:sqs:elasticmq:000000000000:MySqsQueue

This time I am not getting an error but the subscription does not get created. From the debug output of localstack I can see this:

enter image description here

And the subscription is not created. Is this feature not supported in the Cloudformation implementation of localstack or am I doing something wrong?


Solution

  • This has now been fixed: https://github.com/localstack/localstack/issues/1191

    Although TopcArn and Endpoint still need to be hard-coded.