This is a boilerplate Go Lambda function that handles different HTTP request types and returns JSON responses. It's designed to work with AWS Application Load Balancer (ALB) and includes multiple local development options.
- Handles all major HTTP methods: GET, POST, PUT, DELETE, PATCH
- Returns JSON responses with request details
- CORS support for web applications
- Proper error handling and logging
- ALB integration ready
- Local development server for testing
- LocalStack integration for full AWS service simulation
.
├── main.go # Main Lambda function with local server support
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
├── start.sh # Unified starter script (main entry point)
├── docker-compose.yml # LocalStack Docker Compose configuration (Advanced)
├── docker-compose-simple.yml # LocalStack Docker Compose configuration (Simple)
├── scripts/ # All utility scripts
│ ├── utils.sh # Shared utility functions
│ ├── build.sh # Build script for Lambda deployment
│ ├── create-iam-role.sh # Create IAM role for LocalStack
│ ├── deploy-lambda-localstack.sh # Deploy Lambda to LocalStack
│ ├── test-localstack.sh # Test Lambda in LocalStack
│ ├── create-alb-localstack-simple.sh # Create ALB in LocalStack
│ ├── cleanup-localstack-simple.sh # Cleanup LocalStack (Simple Mode)
│ └── install-prerequisites.sh # Prerequisites checker script
├── .gitignore # Git ignore file
└── README.md # This file
- Go 1.21 or later
- curl (for testing)
- Docker and docker-compose (for LocalStack)
- AWS CLI (for LocalStack testing)
- zip (for building Lambda deployment packages)
Prerequisites Check: The project includes automatic checking of prerequisites. When you run any LocalStack option, the script will:
- Check if AWS CLI is installed and provide installation instructions if missing
- Check if zip package is installed and provide installation instructions if missing
- Exit with clear error messages if prerequisites are not met
Manual Installation: You must install prerequisites manually:
# Install AWS CLI
# Linux:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
rm -rf aws awscliv2.zip
# macOS:
brew install awscli
# Install zip package
# Ubuntu/Debian:
sudo apt-get update && sudo apt-get install -y zip
# CentOS/RHEL:
sudo yum install -y zip
# macOS:
brew install zipPrerequisites Checker:
./scripts/install-prerequisites.shSingle Entry Point - Unified Starter Script
./start.shThis will show an interactive menu with 8 options:
- Deploy Simple - Start local development server
- Test Simple - Test local development server
- Deploy LocalStack Simple - Deploy to LocalStack (Simple Mode)
- Test LocalStack Simple - Test LocalStack (Simple Mode)
- Cleanup LocalStack Simple - Cleanup LocalStack (Simple Mode)
- Deploy LocalStack Advanced - Deploy to LocalStack (Advanced Mode)
- Test LocalStack Advanced - Test LocalStack (Advanced Mode)
- Cleanup LocalStack Advanced - Cleanup LocalStack (Advanced Mode)
Or you can specify directly:
# Local development server
./start.sh deploy-simple
./start.sh test-simple
# LocalStack (Simple Mode) - RECOMMENDED
./start.sh deploy-localstack-simple
./start.sh test-localstack-simple
./start.sh cleanup-localstack-simple
# LocalStack (Advanced Mode)
./start.sh deploy-localstack-advanced
./start.sh test-localstack-advanced
./start.sh cleanup-localstack-advanced
# Show help
./start.sh helpThe starter script will automatically:
- Check prerequisites
- Build the Lambda binary
- Start the local server on port 8080
- Provide testing instructions
Tests all HTTP methods against the running local server:
- GET, POST, PUT, DELETE, PATCH, OPTIONS
- Validates JSON responses
- Checks status codes
The starter script will automatically:
- Check prerequisites (Docker, AWS CLI)
- Install AWS CLI if needed
- Start LocalStack (Simple Mode)
- Configure AWS CLI for LocalStack
- Build and deploy Lambda function
- Create IAM role
Tests the deployed Lambda function in LocalStack:
- Direct Lambda invocation
- All HTTP methods
- JSON response validation
Cleans up LocalStack (Simple Mode):
- Stops containers
- Removes data directories
The starter script will automatically:
- Check prerequisites (Docker, AWS CLI)
- Install AWS CLI if needed
- Start LocalStack (Advanced Mode)
- Configure AWS CLI for LocalStack
- Build and deploy Lambda function
- Create IAM role
Tests the deployed Lambda function in LocalStack:
- Direct Lambda invocation
- All HTTP methods
- JSON response validation
Cleans up LocalStack (Advanced Mode):
- Stops containers and removes volumes
- Removes data directories
LOCAL_DEV=true- Enables local development modePORT=8080- Sets the port for local server (default: 8080)AWS_ENDPOINT_URL=http://localhost:4566- LocalStack endpointAWS_PROFILE=localstack- LocalStack AWS profile
The function returns JSON responses with the following structure:
{
"message": "GET request processed successfully",
"request_type": "GET",
"path": "/",
"method": "GET",
"headers": {
"Content-Type": "application/json",
"User-Agent": "curl/7.68.0"
},
"query_params": {
"param1": "value1"
},
"body": "",
"status": 200
}| Method | Status Code | Description |
|---|---|---|
| GET | 200 | Retrieve data |
| POST | 201 | Create new resource |
| PUT | 200 | Update resource |
| DELETE | 200 | Delete resource |
| PATCH | 200 | Partial update |
| OPTIONS | 200 | CORS preflight |
The function includes proper error handling for:
- Unsupported HTTP methods (405 Method Not Allowed)
- Internal server errors (500 Internal Server Error)
- JSON marshaling errors
The function includes CORS headers to support web applications:
Access-Control-Allow-Origin: *Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONSAccess-Control-Allow-Headers: Content-Type, Authorization
When you're ready to deploy to AWS Lambda:
-
Build the Lambda function:
./build.sh
-
Deploy to AWS Lambda:
aws lambda create-function \ --function-name go-alb-lambda \ --runtime provided.al2 \ --handler bootstrap \ --zip-file fileb://function.zip \ --role arn:aws:iam::YOUR_ACCOUNT_ID:role/lambda-execution-role
| Feature | Local Server | LocalStack Simple | LocalStack Advanced |
|---|---|---|---|
| Setup Complexity | Simple | Simple | Moderate |
| AWS Service Simulation | None | Full | Full |
| Lambda Testing | HTTP simulation | Real Lambda | Real Lambda |
| ALB Testing | HTTP simulation | Real ALB | Real ALB |
| IAM/Roles | Not needed | Full simulation | Full simulation |
| Resource Usage | Low | Medium (Docker) | High (Docker + volumes) |
| Startup Time | Fast | Medium | Slow |
| Data Persistence | None | None | Yes |
| Debug Logging | Basic | Minimal | Full debug output |
| Reliability | High | High (no persistence issues) | Medium (persistence can cause issues) |
| Use Case | Quick development | Quick testing, CI/CD | Development with data persistence |
| Cleanup | Simple stop | Simple container stop | Full cleanup with volume removal |
Recommendation:
- Use Local Server for quick development and testing
- Use LocalStack Simple for integration testing and AWS service validation (RECOMMENDED)
- Use LocalStack Advanced for development requiring data persistence
-
Start Local Development:
./start.sh # Choose option 1: Deploy Simple -
Test Local Development:
./start.sh # Choose option 2: Test Simple -
Make Changes:
- Edit
main.go - Restart the local server (Ctrl+C, then option 1 again)
- Edit
-
Deploy to LocalStack Simple (Recommended):
./start.sh # Choose option 3: Deploy LocalStack Simple -
Test LocalStack:
./start.sh # Choose option 4: Test LocalStack Simple -
Cleanup when done:
./start.sh # Choose option 5: Cleanup LocalStack Simple
-
Deploy to LocalStack Advanced:
./start.sh # Choose option 6: Deploy LocalStack Advanced -
Test LocalStack Advanced:
./start.sh # Choose option 7: Test LocalStack Advanced -
Cleanup when done:
./start.sh # Choose option 8: Cleanup LocalStack Advanced
- All HTTP methods are tested automatically (GET, POST, PUT, DELETE, PATCH, OPTIONS)
- Check response format and status codes
- Verify CORS headers
- JSON response validation
-
Build for AWS:
./scripts/build.sh
-
Deploy to AWS Lambda:
aws lambda create-function \ --function-name go-alb-lambda \ --runtime provided.al2 \ --handler bootstrap \ --zip-file fileb://function.zip \ --role arn:aws:iam::YOUR_ACCOUNT_ID:role/lambda-execution-role
-
Attach to Application Load Balancer
To customize this boilerplate:
- Add route-specific logic: Modify the switch statement in
handleRequest - Add authentication: Implement middleware for JWT or API key validation
- Add database integration: Include database connections and queries
- Add environment variables: Use
os.Getenv()to read configuration - Add structured logging: Replace
log.Printfwith a structured logging library
-
Port already in use:
export PORT=8081 ./run-local.sh -
Dependencies not found:
go mod tidy
-
Permission denied:
chmod +x *.sh -
LocalStack not starting:
# Try simple mode first (recommended) ./start.sh # Choose option 5: Cleanup LocalStack Simple ./start.sh # Choose option 3: Deploy LocalStack Simple # If that doesn't work, try advanced mode ./start.sh # Choose option 8: Cleanup LocalStack Advanced ./start.sh # Choose option 6: Deploy LocalStack Advanced
-
AWS CLI not configured for LocalStack:
aws configure set aws_access_key_id test --profile localstack aws configure set aws_secret_access_key test --profile localstack aws configure set region us-east-1 --profile localstack
The local server provides detailed logging:
- Request details (method, path)
- Response details
- Error information
Check the terminal output for debugging information.