Create AWS Lambda with ease

Hi readers, this is my second blog on AWS lambda, I covered the basics in my previous blogs. Now we will focus on the implementation part. Before starting, please keep a check on these things:

Prerequisite

All set let’s start.

1) Configure lambda function on our AWS console.

Log in to the AWS account and go to AWS lambda console. You will get a basic lambda created with the hello world example. But we will create a new one so click on the create lambda function and fill in the necessary details and click on create function.

Now, you will get a page to configure the lambda. The first tab to set up the triggers for the function.

Lambda-Designer

We get many triggers that can be set for a lambda function like setting an API gateway or an event triggered when we push something to the S3 bucket. Following are the triggers available:

You can explore each one of them by click on it. In our example, we will use API gateway to interact with our lambda. So in order to create a new trigger click the API gateway option under add triggers.

2) Create a project

Now let’s create a java code that we want to run on our lambda. You can get the below code from my repository

Let me highlight the main part of this code i.e the handleRequest function, which will handle the request coming in. It contains two parameters requestEvent and the context. requestEvent will bring in the request data which we will pass when we hit our lambda with the API gateway as a JSON body. The context object allows you to access useful information available within the Lambda execution environment like the memory limit, request id etc.

We are responding back with the APIGatewayProxyResponseEvent object where we set the body part and the status code.

3) Create a jar of the project and upload to the lambda

mvn clean package

Then upload the jar or zip file in the function code tab on the AWS lambda console and also we need to set the handler for the class on the consolepackage.class::method-reference format.

Function-Code

4) Hit the API Gateway

Now we are ready with all the configurations and now let’s hit our lambda with the postman. Keep the request JSON body as:

{

“message” : “Hello Coders”

}

Wow, we got the response.

{
“status”: “Success”,
“message”: “Got Hello Coders!!”
}

Yeah, we have successfully completed our demo.

Try it yourself.

Thanks for your valuable time.

Reference:


knoldus-advt-sticker

Written by 

I am a Software Consultant at Knoldus Inc. I am a Scala Enthusiast. I am familiar with Object Oriented Programming Paradigms, and has also worked upon .NET based technologies. Aside from being a programmer, I am familiar with NoSQL database technologies such like Cassandra. I also worked on Lagom microservice architecture.

Leave a Reply

%d bloggers like this: