A serverless user authentication service built with AWS Lambda and Go, providing secure user registration, JWT-based login, and protected API routes.
- User Registration - Secure account creation with username and password
- JWT Authentication - Token-based login system with secure access tokens
- Protected Routes - Middleware-based route protection with JWT validation
- Persistent Storage - DynamoDB integration for reliable user data storage
- Distributed Tracing - AWS X-Ray monitoring for performance insights
- Infrastructure as Code - Complete AWS CDK deployment setup
- Go - Core language for Lambda functions and API logic
- AWS Lambda - Serverless compute platform
- API Gateway - HTTP request routing to Lambda handlers
- DynamoDB - NoSQL database for user data storage
- AWS X-Ray - Distributed tracing and monitoring
- AWS CDK - Infrastructure as code deployment
Before you begin, ensure you have the following installed:
- AWS CLI configured with proper credentials
- AWS CDK (
npm install -g aws-cdk
) - Go 1.20+
- AWS account with permissions for Lambda, DynamoDB, and API Gateway
cdk bootstrap aws://YOUR_ACCOUNT/YOUR_REGION
cdk deploy
This creates:
- DynamoDB table (
userTable
) - Lambda function with Go runtime
- API Gateway with endpoints:
/register
,/login
,/protected
- X-Ray tracing configuration
POST /register
curl -X POST https://YOUR_API_GATEWAY_URL/prod/register \
-H "Content-Type: application/json" \
-d '{
"username": "your_username",
"password": "your_password"
}'
Responses:
200 OK
- User registered successfully409 Conflict
- User already exists400 Bad Request
- Validation error or malformed request
POST /login
curl -X POST https://YOUR_API_GATEWAY_URL/prod/login \
-H "Content-Type: application/json" \
-d '{
"username": "your_username",
"password": "your_password"
}'
Responses:
200 OK
- Returns JSON with JWT access token400 Bad Request
- Invalid credentials or malformed request
GET /protected
curl -X GET https://YOUR_API_GATEWAY_URL/prod/protected \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
Responses:
200 OK
- Access granted401 Unauthorized
- Missing or invalid token
# Build the Docker image
docker build -t lambda-auth .
# Run the container locally
docker run --rm lambda-auth
For container-based Lambda deployment:
# Create ECR repository
aws ecr create-repository --repository-name lambda-auth
# Authenticate Docker to ECR
aws ecr get-login-password --region YOUR_REGION | \
docker login --username AWS --password-stdin \
YOUR_AWS_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com
# Tag and push image
docker tag lambda-auth:latest \
YOUR_AWS_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com/lambda-auth:latest
docker push \
YOUR_AWS_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com/lambda-auth:latest
- AWS X-Ray - Distributed tracing for Lambda executions and DynamoDB calls
- CloudWatch Logs - Centralized logging for debugging and monitoring
- Performance Metrics - Latency and error analysis through X-Ray console
You can test Lambda handlers locally using:
- AWS SAM CLI - For local Lambda simulation
- Integration Tests - Direct API Gateway endpoint testing
- Deployment Fails: Verify AWS credentials and permissions
- DynamoDB Access: Ensure table exists and Lambda has proper IAM roles
- JWT Validation: Check token generation and verification logic
- Runtime Errors: Review CloudWatch logs for detailed error messages
- Check AWS CLI configuration:
aws sts get-caller-identity
- Verify CDK deployment:
cdk ls
- Review Lambda logs in CloudWatch
- Test API endpoints with proper headers and payloads
-
Password Reset Functionality
Add a secure password reset flow with email verification or temporary tokens, so users can recover access safely. -
Multi-Factor Authentication (MFA)
Implement MFA using AWS Cognito or custom flows to boost security on login. -
Better Error Handling and Retries
Add exponential backoff retries for DynamoDB requests and improved error messages to handle transient AWS service errors gracefully. -
Rate Limiting and Throttling
Protect APIs from abuse using API Gateway throttling or custom Lambda logic. -
User Role Management & Authorization
Add roles and permissions for users, controlling access to certain endpoints or resources. -
Infrastructure as Code Improvements
Expand CDK stack to manage multiple environments (dev/staging/prod) with easier deployments and automated CI/CD pipelines. -
Monitoring & Alerting
Set up CloudWatch alarms for error rates and latency, plus integrate with SNS or Slack for real-time alerts. -
Use AWS Secrets Manager
Securely manage sensitive configs like JWT secrets or DB credentials instead of hardcoding them. -
API Gateway Improvements
Use request/response validation, schema enforcement, and caching for better performance and security. -
Move to AWS Cognito or Amplify
Evaluate using AWS Cognito for user pools and authentication to reduce custom auth code and improve scalability.
This project is licensed under the MIT License - see the LICENSE file for details.