Creating AWS Lambda using java and spring framework



AWS Lambda is another way to use cloud computing on Amazon’s AWS.

It allows you deliver you code and run in production without any server management.

It is auto-scale, high available and you pay only when you are using your function.

Creating the Application

For this basic example, I choose the spring as framework because most of my webservices are created using this framework.

If you are using eclipse, you can install the AWS Toolkit for eclipse. It is very helpful during development and testing stages.

First, you have create the pom.xml file:

<project xmlns="" xmlns:xsi=""





As you can see, the dependencies for the spring framework and aws lambda are defined and the plugin maven-shade-plugin is set in the build section.

Below are the structure and the files used in this project.



<beans xmlns=""
 xmlns:xsi="" xmlns:p=""
 xmlns:aop="" xmlns:context=""
 xmlns:jee="" xmlns:tx=""

<context:component-scan base-package="" />

import org.springframework.context.ApplicationContext;

public class Application {
    private static ApplicationContext springContext = null;
    private static ApplicationContext getSpringContext() {
        if (springContext == null) {
            synchronized (ApplicationContext.class) {
                if (springContext == null) {
                    springContext = new ClassPathXmlApplicationContext("/application-context.xml");
        return springContext;
    public static <T> T getBean(Class<T> clazz) {
        return getSpringContext().getBean(clazz);

In the file is created the ApplicationContext using singleton pattern and the ApplicationContext.getBean is encapsulated in the Application.getBean protecting the application context for been accessed from other classes.


import java.util.Calendar;

public class LambdaFunctionHandler implements RequestHandler<String, String> {
    private BasicSample basicSample;
    public String handleRequest(String input, Context context) {
        basicSample = Application.getBean(BasicSample.class);
        context.getLogger().log("AWS Request ID: " + context.getAwsRequestId());
        context.getLogger().log("Input: " + input + " at " + Calendar.getInstance().getTimeInMillis());
        return basicSample.doSomething(input);

In the the RequestHandler interface is implemented in order to receive the AWS Lambda call.


import org.springframework.stereotype.Component;

public class BasicSample {
    public String doSomething(String input) {
        return "Something has done with the input " + input;

In this file the method doSomething uses the input string and returns a new string.

Deploying on AWS

The AWS Toolkit helps us on this step. The only thing we have to do is open the any .java file, open the context menu (right-click), chose “AWS Lambda” and then “Upload function to AWS Lambda…” option.


You will see the window below:


Chose the “Create a new Lambda function” option, type the name BasicSampleFunction and click “Next”.


In the window above, you must create a IAM role and a S3 Bucket for your function.

You also need to change the Memory to 512MB, because with less memory, the application takes longer during cold start.

Click Finish and wait until your function is deployed.

Uploading function code to Lambda _005

Running the Function

It’s time to test our work. Right click on any .java file, chose “AWS Lambda” and then “Run function on AWS Lambda…” option.


In the window below, type any text you want to pass to your lambda function and click “Invoke”

Lambda Function Input _006

In the first execution, it will take some time to start your application (~ 3 sec).

If everything works fine, you will see the output in the console.


If you want to see the log of your call, you can go to the CloudWatch and click on the Log in the left menu.

Look for your function, the log should be /aws/lambda/FunctionName, click on it and a new window will appear.

Chose the first (or the only) Log Stream to see the log.


This is just a start point for the AWS Lambda, you can try some other frameworks instead of spring.

In the next post I’ll show how to create a API Gateway and call the lamba function.