SNS (Simple Notification Service) and SQS (Simple Queue Service) are two popular services provided by AWS (Amazon Web Services) that can be used to build scalable and reliable applications. SNS allows you to publish messages to topics, while SQS enables you to decouple and scale microservices, distributed systems, and serverless applications.
In this article, we will discuss how to add a resource policy for SNS to an SQS using the serverless framework. The serverless framework is an open-source framework that simplifies the deployment and management of serverless applications.
Step 1: Set up the Serverless Framework
The first step is to set up the serverless framework on your local machine. To do this, you need to have Node.js and npm (Node Package Manager) installed. Once you have them installed, you can install the serverless framework by running the following command:
npm install -g serverless
This command will install the serverless framework globally on your machine.
Step 2: Create a new Serverless Service
Next, you need to create a new serverless service. Open your terminal or command prompt and navigate to the directory where you want to create the service. Run the following command to create a new serverless service:
serverless create --template aws-nodejs --path my-service
This command will create a new serverless service using the aws-nodejs template.
Step 3: Configure the SNS and SQS Resources
Now, you need to configure the SNS and SQS resources in the serverless.yml file. Open the serverless.yml file in your favorite text editor and add the following code:
service: my-service
provider:
name: aws
runtime: nodejs12.x
resources:
Resources:
MySNS:
Type: AWS::SNS::Topic
Properties:
TopicName: my-topic
MySQS:
Type: AWS::SQS::Queue
Properties:
QueueName: my-queue
In this code, we define two resources: MySNS and MySQS. MySNS is an SNS topic with the name "my-topic", and MySQS is an SQS queue with the name "my-queue".
Step 4: Add the Resource Policy
Next, we need to add a resource policy to allow SNS to send messages to the SQS queue. Add the following code to the serverless.yml file:
resources:
Resources:
MySNS:
Type: AWS::SNS::Topic
Properties:
TopicName: my-topic
MySQS:
Type: AWS::SQS::Queue
Properties:
QueueName: my-queue
MySQSPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref MySQS
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: sns.amazonaws.com
Action: sqs:SendMessage
Resource: !GetAtt MySQS.Arn
Condition:
ArnEquals:
aws:SourceArn: !Ref MySNS
In this code, we define a new resource called MySQSPolicy of type AWS::SQS::QueuePolicy. We specify the MySQS queue as the resource to which the policy applies. The policy allows the SNS service (sns.amazonaws.com) to send messages to the SQS queue using the sqs:SendMessage action. The condition specifies that the source ARN (aws:SourceArn) of the SNS topic must match the ARN of the MySNS topic.
Step 5: Deploy the Service
Now, we are ready to deploy the service. Open your terminal or command prompt and navigate to the directory where the serverless.yml file is located. Run the following command to deploy the service:
serverless deploy
This command will deploy the service to your AWS account. Once the deployment is complete, you will see the output with the endpoint URLs for the SNS topic and SQS queue.
Step 6: Test the Setup
Finally, let's test the setup to ensure that SNS can send messages to the SQS queue. Open the AWS Management Console and navigate to the SNS service. Select the SNS topic that you created (my-topic) and click on the "Publish message" button. Enter a message and click on the "Publish message" button again.
Now, navigate to the SQS service and select the SQS queue that you created (my-queue). You should see the message that you published from the SNS topic in the SQS queue.
Congratulations! You have successfully added a resource policy for SNS to an SQS using the serverless framework.
In this article, we discussed how to add a resource policy for SNS to an SQS using the serverless framework. We covered the steps to set up the serverless framework, create a new serverless service, configure the SNS and SQS resources, add the resource policy, deploy the service, and test the setup.
References
| Resource | Link |
|---|---|
| Serverless Framework | https://www.serverless.com/ |
| AWS SNS | https://aws.amazon.com/sns/ |
| AWS SQS | https://aws.amazon.com/sqs/ |