Skip to content

atulagrawal/aws-lambda-java-hello

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Lambda Java Hello World (Java 21)

A minimal, production-ready "Hello World" AWS Lambda written in Java 21 with API Gateway, structured logging, unit tests, and an AWS SAM template for deployment.

Prerequisites

  • Java 21 (JDK)
  • Maven 3.9+
  • AWS CLI configured (aws configure)
  • AWS SAM CLI 1.100+

Project Structure

  • src/main/java/com/example/hello/HelloHandler.java — Lambda handler using API Gateway proxy integration
  • src/main/java/com/example/hello/Json.java — minimal JSON helper using Jackson
  • src/test/java/com/example/hello/HelloHandlerTest.java — JUnit tests
  • template.yaml — AWS SAM template (runtime java21)
  • events/hello.json — sample API Gateway event for local testing
  • pom.xml — Maven build with shading for deployable artifact

Build

mvn -q -DskipTests package

The deployable artifact will be in target/lambda-hello-1.0.0-shaded.jar.

Test

mvn -q test

Local Invoke (SAM)

Build with SAM (uses Maven under the hood):

sam build

Invoke locally with the sample event:

sam local invoke HelloFunction -e events/hello.json

Start a local API (hot-reload-like loop):

sam local start-api

Then open: http://127.0.0.1:3000/hello?name=Atul

Deploy (SAM)

First deploy (guided):

sam deploy --guided

Recommended choices during guided deploy:

  • Stack Name: lambda-java-hello
  • Region: your preferred region (e.g., us-east-1)
  • Confirm changes before deploy: y
  • Save arguments to samconfig.toml: y

Subsequent deploys become just:

sam build && sam deploy

After deploy, SAM outputs ApiUrl. Example:

https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/Prod/hello

Try: ?name=YourName

Configuration

  • Environment variable GREETING controls the greeting prefix. Default is Hello.
    • Set in template.yaml under Globals -> Function -> Environment -> Variables.

Logging

  • Uses SLF4J (org.slf4j:slf4j-simple) for structured logs.
  • Also writes a short line to the LambdaLogger for quick timing visibility.
  • View logs via CloudWatch Logs or locally when using sam local.

Handler Signature

  • Handler: com.example.hello.HelloHandler::handleRequest
  • Input: APIGatewayProxyRequestEvent
  • Output: APIGatewayProxyResponseEvent

Notes

  • If your IDE shows package mismatch errors like main.java.com..., import the project as a Maven project. The source layout here is standard Maven (src/main/java and src/test/java).
  • Java 21 runtime is specified in the SAM template (java21). Ensure your AWS region supports it.

Cleanup

To remove deployed resources:

aws cloudformation delete-stack --stack-name lambda-java-hello

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published