How To Connect SNS to API Gateway

How To Connect SNS to API Gateway

January 20, 2022

serverlessawssnsapi-gateway

How To Connect SNS to API Gateway

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.

How API Gateway works

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.

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.

How SNS works

Use-Case for 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 use the API Gateway and SNS for the above requirement.

The architecture will look like the following.

Architecture

How to connect API Gateway with SNS

1- Create an SNS Topic with an email subscription

SNS Topic

Select email protocol and add endpoint (user mail).

Subscription

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

Subscription Pending Status

Email Confirmation Confirmation Message

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

Subscription Confirmed

2- Create an IAM Role for the API with sns Publish permission

Create a new IAM role

New IAM Role

Create a policy with SNS publish permission.

New SNS Policy

Attach the policy to the newly created IAM role.

Attach Policy

3- Create the Rest Api

Create a new rest api

Create Rest API

Attach a resource and method to the api

Resource and method

Update the Integration Request of the api

Integration

Add query parameters in the method request

Method

Update the query string parameters in the integration request

Query Strings

4- Deploy the API

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

Test API

The user will receive the notification

Result

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.

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.

Update the above Rest API

Create a modal for the incoming request

Create Modal

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

Request Validator

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

Updated Integration Topic

Add the Content-type header in the integration request.

Update Query String

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

Mapping Template

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.

Test the api by passing the appropriate request body.

  • message: the message you want to send

Test Updated API

The user will receive the notification

New Result

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.

Antstack Blog Post
Antstack Blog Post

Are you planning to go to serverless? AntStack is a cloud computing service and consulting company primarily focusing on Serverless Computing. We help companies get up and running with serverless, and we’ll make sure that there are no limits.

Keep track of our socials and connect with us - LinkedIn

event bannerevent bannerevent banner