How To: SQS Destination For Lambda (4 Min) | AWS | Asynchronous Invocation Using Event Bridge

preview_player
Показать описание
In this tutorial, you'll learn how to invoke Lambda function asynchronously using Event Bridge and set SQS as the destination for both success and failure conditions.


Video Transcript:
_
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to create an SQS destination for a Lambda function in AWS. Let's start by navigating to the SQS service and then create two new cues. The first one is called My Success SQSQ and the second one is called my failure SQSQ. 

I'm going to leave all the configuration settings to default while creating them. Let's navigate to the Lambda service then click on create function and give it a name. Select Python for runtime then click on create function and scroll down to the code Source section. 

Click on test and give your test event a name then hit save. Click on the test again to execute your Lambda function. You should see hello from Lambda as the response body which means the Lamba function was successfully executed. 

Click on ADD destination in the function overview section then select asynchronous invocation for source and on success for condition. Choose SQSQ for the Destination type and my success SQSQ as the destination then hit save. Click on ADD destination one more time and this time choose failure for condition and my failure SQSQ for Destination then hit save again. 

Now whenever our Lambda function is invoked asynchronously, the event will either show up in the My Success SQSQ or my failure SQSQ depending on whether the function was executed successfully or not. Next, to invoke our Lambda function asynchronously, let's leverage the event bridge service. Click on create rule then give it a name. 

Select schedule for role type then clicks on continue to create a rule for schedule pattern. Choose a rate expression of one minute then hit next. Select Lambda for the Target type then choose my testing function and hit next. 

Click on create rule then navigate back to the Lambda window and hit refresh. Since our Lambda function will get triggered every minute by the event bridge service, you should now see it in the function overview section as well. Next, let's navigate back to the My Success SQS queue and click on send and receive messages. 

Click on Poll for messages and you should see a successful event show up in this queue from our Lambda function. Next, let's go back to the code Source section of our Lambda function. Here I'm going to add a raise exception statement server Lambda function fails upon execution. 

Hit deploy then clickon test to confirm that our Lambda function does fail on execution. Now, let's go back to my failure SQSQ and click on send and receive messages. Hit Poll for messages then gives it a few minutes for the event bridge service to execute our Lambda function asynchronously. 

Once the message arrives, click on it to confirm that it contains the raise exception message that we created. There you have it. Make sure you like, subscribe, and turn on the notification bell. 

Until next time.
Рекомендации по теме