AWS Serverless Using Java:
AWS Lambda is a serverless feature provided by AWS. Which means there is no server needed to run this application. Well, in reality there is a server which runs in the background, but it does not run the whole time, it runs based on the requests, if a request comes, the application is loaded to the background server and is ran. Then after the response is completed it is offloaded from the server. This process continues every time a request comes.
What this article is for ?
In this article we will create a AWS Lambda function using Java Maven project. We will create that in our local environment and then deploy the same to AWS envrionment.
Business requirements of Lambda:
- You want to run something the moment an AWS API Gateway is hit with a request.
- You want to call another service the moment when an object is loaded to an S3 bucket.
- During weekends you want to shutdown your servers & start the same during weekdays automatically.
The benefit of Lambda is that you pay only for the execution time and not for the idle time of the applications.
Steps to create a AWS Lambda function:
- Create a maven java project (not a Springboot project).
- Add Lambda dependency
- Add Maven Shade plugin (to create a fat jar)
- Write code for Lambda function.
- clean package it
- Deploy to AWS
Below are the detailed steps:
Create a Maven java project.
Add below dependency to pom.xml:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-core</artifactId> <version>1.2.0</version> </dependency>
Add the maven shade plugin in pom.xml like below to make it a fat jar.
What is a fat jar ?
A fat jar = my own source code + dependency
<build> <resources> <resource> <directory>src/main/java/resources</directory> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.2</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <!-- <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>aws</shadedClassifierName> --> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Create a Request Handler class as below:
package com.heapsteep; import com.amazonaws.services.lambda.runtime.RequestHandler; public class App implements RequestHandler<Object,Object>{ public Object handleRequest(Object input, Context context) { System.out.println("==============> input: " + input);//This will be printed in the log and also in CloudWatch log. return "*******> Returned value from Heapsteep"; } }
We have implemented the handleRequest() method from RequestHandler interface.
Do Maven Build by calling this command: clean package
To do the above, right click the project -> Run As -> Maven Build… -> clean package -> Apply -> Run
Once the build is success, you can see the message in the log stating the path where the jars are created.
If you go to the path you can see 2 jars, the jar with more size is the fat jar.
We have to upload this fat jar to the AWS Lambda console.
Log in to your AWS account and follow the below sequence of steps:
AWS Console -> Lambda -> Create function -> Give a name -> Choose any java:
You need to have a IAM Role for the Lambda having “Lambda Full Access” (or whatever access you want to give). I am already having one IAM Role, so I am selecting it:
Choose create function.
Go to “Code” tab -> Upload From -> .zip or .jar file -> choose the fat jar.
Go to “Runtime Settings” section -> Edit
Give the Handler name as below:
com.heapsteep.App::handleRequest
Go to Test tab ->
Test the lambda function by passing anything(as its not using the input parameter). You will see the statements printed in the console.
That’s it…
You have successfully deployed a Lambda function to AWS.
Source code of the above code: https://github.com/heapsteep/lambda-maven
If you want to see an usage of Lambda you can read the below article:
How to Start/Stop AWS ECS automatically using Java Spring
You didn’t got what you are looking for ? You can hire me for your work at $10/hr.
Do drop a mail to below id with your problem statement, I will contact you within 1 hr:
[email protected]
Working hours: 7:00 am to 5 pm GMT