Automating AWS Lambda Invocations to run every 2 Minutes Using with Amazon EventBridge

2024.06.05

Introduction

Hello, Hemanth from the Alliance Department here. In this blog, I scheduled a Lambda function to run every 2 minutes using Amazon EventBridge.

AWS

Is a secure cloud service platform that offers compute power, database storage, content delivery, network, and other functionality to help businesses scale and grow. It is one of the first cloud vendors to start services in the year 2006. It offers all the 3 service models namely IAAS, PAAS, and SAAS. Some of the notable domains in AWS are Compute, Migration, Storage, Network and Content Delivery, Management Tools, Database, Messaging, Security and Identity Compliance, and many more.

AWS Lambda

A serverless compute service which runs code as a reply to events and automatically takes care of the bottom resources. It runs code on high availability compute infrastructure and performs all the administration of the compute resources. A few examples are HTTP requests via Amazon API Gateway, changes to objects in S3, and many others.

Amazon EventBridge

A serverless event bus solution that makes it simple to link apps by utilizing data from several sources. By directing events from sources like AWS services, SaaS apps, and bespoke apps, you may create event-driven architectures. By making event intake, filtering, and delivery simpler, EventBridge makes it possible to create decoupled, scalable system designs. It facilitates archiving, schema discovery, and customized event buses. Rules can be configured to match incoming events and direct them to target services such as Step Functions, AWS Lambda, or other AWS offerings. With integrated retry logic and cross-account event delivery, EventBridge also guarantees dependable event delivery.

Demo

Create a Lambda Function

Go to the AWS Management Console and navigate to AWS Lambda and click on "Create Function". Provide a function name, leave other settings as default, and click "Create function." The Lambda function is successfully created.

Add a Trigger Using EventBridge

In the Lambda function, click on "Add trigger". img In the source section, select "EventBridge (CloudWatch Events)". Choose to create a new rule. Provide a rule name and set the schedule expression to cron(0/2 * * * ? *) to run every 2 minutes. Click "Add". The trigger is successfully created. In the trigger section of configuration tab, you can see the new rule and related information.

Monitor Lambda Invocations

Go to the "Monitor" tab in the Lambda function page to observe the function's invocations. Now from images below it can be observed that lambda was invoked twice, once at 4.16 UTC and other 4.18 UTC From the above, we can confirm that the Lambda function is triggered every 2 minutes as scheduled.

Conclusion

Using Amazon EventBridge to schedule a Lambda function is a great method to manage recurring workloads and automate applications. This integration makes it possible to create scalable, easily managed event-driven architectures. You may effectively set up a Lambda function to run on a regular basis, guaranteeing your applications stay responsive and effective, by following the instructions provided in this article.