Over a million developers have joined DZone.

Java on AWS Using Lambda

DZone 's Guide to

Java on AWS Using Lambda

A practical guide to deploying Java Lambdas in AWS

· Cloud Zone ·
Free Resource

Amazon Web Services gets more popular by the day. Java is a first-class citizen on AWS, and it is pretty easy to get started.

Deploying your application is a bit different, but still easy and convenient.

AWS Lambda is a compute service where you can upload your code to AWS Lambda and the service can run the code on your behalf using AWS infrastructure. After you upload your code and create what we call a Lambda function, AWS Lambda takes care of provisioning and managing the servers that you use to run the code.

Actually, think of Lambda as running a task that needs up to five minutes to finish. In case of simple actions or jobs that are not time-consuming — and don’t require a huge framework — AWS Lambda is the way to go. Also, AWS Lambda is great for horizontal scaling.

The most stripped down example would be to create a Lambda function that responds to a request.

We shall implement the RequestHandler interface.

package com.gkatzioura.deployment.lambda;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import java.util.Map;
import java.util.logging.Logger;

 * Created by gkatzioura on 9/10/2016.
public class RequestFunctionHandler implements RequestHandler<Map<String,String>,String> {

    private static final Logger LOGGER = Logger.getLogger(RequestFunctionHandler.class.getName());

    public String handleRequest(Map <String,String> values, Context context) {

        LOGGER.info("Handling request");

        return "You invoked a lambda function";


Somehow RequestHandler is like a controller.

To proceed we will have to create a JAR file with the dependencies needed, therefore we will create a custom Gradle task:

apply plugin: 'java'

repositories {

dependencies {
    compile (

task buildZip(type: Zip) {
    from compileJava
    from processResources
    into('lib') {
        from configurations.runtime

build.dependsOn buildZip

Then we should build:

gradle build

Now we have to upload our code to our Lambda function.

I have an S3 bucket on Amazon for Lambda functions only. Supposing that our bucket is called lambda-functions (I am pretty sure it is already reserved).

We will use AWS CLI wherever possible.

aws s3 cp build/distributions/JavaLambdaDeployment.zip s3://lambda-functions/JavaLambdaDeployment.zip

Now instead of creating a Lambda function the manual way, we are going to do so by creating a cloud formation template.

    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "LF9MBL": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Code": {
                    "S3Bucket": "lambda-functions",
                    "S3Key" : "JavaLambdaDeployment.zip",
                "FunctionName": "SimpleRequest",
                "Handler": "com.gkatzioura.deployment.lambda.RequestFunctionHandler",
                "MemorySize": 128,
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "66b2b325-f19a-4d7d-a7a9-943dd8cd4a5c"

The next step is to upload our CloudFormation template to an S3 bucket. Personally, I use a separate bucket for my templates. Supposing that our bucket is called CloudFormation-templates

aws s3 cp cloudformationjavalambda.template s3://cloudformation-templates/cloudformationjavalambda.template

Next step is to create our CloudFormation stack using the template specified:

aws cloudformation create-stack --stack-name JavaLambdaStack --template-url https://s3.amazonaws.com/cloudformation-templates/cloudformationjavalambda.template

In order to check we shall invoke the lambda function through the amazon CLI:

aws lambda invoke --invocation-type RequestResponse --function-name SimpleRequest --region eu-west-1 --log-type Tail --payload '{}' outputfile.txt

And the result is the expected:

"You invoked a lambda function"

You can find the source code on GitHub.

functions ,aws lambda ,cloud ,java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}