Invoking AWS Lambda functions via Amazon SNS
We released a new feature today for Amazon SNS that enables developers to perform custom message handling or publish messages to other AWS services by subscribing AWS Lambda functions to SNS topics. When a message is published to an SNS topic that has a Lambda function subscribed to it, the Lambda function is invoked with the payload of the published message. The Lambda function receives this message payload as an input parameter and can use information contained in the payload to manipulate it, publish it to another set of SNS topics, or send the message to other AWS services. Additionally, we have now added delivery status support for Lambda destinations.
In this post, we will show you how to create a sample message history store using SNS, Lambda, and Amazon DynamoDB. To accomplish this, we will first create a DynamoDB table. Once the DynamoDB table is created, we will create a Lambda function, prepare it for invocation, subscribe it to an SNS topic and finally invoke it by publishing a message to the SNS topic via the
Creating a DynamoDB table
Begin by creating a table in DynamoDB named snslambda
with a primary hash key named SnsTopicArn
and a primary range key named SnsPublishTime
. To query the table with a SNS MessageId
, you can optionally add a global secondary index on SnsMessageId
Creating the Lambda function
Using the Lambda console create the following Lambda function and name it messageStore:
console.log('Loading event'); var aws = require('aws-sdk'); var ddb = new aws.DynamoDB({params: {TableName: 'snslambda'}}); exports.handler = function(event, context) { var SnsMessageId = event.Records[0].Sns.MessageId; var SnsPublishTime = event.Records[0].Sns.Timestamp; var SnsTopicArn = event.Records[0].Sns.TopicArn; var LambdaReceiveTime = new Date().toString(); var itemParams = {Item: {SnsTopicArn: {S: SnsTopicArn}, SnsPublishTime: {S: SnsPublishTime}, SnsMessageId: {S: SnsMessageId}, LambdaReceiveTime: {S: LambdaReceiveTime} }}; ddb.putItem(itemParams, function() { context.done(null,''); }); };
This Lambda function takes the SNS message it receives (see below for the exact SNS message structure) and extracts the MessageId
, Timestamp
, and TopicArn
. It then stores this data into the DynamoDB table that we created prior to this step. Additionally, it stores the current time from the Lambda function’s perspective.
To use DynamoDB from a Lambda function, you also need to add a policy to your IAM lambda_exec_role
. Here is an example policy:
{ "Version": "2012-10-17", "Statement":[ { "Sid":"Stmt1428510662000", "Effect":"Allow", "Action":["dynamodb:*"], "Resource":["arn:aws:dynamodb:us-east-1:123456789012:table/snslambda"] } ] }
Preparing the Lambda function for invocation
Once your Lambda function is created you need to assign a policy document to your Lambda function to allow it to be invoked by SNS. This is done using the AddPermissions
API call. The AddPermissions
API takes a policy document as an argument. Here is an example policy document for our Lamba function, messageStore
and an SNS topic named lambda_topic
:
{ "Statement":[ {"Condition": {"ArnLike":{"AWS:SourceArn":"arn:aws:sns:us-east-1:123456789012:lambda_topic"}}, "Resource":"arn:aws:lambda:us-east-1:123456789023:function:messageStore", "Action":"lambda:invokeFunction", "Principal":{"Service":"sns.amazonaws.com"}, "Sid":"sns invoke","Effect":"Allow" }], "Id":"default", "Version":"2012-10-17" }
The policy is automatically set for you if you are using the Lambda console. Once the policy has been assigned, the Lambda function is ready to be subscribed to an SNS topic.
Subscribing Lambda functions to SNS topics
Step 1: Using the SNS console, navigate to the Topics page. Identify an existing SNS topic that you would like to subscribe the Lambda function to. If you do not have an existing SNS topic, create one by clicking the Create new topic button, entering details such as Topic name and Display name, and then clicking Create topic. In this example, we will use a SNS topic named lambda_topic
. Once the SNS topic has been identified or created, click the ARN of the SNS topic in the Topics page to open the Topic Details page.
You can also create the SNS topic and retrieve its ARN using the SNS APIs. Here is an example:
String topicArn = snsClient.createTopic(new CreateTopicRequest("lambda_topic" /*topic Name*/).getTopicArn();
Step 2: In the Topic Details page, click the Create subscription button. In the dialog box that appears, select AWS Lambda in the Protocol drop-down list. In the Endpoint field, select the ARN of the Lambda function, and click Create subscription. You will see a new subscription appear in the table of subscriptions associated with your SNS topic in the Topic Details page. This new subscription should have Lambda as the Protocol and the Lambda function ARN as the Endpoint.
The Lambda function can also be subscribed to an SNS topic using the SNS APIs. Here is an example:
String subscriptionArn = snsClient.subscribe(new SubscribeRequest(topicArn,"lambda", "arn:aws:lambda:us-east-1:123456789012:function:messageStore"));
Publishing messages to invoke the Lambda function
Now that the Lambda function has subscribed to an SNS topic, the next step is to publish a message to the SNS topic and observe the invocation of the Lambda function in the Lambda console. Using the SNS console, navigate to the Topic Details page for the SNS topic that has the Lambda function subscribed to it. Click the Publish to topic button, which will redirect you to the Publish a message page. On this page, enter a subject, select a message format, and enter the message payload that you want to invoke the Lambda function. If you select Raw (same message body for all protocols), you can simply enter your message in the Message text area. If you select JSON, you must encode the message in the JSON message format expected by SNS. You can also use the JSON message generator for assistance with converting your message to the expected SNS format.
When you have finished creating your message payload, click the Publish message button. This will publish the message to the SNS topic, and SNS will then attempt to deliver to all subscribers for that SNS topic. In this case, it will attempt to deliver the message to the subscribed Lambda function, thereby invoking it and passing a JSON formatted SNSEvent
as an input parameter. Here is an example SNSEvent
:
{ "Records":[ { "EventSource":"aws:sns", "EventVersion": "1.0", "EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789012:lambda_topic:0b6941c3-f04d-4d3e-a66d-b1df00e1e381", "Sns":{ "Type": "Notification", "MessageId":"95df01b4-ee98-5cb9-9903-4c221d41eb5e", "TopicArn":"arn:aws:sns:us-east-1:123456789012:lambda_topic", "Subject":"TestInvoke", "Message":"<message payload>", "Timestamp":"2015-04-02T07:36:57.451Z", "SignatureVersion":"1", "Signature":"r0Dc5YVHuAglGcmZ9Q7SpFb2PuRDFmJNprJlAEEk8CzSq9Btu8U7dxOu++uU", "SigningCertUrl":"http://sns.us-east-1.amazonaws.com/SimpleNotificationService-d6d679a1d18e95c2f9ffcf11f4f9e198.pem", "UnsubscribeUrl":"http://cloudcast.amazon.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:example_topic:0b6941c3-f04d-4d3e-a66d-b1df00e1e381", "MessageAttributes":{"key":{"Type":"String","Value":"value"}} } } ] }
Once the Lambda function is invoked and starts executing, you can see the results of your function invocation in the Lambda console. Additionally, you should also see records getting created in your DynamoDB table for every message that gets published to the SNS topic.
You can also publish a message to a SNS topic to invoke a subscribed Lambda function using the SNS API. Here is an example:
snsClient.publish(topicArn, "example_message");
Checking the delivery status of messages sent to Lambda functions
You can use the delivery status feature in SNS to track success and failure rates of deliveries to Lambda functions. For more information on how to use the delivery status feature, see our documentation.
We look forward to hearing from you about this feature. You can provide feedback using the SNS forum, the Lambda forum or our Twitter account.
相關推薦
Invoking AWS Lambda functions via Amazon SNS
We released a new feature today for Amazon SNS that enables developers to perform custom message handling or publish messages to other AWS servic
Invoking AWS Lambda from Amazon MQ
This post courtesy of Josh Kahn, AWS Solutions Architect Message brokers can be used to solve a number of needs in enterprise architecture
AWS Lambda Functions Made Easy
AWS Lambda Functions Made EasyA Step by Step Guide with Code Snippets for Packing Your Python 2.7 Function for AWS LambdaBackgroundI hope the code in this
Developing .NET Core AWS Lambda functions
This post is courtesy of Mark Easton, Senior Solutions Architect – AWS One of the biggest benefits of Lambda functions is that they isolat
AWS Lambda enables functions that can run up to 15 minutes
Amazon Web Services is Hiring. Amazon Web Services (AWS) is a dynamic, growing business unit within Amazon.com. We are currently hiring So
Use AWS DeepLens to give Amazon Alexa the power to detect objects via Alexa skills
People are using Alexa for all types of activities in their homes, such as checking their bank balances, ordering pizza, or simply listening to th
Encrypting messages published to Amazon SNS with AWS KMS
Amazon Simple Notification Service (Amazon SNS) is a fully managed pub/sub messaging service for decoupling event-driven microservices, distribute
Invoke AWS Lambda when a State (Execution Event) Changes in AWS Step Functions
Before you begin this procedure, you must: Confirm that the event change that you want to trigger the Lambda function i
Amazon Kinesis Firehose Data Transformation with AWS Lambda
Shiva Narayanaswamy, Solution Architect Amazon Kinesis Firehose is a fully managed service for delivering real-time streaming data to des
Pass Custom Headers Through Amazon API Gateway to an AWS Lambda Function
By default, a Lambda function only ingests the request body received by an API created in the API Gateway. To pass custom headers f
AWS Snowball Edge – More Storage, Local Endpoints, Lambda Functions
A I was preparing to write this blog post I went back and read the post I wrote when we launched AWS Snowball last year (AWS Import/Export Snowbal
Better Together: Amazon ECS and AWS Lambda
My colleague Constantin Gonzalez sent a nice guest post that shows how to create container workers using Amazon ECS. — Amazon EC2
Testing Lambda functions using the AWS Toolkit for Eclipse
In this blog post, I will introduce how to test AWS Lambda functions in Eclipse by using the AWS Toolkit for Eclipse. The AWS Toolkit for Eclipse
使用AWS Lambda自動定時快照EBS卷
imp delet def watch host %s date create creat 一、創建IAM策略IAM->策略->創建策略->JSON{ "Version": "2012-10-17",
AWS Lambda重大更新,跨越程式語言差異之門?
北京時間11月30日凌晨,在美國拉斯維加斯召開的AWS re: Invent 2018上,和往年一樣,AWS CTO Werner·Vogels博士又釋出了AWS多項重要的更新,包括資料庫、程式設計工具、架構等多個方面,而在其中,有兩項針對於AWS Lambda的更新引爆了現場眾多開發者的歡呼,
在aws-lambda 中使用xvfb,firefox,ffmpeg
前一段時間,做了一個對瀏覽器錄製推流的映象,由於對資源的要求比較高,所以需要服務動態伸縮。後發現aws的一款免費服務lambda,號稱可以最高一千個例項,並且免費,就嘗試遷移到其上面。研究了幾天,最終效果差強人意,現做個記錄。 1.問題 lambda想要提供的應該只是簡單的服務部署,比如
AWS Lambda整合Sentry
匯入名稱:sentry_sdk.integrations.aws_lambda.AwsLambdaIntegration 可以像下面這樣使用用於Python SDK的AWS Lambda整合: import sentry_sdk from sentry_sdk.integr
Managing a Spotify Library with Go and AWS Lambda
Managing a Spotify Library with Go and AWS LambdaSpotify exposes a robust API that can be used to manage your (or someone elses) music library and do all s
Serverless Backend using AWS Lambda: Hands
Storing Data in DynamoDBBefore we can start storing data in our DynamoDB, we need to set some permissions for the Lambda function to have write access.Insi
AWS Lambda & Serverless Development — Intro
AWS Lambda & Serverless Development — IntroThis is the intro to a two part series to help people new to AWS Lambda and serverless development. This inc