Skip to content

Instantly share code, notes, and snippets.

@mrollinsiv
Created November 11, 2021 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrollinsiv/dfa22ccbf415afe3e70911f895f0ea61 to your computer and use it in GitHub Desktop.
Save mrollinsiv/dfa22ccbf415afe3e70911f895f0ea61 to your computer and use it in GitHub Desktop.
SNS Fanout in Serverless Framework

Create a simple SNS Fanout in the Serverless Framework

org: barstool
app: service1
service: demo
provider:
name: aws
resources:
Resources:
MyOutputTopic:
Type: 'AWS::SNS::Topic'
Properties:
TopicName: MyOutput-${opt:stage}
Outputs:
MyOutputTopic:
Value: !Ref MyOutputTopic
Export:
Name: MyOutputTopic-${opt:stage}
org: barstool
app: service2
service: demo
provider:
name: aws
resources:
Resources:
# Create a basic queue
Service2Queue:
Type: 'AWS::SQS::Queue'
Properties:
QueueName: Service2-${opt:stage}
VisibilityTimeout: 60
# Create an SNS subscription for the queue above
Service2Subscription:
Type: 'AWS::SNS::Subscription'
Properties:
TopicArn: MyOutputTopic-${opt:stage}
Endpoint:
Fn::GetAtt: [Service2Queue, Arn]
Protocol: sqs
RawMessageDelivery: 'true'
# Provide the proper permissions for Service2Queue to recieve messages from MyOutputTopic-{$opt:stage}
Service2QueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: '*'
Action: SQS:SendMessage
Resource:
- Fn::GetAtt: [Service2Queue, Arn]
Condition:
ArnEquals:
AWS:SourceArn: MyOutputTopic-${opt:stage}
Queues:
- !Ref Service2Queue
functions:
MyOutputTopicQueue:
handler: handlers/sqs.myOutputTopic
events:
- sqs:
arn:
Fn::GetAtt: [Service2Queue, Arn]
batchSize: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment