Skip to content

Lambda Java

Building serverless applications with Java and AWS Lambda is a powerful way to create scalable, cost-effective, and event-driven systems.


AWS Lambda is a serverless computing service that runs your code without requiring you to manage servers. You only pay for the compute time you consume.

  • No server management
  • Automatic scaling
  • Pay-per-use pricing
  • High availability
  • Event-driven architecture
  • REST APIs
  • Data processing
  • Automation scripts
  • Real-time file processing
  • Scheduled tasks
  • Chatbots and AI integrations

In a serverless model, AWS manages infrastructure while you focus on writing code. Applications run in response to events.

Example Workflow:

  1. A user uploads a file to S3.
  2. S3 triggers an AWS Lambda function.
  3. The function processes the file and stores results in DynamoDB.

Before you begin, ensure you have:

  • Java 11, 17, or 21
  • AWS Account
  • AWS CLI installed and configured
  • Maven or Gradle
  • IntelliJ IDEA or Eclipse
  • Basic knowledge of Java and REST APIs

Verify installations:

Terminal window
java -version
mvn -version
aws --version

Configure AWS CLI:

Terminal window
aws configure

Terminal window
mvn archetype:generate \
-DgroupId=com.example.lambda \
-DartifactId=hello-lambda \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

Update your pom.xml:

<dependencies>
<!-- AWS Lambda Core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.3</version>
</dependency>
<!-- Lambda Events -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.4</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Build the project:

Terminal window
mvn clean package

6. Creating Your First Java Lambda Function

Section titled “6. Creating Your First Java Lambda Function”
package com.example.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class HelloLambda implements RequestHandler<String, String> {
@Override
public String handleRequest(String input, Context context) {
return "Hello, " + input + "!";
}
}

Handler Name:

com.example.lambda.HelloLambda

A handler is the entry point for your Lambda function.

public class MyHandler implements RequestHandler<InputType, OutputType> {
public OutputType handleRequest(InputType input, Context context) {
return output;
}
}
public class StreamHandler implements RequestStreamHandler {
public void handleRequest(InputStream input, OutputStream output, Context context)
throws IOException {
String response = "Hello from Stream Lambda!";
output.write(response.getBytes());
}
}

  • Go to AWS IAM
  • Create a role with AWSLambdaBasicExecutionRole
  1. Open AWS Lambda Console.

  2. Click Create Function.

  3. Select Author from Scratch.

  4. Choose:

    • Runtime: Java 17
    • Architecture: x86_64
  5. Upload the JAR from:

    target/hello-lambda-1.0-SNAPSHOT.jar
  6. Set handler:

    com.example.lambda.HelloLambda

Use this test input:

"World"

Output:

"Hello, World!"

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import java.util.Map;
public class ApiHandler implements RequestHandler<
APIGatewayProxyRequestEvent,
APIGatewayProxyResponseEvent> {
@Override
public APIGatewayProxyResponseEvent handleRequest(
APIGatewayProxyRequestEvent request,
Context context) {
String name = request.getQueryStringParameters().getOrDefault("name", "Guest");
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withHeaders(Map.of("Content-Type", "application/json"))
.withBody("{\"message\":\"Hello, " + name + "!\"}");
}
}

Example API Call:

https://api-id.execute-api.region.amazonaws.com/prod?name=John

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;
public class S3Handler implements RequestHandler<S3Event, String> {
@Override
public String handleRequest(S3Event event, Context context) {
event.getRecords().forEach(record -> {
String bucket = record.getS3().getBucket().getName();
String key = record.getS3().getObject().getKey();
context.getLogger().log("File uploaded: " + bucket + "/" + key);
});
return "Processed S3 Event";
}
}

Set environment variables in the AWS Console.

String dbUrl = System.getenv("DB_URL");

context.getLogger().log("Processing request...");
  • Amazon CloudWatch Logs
  • AWS X-Ray
  • Amazon CloudWatch Metrics

  • Use Java 17 or 21
  • Minimize dependencies
  • Enable SnapStart (for Java)
  • Increase memory to improve CPU performance
  • Reuse objects outside the handler
private static final ObjectMapper mapper = new ObjectMapper();
  • Follow least privilege principle
  • Use IAM roles instead of credentials
  • Store secrets in AWS Secrets Manager
  • Optimize execution time
  • Use appropriate memory allocation
  • Avoid unnecessary logging

Terminal window
brew install aws-sam-cli
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.example.lambda.HelloLambda
Runtime: java17
CodeUri: target/hello-lambda-1.0-SNAPSHOT.jar
MemorySize: 512
Timeout: 10
Events:
ApiEvent:
Type: Api
Properties:
Path: /hello
Method: get
Terminal window
sam build
sam deploy --guided

hello-lambda/
│── src/main/java/com/example/lambda/
│ ├── HelloLambda.java
│ ├── ApiHandler.java
│ └── S3Handler.java
│── src/main/resources/
│── pom.xml
│── template.yaml
└── README.md

Java Lambdas may experience cold starts due to JVM initialization.

  • Use AWS Lambda SnapStart
  • Use Provisioned Concurrency
  • Reduce dependency size
  • Prefer lightweight frameworks like Micronaut or Quarkus

FrameworkBenefit
Spring BootEnterprise-grade applications
Spring Cloud FunctionSimplified Lambda integration
MicronautFast startup and low memory usage
QuarkusOptimized for serverless and GraalVM

Serverless REST API

  • API Gateway → Lambda → DynamoDB → CloudWatch

File Processing Pipeline

  • S3 → Lambda → SNS/SQS → DynamoDB

Scheduled Jobs

  • EventBridge → Lambda → RDS

  • Java basics
  • AWS fundamentals
  • Write simple Lambda functions
  • Deploy using AWS Console
  • API Gateway integration
  • DynamoDB interactions
  • AWS SAM and CI/CD
  • Error handling and logging
  • Microservices with Spring Cloud Function
  • SnapStart and performance tuning
  • Event-driven architectures
  • Infrastructure as Code with Terraform

Terminal window
# Build project
mvn clean package
# Invoke Lambda locally
sam local invoke
# Start local API
sam local start-api
# Deploy
sam deploy --guided
# View logs
sam logs -n HelloFunction --stack-name <stack-name> --tail

  • AWS Lambda Documentation
  • AWS Free Tier
  • AWS Workshops
  • AWS GitHub Samples

Java with AWS Lambda enables you to build scalable, secure, and cost-efficient serverless applications. By mastering event-driven development, integrating AWS services, and applying best practices, you can create production-grade cloud solutions.