A simple Spring Boot application that demonstrates how to invoke AWS Lambda functions and fetch data from them.
- Project Overview
- Prerequisites
- AWS CLI Installation
- AWS Configuration
- Lambda Function Setup
- Spring Boot Setup
- Running the Application
- API Endpoints
- Testing
- Architecture
This project demonstrates:
- β Installing and configuring AWS CLI locally
- β Creating AWS Lambda functions
- β Spring Boot application invoking Lambda functions
- β Fetching data from Lambda using Spring Boot
- β RESTful API endpoints to interact with Lambda
- Java 17 or higher
- Maven 3.6+
- AWS Account (free tier is sufficient)
- Internet connection for AWS API calls
Option 1: Using MSI Installer (Recommended)
-
Download AWS CLI MSI installer:
https://awscli.amazonaws.com/AWSCLIV2.msi -
Run the downloaded MSI installer
-
Follow the installation wizard
-
Verify installation:
aws --version
Should output:
aws-cli/2.x.x Python/3.x.x Windows/...
Option 2: Using Command Line
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msiUsing Homebrew:
brew install awscliUsing Package:
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /Ubuntu/Debian:
sudo apt update
sudo apt install awscliUsing Python pip:
pip install awscli --upgrade --userUsing Binary:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/installaws --version- Log in to AWS Console
- Go to IAM (Identity and Access Management)
- Click Users β Add User
- Create user with Programmatic access
- Attach policy:
AWSLambdaFullAccess - Save your:
- Access Key ID
- Secret Access Key
aws configureEnter your credentials:
AWS Access Key ID: YOUR_ACCESS_KEY_ID
AWS Secret Access Key: YOUR_SECRET_ACCESS_KEY
Default region name: us-east-1
Default output format: json
aws sts get-caller-identityThis should show your AWS account details.
Create file at:
- Windows:
C:\Users\USERNAME\.aws\credentials - Mac/Linux:
~/.aws/credentials
[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEYCreate config file at:
- Windows:
C:\Users\USERNAME\.aws\config - Mac/Linux:
~/.aws/config
[default]
region = us-east-1
output = json-
Go to AWS Lambda Console:
https://console.aws.amazon.com/lambda -
Create Function 1: Simple Hello
- Click "Create function"
- Choose "Author from scratch"
- Function name:
helloWorldFunction - Runtime: Python 3.9
- Click "Create function"
-
Add Code:
import json def lambda_handler(event, context): name = event.get('name', 'Guest') return { 'statusCode': 200, 'body': json.dumps({ 'message': f'Hello {name} from AWS Lambda!', 'timestamp': context.request_id }) }
-
Create Function 2: Get User Data
- Function name:
getUserDataFunction - Runtime: Python 3.9
- Code:
import json from datetime import datetime def lambda_handler(event, context): name = event.get('name', 'Unknown') email = event.get('email', 'no-email@example.com') response = { 'message': 'User data processed successfully by Lambda!', 'userName': name, 'userEmail': email, 'timestamp': datetime.now().isoformat() } return response
- Function name:
-
Deploy both functions
Create Python Lambda function:
# Create lambda_function.py first, then:
zip function.zip lambda_function.py
aws lambda create-function \
--function-name getUserDataFunction \
--runtime python3.9 \
--role arn:aws:iam::YOUR_ACCOUNT_ID:role/lambda-execution-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://function.zipcd "D:\Spring Boot\sprinBoot-lambda-demo"In LambdaService.java, update function names if needed:
.functionName("getUserDataFunction") // Your actual Lambda function namemvn clean installmvn spring-boot:runOr run the JAR:
java -jar target/sprinBoot-lambda-demo-0.0.1-SNAPSHOT.jarThe application will start on: http://localhost:8080
GET http://localhost:8080/api/lambda/healthResponse:
Spring Boot Lambda Integration is running!
GET http://localhost:8080/api/lambda/hello?name=JohnResponse:
{
"message": "Hello John from AWS Lambda!",
"timestamp": "abc-123-xyz"
}POST http://localhost:8080/api/lambda/invoke
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com"
}Response:
{
"message": "User data processed successfully by Lambda!",
"userName": "John Doe",
"userEmail": "john@example.com",
"timestamp": "2025-10-27T10:30:00.000Z"
}GET http://localhost:8080/api/lambda/user?name=Jane&email=jane@example.comResponse:
{
"message": "User data processed successfully by Lambda!",
"userName": "Jane",
"userEmail": "jane@example.com",
"timestamp": "2025-10-27T10:30:00.000Z"
}Test Health:
curl http://localhost:8080/api/lambda/healthTest Simple Lambda:
curl "http://localhost:8080/api/lambda/hello?name=Alice"Test POST Request:
curl -X POST http://localhost:8080/api/lambda/invoke \
-H "Content-Type: application/json" \
-d '{"name":"Bob","email":"bob@example.com"}'- Import collection or create new requests
- Set base URL:
http://localhost:8080 - Test each endpoint above
Open: http://localhost:8080/api/lambda/health
βββββββββββββββββββ
β Client β
β (Browser/API) β
ββββββββββ¬βββββββββ
β HTTP Request
βΌ
βββββββββββββββββββ
β Spring Boot β
β Application β
β (Port 8080) β
ββββββββββ¬βββββββββ
β AWS SDK
βΌ
βββββββββββββββββββ
β AWS Lambda β
β Functions β
βββββββββββββββββββ
- Client sends HTTP request to Spring Boot
- Spring Boot Controller receives request
- Lambda Service invokes AWS Lambda using AWS SDK
- AWS Lambda processes the request
- Lambda returns response to Spring Boot
- Spring Boot sends response back to client
sprinBoot-lambda-demo/
β
βββ src/main/java/com/example/springbootlambdademo/
β βββ SprinBootLambdaDemoApplication.java # Main class
β βββ config/
β β βββ AwsConfig.java # AWS configuration
β βββ controller/
β β βββ LambdaController.java # REST endpoints
β βββ service/
β β βββ LambdaService.java # Lambda invocation logic
β βββ model/
β βββ UserRequest.java # Request model
β βββ UserResponse.java # Response model
β
βββ src/main/resources/
β βββ application.properties # App configuration
β
βββ pom.xml # Maven dependencies
βββ lambda-functions.md # Lambda code samples
- Configures AWS Lambda client
- Sets AWS region
- Uses default credentials provider (reads from ~/.aws/credentials)
- Contains business logic to invoke Lambda
- Converts Java objects to JSON
- Sends payload to Lambda
- Receives and parses Lambda response
- Exposes REST API endpoints
- Handles HTTP requests/responses
- Calls LambdaService methods
- POJOs for request/response data
- Jackson serialization/deserialization
Error: Unable to load credentials
Solution:
aws configureOr check: ~/.aws/credentials file exists
Error: ResourceNotFoundException
Solution:
- Verify function name in
LambdaService.java - Check function exists in AWS Console
- Ensure correct AWS region
Error: AccessDeniedException
Solution:
- Check IAM user has
AWSLambdaFullAccesspermission - Verify AWS credentials are correct
Solution:
- Check internet connection
- Verify AWS region is correct
- Check firewall settings
- This is a demo project for learning purposes
- Use IAM roles with minimal permissions in production
- Consider using AWS Secrets Manager for credentials in production
- Lambda cold start times may affect response time
Feel free to improve this demo by:
- Adding error handling
- Implementing retry logic
- Adding unit tests
- Improving documentation
This project is for educational purposes.
Made with β€οΈ for learning Spring Boot + AWS Lambda Integration