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.



7 thoughts on “Creating AWS Lambda using java and spring framework

  1. Howard

    Is there anything that would prevent the spring context from being loaded on every call to this lambda function? If not or if the context needs to be loaded frequently, the response time will not be good right? Is there some way to reduce the impact of spring context loading in a lambda function call? Does it even make sense to use spring in a lambda function? I suppose your spring context could be smaller and lighter since you would be focused on a single api, but it seems to me that use of the spring context makes more sense for a full application server or batch job.


  2. Niraj


    Somehow I am getting below error while running above example on AWS. Any idea?

    errorMessage”: “Line 12 in XML document from class path resource [application-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 113; cvc-elt.1: Cannot find the declaration of element ‘beans’.”,
    “errorType”: “org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException”,


    • Are you using the same version of spring as me? Also, did you change de xsd confing in the application-context.xml?

      Make sure that the parameter xsi:schemaLocation is the same as below:



  3. Abdullah Al Ahad

    What about the latest version of spring where we do not use .xml file rather we use annotation based configuration file. How will be the context in that case?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s