# Crating SNS Topic and Subscription at the same time

**URL:** <https://forum.serverless.com/t/crating-sns-topic-and-subscription-at-the-same-time/6768>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [December 20, 2018, 5:46am UTC](https://forum.serverless.com/t/crating-sns-topic-and-subscription-at-the-same-time/6768 "2018-12-20T05:46:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![szkkeny](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@szkkeny](https://forum.serverless.com/u/szkkeny)\
**Post date:** [December 20, 2018, 5:46am UTC](https://forum.serverless.com/t/crating-sns-topic-and-subscription-at-the-same-time/6768/1 "2018-12-20T05:46:20Z")

</div>

I got below error by serverless deploy on aws.

```auto
An error occurred: SNSSubscription - Topic does not exist

```

It seems that this error is caused by creating sns subscription and  
its related topic at the same time.

yml file is like below.

```auto
service: aws-sample

provider:
  name: aws
  runtime: nodejs8.10
  region: ap-northeast-1
  stage: dev

  environment:
      sqsUrl:
        Ref: SQSQueue
      snsArn:
        Ref: SNSTopic

  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:ap-northeast-1:*:*"

functions:
  clientCreate:
    handler: src/functions/client/handler.create
    events:
      - http:
          path: clients
          method: post
          cors: true
  clientFind:
    handler: src/functions/client/handler.find
    events:
      - http:
          path: clients/{id}
          method: get
          cors: true

resources:
  Resources:
    SQSQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: ${self:service}-${self:provider.stage}-queue

    SNSTopic:
      Type: AWS::SNS::Topic
      Properties:
        DisplayName: SNS Topic
        TopicName: ${self:service}-${self:provider.stage}-topic

    SNSSubscription:
      Type: AWS::SNS::Subscription
      Properties:
          Endpoint: email@provider.com
          Protocol: email
          TopicArn: { "Fn::Join" : ["", ["arn:aws:sns:${self:provider.region}:", { "Ref" : "AWS::AccountId" }, ":${self:resources.Resources.SNSTopic.Properties.TopicName}"] ] }

```

Are there any way to creating them with one deploy?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![szkkeny](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@szkkeny](https://forum.serverless.com/u/szkkeny)\
**Post date:** [December 20, 2018, 7:35am UTC](https://forum.serverless.com/t/crating-sns-topic-and-subscription-at-the-same-time/6768/2 "2018-12-20T07:35:23Z")

</div>

Finally, this issue was solved.

---

<div class="post-metadata">

**Author:** ![mkhan24](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@mkhan24](https://forum.serverless.com/u/mkhan24)\
**Post date:** [February 19, 2019, 2:03am UTC](https://forum.serverless.com/t/crating-sns-topic-and-subscription-at-the-same-time/6768/3 "2019-02-19T02:03:47Z")

</div>

Please can you advise how did you solve this? I am still getting this error.

An error occurred: SNSSubscription - Topic does not exist

Thanks

---

<div class="post-metadata">

**Author:** ![szkkeny](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@szkkeny](https://forum.serverless.com/u/szkkeny)\
**Post date:** [February 19, 2019, 10:56am UTC](https://forum.serverless.com/t/crating-sns-topic-and-subscription-at-the-same-time/6768/4 "2019-02-19T10:56:52Z")

</div>

You can solve this issue by writing yml file like below.

```auto
    SNSTopic:
      Type: AWS::SNS::Topic
      Properties:
        DisplayName: SNSTopic
        TopicName: ${self:service}-${self:provider.stage}-topic
        Subscription:
          - Protocol: SQS
            Endpoint: { “Fn::Join” : [“”, [“arn:aws:sqs:${self:provider.region}:“, { “Ref” : “AWS::AccountId” }, “:${self:resources.Resources.SQSQueue.Properties.QueueName}”] ] }

```

Maybe, we should write Subscription section under Topic section.

Thanks

---

<div class="post-metadata">

**Author:** ![andykenward](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/andykenward/32/3117_2.png) [@andykenward](https://forum.serverless.com/u/andykenward)\
**Post date:** [March 9, 2019, 7:31pm UTC](https://forum.serverless.com/t/crating-sns-topic-and-subscription-at-the-same-time/6768/5 "2019-03-09T19:31:04Z")

</div>

If you need to add a subscription to a topic before it’s created, you can do.

```auto
resources:
  Resources:
    SNSTopic:
       Type: AWS::SNS::Topic
       Properties:
         DisplayName: SNS Topic
         TopicName: SNSTopicName
    
    SNSSubscription:
      Type: AWS::SNS::Subscription
      Properties:
        TopicArn: {"Ref": "SNSTopic"}

```
