Beyond Migration: Why Non-Profits Need to Modernize now & not 3 years later
Connecting SNS to API Gateway allows for a streamlined, secure, and flexible approach to handling message delivery and enhances serverless applications by integrating asynchronous communication.
In this article, I will share two ways of connecting API gateways to SNS. Before we get started, let me share some context about Amazon API Gateway and SNS.
What is Amazon API Gateway?
Amazon API Gateway is a fully managed service that allows developers to create, publish, maintain, monitor, and secure APIs at any scale. API Gateway handles thousands of concurrent API calls. At its basic, it is a service that allows us to create backend endpoints that we later request through the frontend of the application.
Features of Amazon API Gateway
- Fully managed service by Amazon
- Pay-as-you-go service
- Supports authorization and access control, throttling, monitoring, and API version management.
- Support for RESTful APIs and WebSocket APIs.
- Console for building and testing API.
- Integration with different other AWS services.

What is Amazon SNS?
Amazon SNS is a managed service that provides message delivery from publishers to subscribers (also known as producers and consumers). It is a one-to-many relationship service. It consists of Topic (a logical access point that acts as a communication channel) and Subscriptions (Consumers- AWS Lambda, AWS SQS, Email, etc.). Publishers communicate asynchronously with subscribers by sending messages to a topic, simplifying SNS send message tasks.
Features of Amazon SNS
- Supports application-to-application messaging, where subscribers can be AWS Lambda, AWS SQS, HTTP/S endpoints, etc.
- Supports application-to-person notifications, where subscribers can be a user email address, mobile number.
- Supports several strategies( delivery retry policy, dead letter queue) that work together to provide message durability.
- Supports message filtering which allows subscribers to only get the notification when the message adheres to the filter policy.
- Provides different SNS API or Actions that enables you to build distributed web-enabled applications.

2 Ways of Connecting API Gateway to SNS
Suppose you are working on an application where there is a need to send different messages to certain users asynchronously. You can create an endpoint that takes the message to send to the user. We can connect API Gateway to SNS for this implementation.
The architecture will look like the following.

01. Connect API Gateway with SNS
Following are the steps to connect AI Gateway with SNS to enable seamless message delivery using Amazon SNS & API.
Step 1- Create an SNS Topic with an email subscription

Select email protocol and add endpoint (user mail).

After the subscription is created the endpoint will receive a confirmation mail and the status will remain pending until the user confirmed the subscription.



Once the user accepted the subscription the status will change to confirmed.

Step 2- Create an IAM Role for the API with SNS Publish permission
Create a new IAM role

Create a policy with SNS publish permission.

Attach the policy to the newly created IAM role.

Step 3- Create the Rest API
Create a new rest api

Attach a resource and method to the api

Update the Integration Request of the api. In the Action, I am using Publish (SNS Publish API) to send message to the topic.

Add Query Parameters in the method request

Update the Query String Parameters in the integration request

Step 4- Deploy the API
Step 5- After deploying the api test it by passing appropriate query parameters.
- Message : the message you want to send
- Topic : ARN of created SNS topic

The user will receive the notification

We can use the above API for our use case but the only problem is that we have to pass the SNS topic ARN in query parameter whenever we want to send the message. It is not ideal to share the resource physical ID. So we will use a different way to connect API to SNS.
02. Connecting API to SNS using Request Template
All the resource creation will remain the same except the Rest API. The Rest API will be using the request template.
What is the request template?
In API Gateway, an API’s method request can take a payload in a different format from the corresponding integration request payload, as required in the backend. API Gateway lets you use mapping templates to map the payload from a method request to the corresponding integration request and an integration response to the corresponding method response.
A mapping template is a script expressed in Velocity Template Language (VTL) and applied to the payload using JSONPath expressions.
1. Update the above Rest API
Create a modal for the incoming request

2. Add the modal in the method request and update the request validator to validate body

3. In the Integration request change the action type to path and update the path according to your respective region of SNS Topic.

4. Add the Content-type header in the integration request.

5. Add the mapping template where topic will contain the arn of your SNS Topic.

Here I am using modal to validate the incoming request and a request template to format the incoming request according to the publish API. While using this we don’t need the topic arn each time to call the API. This helps us in providing abstraction with the underline resource data.
6. Test the api by passing the appropriate request body.
- message: the message you want to send

The user will receive the notification

All done, You can start consuming the api.
The entire process can be pretty confusing while doing it for the first time, so here a repository with a sample SAM application creates all the required resources.
Conclusion
Connecting Amazon SNS to API Gateway provides a powerful solution for building scalable and responsive applications that require real-time notifications. By following the steps outlined in the two approaches, you can set up an SNS integration, streamline communication, and enhance reliability and performance through AWS’s robust infrastructure.
FAQs
1. Why Should You Use SNS with API Gateway?
Integrating SNS with an API gateway allows you to send notifications asynchronously based on API requests. This is useful in event-driven architectures where you need to notify other services without blocking the main application flow.
2. Can You Use Other AWS Services with SNS and API Gateway?
You can integrate other AWS services, like Lambda, DynamoDB, and CloudWatch.