
This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.
A Simple Notification Service (SNS) Topic can be used to create notification alerts, such as an email notification. This uses the messaging services pub sub model.
- pub (publish) - create a new alert in a Topic
- sub (subscribe) - get alerts in a Topic
Let's say you used the aws sns create-topic command to create a Simple Notification Service (SNS) Topic.
~]$ aws sns create-topic --name my-topic
{
"TopicArn": "arn:aws:sns:us-east-1:123456789012:my-topic"
}
There are various types of subscriptions.
- application - messages published to the topic will be delivered to an EndpointArn for a mobile app or device - the message must be JSO
- email - messages published to the topic will be emailed to the subscriber
- email-json - messages published to the topic will be emailed to the subscriber - the message must be JSON
- firehouse - messages published to the topic will be delivered to an AWS Kinesis Data Firehose delivery stream - the message must be JSON
- http - messages published to the topic will be POST to an HTTP url - the message must be JSON
- https - messages published to the topic will be POST to an HTTPS url - the message must be JSON
- lambda - messages published to the topic will be sent to a Lambda Function
- sms - messages published to the topic will be published to an SMS device (e.g. cellphone)
- Simple Queue Service (SQS) - messages published to the topic will be put onto a SQS queue
The aws sns subscribe command to subscribe one of your Lambda Functions to the topic.
~]$ aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol lambda --notification-endpoint arn:aws:lambda:us-east-1:123456789012:function:my-lambda-function
{
"SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:my-topic:e68a4a47-ce56-48a5-865d-31f5920c4355"
}
The aws lambda add-permission command can be used to allow the SNS Topic to invoke the Lambda Function.
aws lambda add-permission \
--function-name sendgrid \
--source-arn arn:aws:sns:us-east-1:12346789012:my-topic \
--statement-id allow-sns-to-invoke-lambda \
--action "lambda:InvokeFunction" \
--principal sns.amazonaws.com
This command attaches the following IAM Policy to the Lambda Function, allowing the SNS Topic to invoke the Lambda Function.
{
"Version": "2012-10-17",
"Id": "default",
"Statement": [
{
"Sid": "allow-sns-to-invoke-lambda",
"Effect": "Allow",
"Principal": {
"Service": "sns.amazonaws.com"
},
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:my-lambda-function",
"Condition": {
"ArnLike": {
"AWS:SourceArn": "arn:aws:sns:us-east-1:123456789012:my-sns-topic"
}
}
}
]
}
The aws sns publish command can be used to publish a message to the topic, which in this example should deliver the message to the Lambda Function.
~]$ aws sns publish --subject Hello --message World --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic
{
"MessageId": "3756dea6-80cd-5e4a-ba9a-80a33a856481"
}
Often, you will probably be using some sort of client, such as a node.js or Python or Java app, that will be publishing a structured JSON message to the SNS Topic, for example, something like this using Python boto3.
#!/usr/bin/python3
import boto3
import json
client = boto3.client('sns')
message = '{"foo" : "hello", "bar": "world" }'
client.publish(
TopicArn='arn:aws:sns:us-east-1:123456789012:my-topic',
Subject='Hello',
MessageStructure='json',
Message=json.dumps({'default': json.dumps(message)})
)
In this scenario, your Lambda function would have something like this, to get the values of the "foo" and "bar" keys in this example from the published message.
import json
def lambda_handler(event, context):
for item in event['Records']:
parsed_json = json.loads(item['Sns']['Message'])
foo = parsed_json['foo']
bar = parsed_json['bar']
Did you find this article helpful?
If so, consider buying me a coffee over at