A Simple AWS Lambda Function in Java

AWS Lambda is a service that lets you run code without provisioning or managing servers. In this tutorial, I will share with you how to create a very simple Lambda function in Java and run it on AWS.


To be able to follow this tutorial, you will need to have the following:

  • An AWS account,
  • AWS CLI (Command Line Interface) installed and configured on your machine. You will need it to deploy your Lambda function to AWS,
  • Java 8 or later installed on your machine,
  • Apache Maven working on your computer. Install Maven if you do not have it.

Step 1: Create a new project

The very first step will be to create a new project. To do that, you can use your favourite Java development environment, or you can use Maven to create a new Java project.

Step 2. Add AWS SDK for Java

Now that you have created a new maven-based Java project open its pom.xml file and add the following dependency.


This will add AWS Lambda support to your Java project.

Step 3. Create AWS Lambda Function

Now that we have added AWS Lambda SDK for Java to our project, we can create a very simple Lambda function.

Create a new class in your project. This class will be the entry point for your Lambda function. The class should implement the RequestHandler interface provided by the AWS SDK. The RequestHandler interface has a single method called handleRequest, which takes in two arguments: an input object and a context object. Here’s an example of a simple Lambda function class:

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

public class SimpleLambdaFunction implements RequestHandler<String, String> {
    public String handleRequest(String input, Context context) {
        return "Hello, " + input + "!";

Step 4. Build Project

You can now build your project and package it into a JAR archive. You can do this by running the following Maven command in the root directory of your project:

mvn package

This above command will build your project and package it into a single JAR archive. You can deploy this JAR archive to AWS next.

Step 5: Deploy your Lambda function to AWS

You can now deploy your Lambda function to AWS. You can do this by using the AWS CLI command aws lambda create-function.

aws lambda create-function --function-name SimpleLambdaFunction --runtime java8 --role <your-role-arn> --handler SimpleLambdaFunction::handleRequest --zip-file fileb://target/simple-lambda-function.jar

Notice that the above command requires a Role ARN to be provided. You can find your Role ARN in the IAM management console.

Step 6: Invoke your Lambda function

You can now invoke your Lambda function and see if it works. To do that, run the aws lambda invoke command in the terminal window on your computer.

aws lambda invoke --function-name SimpleLambdaFunction --payload '"World"' output.txt

And that’s it! You have just created and deployed a simple AWS Lambda function in Java.

Note: You can also use AWS Management Console to create, deploy and test Lambda functions.

I hope this tutorial was helpful to you. If you are interested in Amazon Web Services, check out other AWS Lambda tutorials that I have.