AWS Lambda: Usecase with RDS and EventBridge

Dayanand Chinchure
5 min readOct 12, 2019

--

Serverless, pay as you use, smart way to utilize resources and many more. That’s how we all have introduced to the AWS Lambda. I am totally impressed by the idea and simplicity of lambda implementation. So let’s start.

What is AWS Lambda?

AWS Lambda is a computing platform for many application scenarios, provided that you can write your application code in languages supported by AWS Lambda, and run within the AWS Lambda standard runtime environment and resources provided by Lambda. (Source: HERE)

Few things to focus on the above sentence as, “computing platform” and “runtime environment”. After reading this, serverless doesn’t make sense in the first go as compute platform and runtime require some sort of deployment and hosting to work altogether. So AWS lambda is with the server but for some time and that’s pay as you use for you. Implementation wise everything you required is placed under the hood and called when needed.

Usecase: Run queries on the AWS RDS whenever the database instance starts.

NB: I have used AWS RDS as a database service and AWS EventBridge as event trigger to trigger my lambda function. Let’s understand the implementation of the same below.

Implementation of AWS Lambda?

AWS Provided good developer guide. Please use the link to set up AWS Lambda on your account HERE. By now you are done with setting up the lambda. Now let’s understand how it’s working.

Let’s understand what is application, Function, and Layer. I will put the snippet of each after the description.

Application: An AWS Lambda application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. You can use AWS CloudFormation and other tools to collect your application’s components into a single package that can be deployed and managed as one resource. Applications make your Lambda projects portable and enable you to integrate with additional developer tools, such as AWS CodePipeline, AWS CodeBuild, and the AWS Serverless Application Model command-line interface (SAM CLI). Source : HERE

AWS Lambda application details

Function: A Lambda function consists of code and any associated dependencies. In addition, a Lambda function also has configuration information associated with it. Initially, you specify the configuration information when you create a Lambda function.

AWS Lambda function details

Few things to note here, you can provide runtime in the function. In my case I have selected python 3.6 as a runtime because I have written my function in python. Also Action and Test are add-ons provided by AWS to test the function there itself. You can also add your environment variables here. As I am calling AWS RDS service I have used mariadb connection details in the environment variables

AWS Lambda function Environment Variables

Layers: You can configure your Lambda function to pull in additional code and content in the form of layers. A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package.

AWS Lambda layer details

By now, you have created AWS lambda function in python to connect to the database and fire a query. But now the questionaries, how will my lambda function will know it has to get executed. There must be some connection between my RDS and Lambda function, that connection we can establish using the AWS service called AWS EventBridge.

What is AWS EventBridge?

EventBridge is a serverless event bus that makes it easy to connect applications together using data from your own applications, Software-as-a-Service (SaaS) applications, and AWS services. Source: HERE

AWS Eventbridge

Let’s configure the AWS EventBridge :

EventBridge does provide good configuration settings like setting up the Event buses, rules, and partner event sources. More details HERE. In rule you can specify triggers and trigger actions. For example, I want to create a “test” rule on AWS RDS service and select the trigger as a database event.

AWS EventBridge rule

Once pattern and trigger is defined then select the target as implemented lambda

AWS EventBridge target

By now you are done with setting up the Lambda function to run some query on AWS RDS and setting up the trigger in EventBridge. Now onwards whenever the database instance is started, Eventbridge will trigger the lambda and lambda will run the query on database.

Summary: There can be multiple use cases for AWS Lambda. We have to be very particular while selecting the lambda service for such use cases. Cause lambda do come with some limitations (link HERE). Keeping all these things in mind, lambda is turning out as gem for AWS as it’s been a perfect fit for most of the use cases. For example, Buy Now button on the amazon site is not heavily used, so to rather waste my resources there I will trigger lambda for that buy now option. :)

--

--