A serverless notification API is a serverless application that uses AWS Lambda and Amazon SNS to send email notifications to subscribers.
As we already mentioned. The API operates by initially generating a Lambda function capable of accepting incoming POST requests containing a specified name and accompanying message.
Subsequently, this Lambda function triggers the transmission of an SNS message, encompassing both the provided name and message, to an assigned topic.
It's worth highlighting that the configuration of the SNS topic is optimized for dispatching email notifications to its group of subscribers.
Here is a comprehensive and detailed representation that I have prepared for your reference.
- An API receives a POST request with a JSON payload containing a
name
andmessage
. - The Lambda function processes the request and extracts the
name
andmessage
from the payload.
API Request (POST) Lambda Function SNS Topic Email Subscribers
+------------------> +-----------------> +--------------------> +-----------------+
H | | | | | List of EMAILs; | Y
E | { | { | { | | | A
L | "name": "Yaya", | "name": "Yaya", | "name": "Yaya", | | | Y
L | "message": "Hey" | "message": "Hey" | "message": "Hey" | | e.g. | A
O | } | } | } | | | 2
| | | | | Yaya Email | D
T +<------------------ +<----------------- +<----------------- | | Yours, etc. | E
H | HTTP Response | Publish SNS | Send Email | | | V
E | | Message | Notifications | | | O
R | Status 200 | with | to Subscribers | | | P
E | | name & message | | | | S
+----------------------+--------------------+--------------------+ +-----------------+
- The Lambda function publishes an SNS message with the extracted
name
andmessage
to a pre-configured SNS topic. - The SNS topic is configured to send email notifications to its subscribers.
- Email subscribers receive notifications containing the
name
andmessage
published by the Lambda function.
- Create a new SNS topic and subscribe to it using the following aws command:
aws sns create-topic --name <topic-name>
After creating the topic, copy the SNS Topic Amazon Resource Name
or ARN for later use e.g. output:
{
"TopicArn": "arn:aws:sns:<region>:<aws-id>:<topic-name>"
}
- Subscribe to the newly created SNS topic using the aws command below. Replace
<region>
with yourAWS region
,<account-id>
with your AWS account ID, and<email>
with the email address you want to use for receiving notifications:
aws sns subscribe \
--topic-arn arn:aws:sns:<region>:<account-id>:<topic-name> \
--protocol email \
--notification-endpoint <email>
- Confirm your subscription to the topic.
- Create a Lambda function with Python
3.9
runtime. - Enable the function URL in the Lambda function configuration
- Check box none to auth or leave both steps for later.
- Add the following code to your lambda
import json
import boto3
def lambda_handler(event, context):
client = boto3.client('sns')
snsArn = 'arn:aws:sns:<REGION>:<ACCOUNT ID>:<topic-name>'
body = json.loads(event.get("body"))
response = client.publish(
TopicArn = snsArn,
Message = body.get("message"),
Subject= f"Hello {body['name']}"
)
return {
'statusCode': 200,
'body': json.dumps(response)
}
- Assign the sns arn.
- Deploy the Lambda function.
You need to enable SNS access to the Lambda function,
- Go to the Lambda Function in the AWS Management Console.
- Click on the Configuration tab.
- In the Permissions section, click on the link for the Role name associated with the Lambda function.
- Under Permissions, click on Add Permission.
- Now, choose Attach Policies to proceed.
- Filter the policy list and search for AmazonSNSFullAccess.
- Select AmazonSNSFullAccess and attach it to the Lambda function's role.
You have to generate an URL to make use of in our post request.
- Open the
configuration pane
. - In the left pane, navigate to the
function URL
section. - Generate a new URL and select the
No Authorization
option (Auth Type; NONE
). - observe the policy sattements you have just made.
{
"Version": "2012-10-17",
"Statement": [
{
"StatementId": "FunctionURLAllowPublicAccess",
"Effect": "Allow",
"Principal": "*",
"Action": "lambda:InvokeFunctionUrl",
"Resource": "arn:aws:lambda:<region>:<aws-id>:function:postapi",
"Condition": {
"StringEquals": {
"lambda:FunctionUrlAuthType": "NONE"
}
}
}
]
}
- Verify the modifications if you haven't performed the aforementioned actions during the initial setup.
- Open Thunder Client. You can install its extension from vs code or any other API testing tool.
Next, you have to send a post request to the Lambda function URL with a name and message.
- Use the following curl command to send a POST request to the Lambda function URL.
curl --request POST \
--url 'https://yours.lambda-url.<region>.on.aws/' \
--header 'Content-Type: application/json' \
--data '{"name": "yaya2devops", "message": "NOTIFIED: Lambda Post Triggered"}'
Replace https://yours.lambda-url.<region>.on.aws/
with the actual Lambda function URL.
- Check that your API tool is correctly returning a status code of 200 OK upon successful operations.
By carefully following the outlined steps, you'll be well-equipped to establish a robust and functional serverless notification API using AWS Lambda and Amazon SNS.
Your thorough understanding of this process will serve as a great deal going forward (Remmember that!)
Since you've made it this far, allow me to design a path for you.