In this video you will learn how to create a AWS Lambda function to handle messages that are being sent to an Amazon SNS topic.
Full playlist: • Hands-on deep-dive into Amazon SNS
TIMESTAMPS:
0:00 - Quick introduction
0:29 - Demo: Create a new Lambda function to handle SNS messages
2:04 - Demo: Test the Lambda function with an example event
2:46 - Demo: Find the logs of the Lambda function
3:00 - Demo: Trigger the Lambda function with a real SNS message
VIDEO TRANSCRIPT:
In this video I'm going to show you how to create a Lambda function to automatically handle SNS messages. There are many use cases - like application-to-application messaging, microservices integration, ops automation, incident detection and response - that can benefit from automation downstream whenever a specific event occurs. And this is where Amazon SNS and AWS Lambda come together. I'll show you how to create a Lambda function that executes whenever there's a new message on our topic.
I'm already in the AWS Lambda console and I'm gonna click on functions. Right now, we don't have any functions, so lets create a new one. For this function, I'm going to use a blueprint. There are dozens of blueprints, and I'm going to filter it down by SNS. This returns two similar blueprints to process messages pushed to an SNS topic. One using Node.js, and the other one using Python. Let's go with Python.
We need a function name - this is going to be the entry point of our billing service - add the SNS trigger - I'm going to select the Events topic that we want to subscribe to - and Lambda will automatically add the necessary permissions for SNS to invoke this Lambda function. Here's the blueprint code, but I'll go into this in detail in a second. For now, lets just click on Create function. This will take a few seconds to create the necessary permissions and deploy the new function.
Here we go. Let's have a look at the function code: We don't need this, let's first print out which service this is whenever the function is invoked, we also don't need this, but we do want to get the message subject from the event - I'll show you an example of the entire event in just a minute. We don't want to return anything, we'll just print the subject and the message to the log.
Lets deploy the changes, scroll up and open the test tab. Right here you can see a sample event. This is exactly, what an SNS message event looks like. We have Records - usually just one - inside this record we have the SNS message itself, with all kinds of attributes, including the subject, and the message. Lets give the event a name, save, and test.
Now lets look at the execution result. I'm going to open up the details, and down here in the log output we can see the subject and the message of our test event. You can also open up the log group in the CloudWatch console by clicking on "logs". This is where you'll find all the logs of your billing service. Lets have a look at the function's log stream, and here you can see the exact same output. The subject and the message from before.
Now lets test this with a real SNS message. I'm going to open the SNS dashboard, go to topics, open our Events topic, click on Publish message, give it a subject, like "Hello, World", the message itself, and click on Publish message. Now lets go back to the CloudWatch log and see if we have any new entries. Here we go: The message has automatically triggered the Lambda function, which in turn wrote the subject and the message into the log.
I've just shown you how to create a new Lambda function triggered by SNS using a blueprint. In the next video, we're going to look at an existing Lambda function and how to hook it up to the topic. Make sure to like this video and subscribe so you'll get notified when the next one comes out.
#AWS #In5MinutesOrLess
Смотрите видео How to create a Lambda function to handle SNS messages онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Dennis Traub 21 Сентябрь 2022, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,458 раз и оно понравилось 35 людям.