A Serverless REST API built using Spring Boot 3, AWS Lambda, and API Gateway, deployed with Terraform and tested locally using LocalStack.
This project demonstrates how to deploy a Java Spring Boot application as an AWS Lambda function integrated with API Gateway.
This demo showcases:
- ☁️ Serverless Spring Boot REST API running on AWS Lambda
- 🌐 API Gateway integration using Terraform
- 🧰 Local testing with LocalStack
Spring Boot 3.5.7 · Java 21 · Terraform · Docker/Podman · LocalStack · AWS Lambda · API Gateway · S3
Install the following:
- Java 21
- Maven
- Terraform
- Docker or Podman
- LocalStack CLI (
pip install localstack)
docker-compose up -dmvn clean packageGenerates the build at:
target/springboot-aws-lambda-api-gateway-1.0-SNAPSHOT.zip
tflocal init
tflocal plan -var-file="terraform.tfvars"
tflocal apply -var-file="terraform.tfvars" -auto-approve
terraform init
terraform plan -var-file="terraform.tfvars"
terraform apply -var-file="terraform.tfvars" -auto-approveCheck the output URL from Terraform and invoke:
curl http://localhost:4566/restapis/<api_id>/prod/_user_request_/health
curl https://n19xfyl2d4.execute-api.ap-south-1.amazonaws.com/prod/health| Command | Description |
|---|---|
mvn clean package -Pshaded-jar |
Builds a fat JAR (single executable JAR) using the shaded profile. This includes all dependencies in one JAR, ideal for AWS Lambda deployments. |
mvn clean package -Passembly-zip |
Builds a ZIP package (containing compiled classes + dependencies in /lib) using the assembly profile. Useful when Lambda expects a ZIP deployment artifact. |
awslocal lambda invoke --function-name SpringBootLambdaFunction out.json |
Invokes your deployed Lambda function on LocalStack and writes the response to the file out.json. Helps verify Lambda works before API Gateway setup. |
awslocal logs tail /aws/lambda/SpringBootLambdaFunction --follow |
Streams real-time Lambda logs from LocalStack to your terminal, similar to aws logs tail. Useful for debugging function initialization and runtime errors. |
tflocal init |
Initializes the Terraform environment for LocalStack, downloading necessary providers and setting up the workspace. |
tflocal plan -var-file="terraform.tfvars" |
Generates and displays an execution plan showing which resources Terraform will create or modify in LocalStack. |
tflocal apply -var-file="terraform.tfvars" -auto-approve |
Applies the Terraform configuration and deploys all defined AWS resources (Lambda, API Gateway, S3, IAM) into LocalStack automatically without confirmation prompts. |
tflocal import aws_iam_role.lambda_exec_role lambda_exec_role |
Imports an existing IAM role into Terraform’s state file. Useful if the role already exists (prevents “EntityAlreadyExists” errors). |
| **`jar tf target/springboot-aws-lambda-api-gateway-1.0-SNAPSHOT.zip | grep StreamLambdaHandler`** |
✅ Spring Boot AWS Lambda API Gateway running successfully on LocalStack and AWS!