REST API with SNS Integration
Understand the integration of API Gateway with SNS.
We'll cover the following
Text message API
Amazon Simple Notification Service (SNS) is a popular Pub/Sub service in AWS. Along with the several services within AWS, it also integrates with external services like email and text messaging. That makes it highly versatile and valuable in a vast range of scenarios that require notifications.
We can integrate SNS with the API Gateway to publish a notification on the SNS topic by making an API call. To make this interesting, let’s make an API that can send an SMS text message to your phone.
We’ll look at some code to understand this better.
Implementation
The API for SNS is purely based on query string parameters. When we publish a message on the topic, we don’t specify the action in the URI. The target API is meant for publishing and doesn’t need any explicit mention. However, the query string parameters carry a payload, and the SNS topic, ARN. The details about the target queue go into the path. So, for a Publish action, the authorized API calls into SNS look like this {snspublishurl}?TopicArn=Arn&Message=Payload
.
We should configure the API Gateway to construct this URL from the input data. Our API can obtain this information in any form. For example, we can configure it in the API definition or provide it from a specific field in the body, path parameters, or query string. However, we should map it so that the target API call carries the data required by the SNS API.
Let’s check an example. A simple trick to test SNS is to configure it to send SMS text messages. That way, we can verify messages published into SNS without messing with other additional AWS services. We create a new SNS topic and configure it to send SMS text messages to a phone number. Check out the code below that creates an SNS topic and a subscription. Then, we can invoke the API to get an SMS text message to the phone number.
Get hands-on with 1400+ tech skills courses.