Skip to content

Latest commit

 

History

History
326 lines (247 loc) · 9.17 KB

File metadata and controls

326 lines (247 loc) · 9.17 KB

Architecture Explanation

What is Serverless?

Serverless is a cloud computing model where you don't manage servers directly. Instead, the cloud provider automatically handles server provisioning, scaling, and maintenance. You only pay for the actual compute time your code uses.

Key Benefits of Serverless:

  • No server management: No need to provision, configure, or maintain servers
  • Auto-scaling: Automatically scales up or down based on demand
  • Pay-per-use: Only pay for actual execution time
  • High availability: Built-in redundancy and fault tolerance
  • Faster development: Focus on code, not infrastructure

AWS Services Used in This Project

1. AWS Lambda

What is Lambda? AWS Lambda is a serverless compute service that runs your code in response to events. It automatically manages the compute resources for you.

How it works:

  • You upload your code (Python, Node.js, Java, etc.)
  • AWS Lambda runs your code when triggered
  • You only pay for the compute time used
  • Automatically scales from zero to thousands of concurrent executions

In our project:

  • Runtime: Python 3.11
  • Handler: lambda_function.lambda_handler
  • Memory: 128 MB
  • Timeout: 30 seconds
  • Triggers: API Gateway HTTP requests

Code example:

def lambda_handler(event, context):
    # Your business logic here
    return {
        'statusCode': 200,
        'body': json.dumps({'message': 'Hello from Lambda!'})
    }

2. Amazon API Gateway

What is API Gateway? API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.

How it works:

  • Acts as a front door for your applications
  • Handles HTTP requests and routes them to backend services
  • Provides features like authentication, rate limiting, and monitoring
  • Supports REST APIs and WebSocket APIs

In our project:

  • Type: REST API
  • Endpoints:
    • GET /tasks - List all tasks
    • POST /tasks - Create new task
    • DELETE /tasks/{id} - Delete specific task
  • Integration: Lambda proxy integration
  • Stage: dev

Request flow:

HTTP Request → API Gateway → Lambda Function → DynamoDB

3. Amazon DynamoDB

What is DynamoDB? DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.

How it works:

  • NoSQL database (key-value and document store)
  • Automatically scales up or down based on demand
  • Built-in high availability and durability
  • Pay-per-request pricing model

In our project:

  • Table: serverless-todo-api-tasks-dev
  • Primary Key: id (String)
  • Billing Mode: Pay-per-request
  • Operations: Create, Read, Delete tasks

Data structure:

{
  "id": "uuid-here",
  "title": "Task title",
  "description": "Task description",
  "completed": false,
  "created_at": "2025-08-23T11:05:09.335843",
  "updated_at": "2025-08-23T11:05:09.335843"
}

4. AWS IAM (Identity and Access Management)

What is IAM? IAM is a service that helps you securely control access to AWS resources. You can create and manage AWS users and groups, and use permissions to allow and deny their access to AWS resources.

How it works:

  • Users: Individual AWS accounts
  • Roles: Temporary permissions for services
  • Policies: JSON documents that define permissions
  • Principle of least privilege: Grant minimum necessary permissions

In our project:

  • Lambda Execution Role: Allows Lambda to access DynamoDB
  • DynamoDB Policy: Grants read/write permissions to the tasks table
  • API Gateway Permissions: Allows API Gateway to invoke Lambda

How the Services Work Together

1. Request Flow

User → API Gateway → Lambda → DynamoDB → Lambda → API Gateway → User

Step-by-step process:

  1. User sends HTTP request to API Gateway

    POST https://api-gateway-url/dev/tasks
    Content-Type: application/json
    {"title": "New Task", "description": "Task description"}
    
  2. API Gateway receives request and routes it to the appropriate Lambda function

  3. Lambda function processes request

    • Parses the HTTP method and path
    • Validates input data
    • Performs business logic
  4. Lambda interacts with DynamoDB

    • Creates, reads, or deletes data
    • Handles database operations
  5. Lambda returns response to API Gateway

  6. API Gateway sends response back to user

2. Data Flow Example

Creating a Task:

  1. User Request:

    curl -X POST /tasks -d '{"title": "Learn AWS", "description": "Study serverless"}'
  2. API Gateway: Routes to Lambda function

  3. Lambda Function:

    # Parse request
    body = json.loads(event['body'])
    title = body['title']
    
    # Create task in DynamoDB
    task_item = {
        'id': str(uuid.uuid4()),
        'title': title,
        'completed': False,
        'created_at': datetime.utcnow().isoformat()
    }
    table.put_item(Item=task_item)
  4. DynamoDB: Stores the task

  5. Response: Returns the created task to user

3. Error Handling

Lambda handles various error scenarios:

  • Invalid JSON in request body
  • Missing required fields
  • DynamoDB connection issues
  • Task not found (for delete operations)

Example error response:

{
  "statusCode": 400,
  "body": "{\"error\": \"Title is required\"}"
}

Infrastructure as Code with Terraform

What is Terraform?

Terraform is an open-source infrastructure as code tool that lets you define and provide data center infrastructure using a declarative configuration language.

How it works in our project:

  1. Define resources in .tf files
  2. Plan changes with terraform plan
  3. Apply changes with terraform apply
  4. Destroy resources with terraform destroy

Key Terraform files:

  • main.tf: Provider configuration and backend setup
  • variables.tf: Input variables for customization
  • lambda.tf: Lambda function and IAM roles
  • api_gateway.tf: API Gateway configuration
  • dynamodb.tf: DynamoDB table definition
  • outputs.tf: Output values (URLs, ARNs, etc.)

Cost Optimization

Pay-per-request Model

Lambda:

  • Pay only for compute time (100ms increments)
  • No charges when not in use
  • Free tier: 1M requests per month

API Gateway:

  • Pay per API call
  • Free tier: 1M API calls per month

DynamoDB:

  • Pay-per-request pricing
  • No minimum capacity requirements
  • Pay only for actual read/write operations

Cost Example

For a typical usage scenario:

  • 1,000 API calls per day
  • Average Lambda execution: 200ms
  • 1,000 DynamoDB operations per day

Estimated monthly cost:

  • Lambda: ~$0.50
  • API Gateway: ~$0.30
  • DynamoDB: ~$0.10
  • Total: ~$0.90/month

Security Considerations

1. IAM Roles and Policies

  • Least privilege principle: Only necessary permissions
  • Lambda execution role: Minimal permissions for DynamoDB access
  • No hardcoded credentials: Uses IAM roles

2. API Gateway Security

  • HTTPS only: All endpoints use HTTPS
  • CORS headers: Configured for web applications
  • No authentication: Public API (can be enhanced with Cognito)

3. DynamoDB Security

  • Encryption at rest: Automatic encryption
  • Encryption in transit: TLS encryption
  • IAM-based access: No database passwords

Scalability

Automatic Scaling

Lambda:

  • Scales from 0 to thousands of concurrent executions
  • No manual scaling required
  • Handles traffic spikes automatically

API Gateway:

  • Handles millions of requests per second
  • Global edge locations for low latency
  • Automatic throttling and rate limiting

DynamoDB:

  • Scales automatically based on demand
  • No capacity planning required
  • Consistent performance at any scale

Performance Characteristics

  • Cold start: ~100-200ms for first request
  • Warm start: ~10-50ms for subsequent requests
  • Database latency: ~1-10ms for DynamoDB operations
  • Total response time: ~50-300ms typical

Monitoring and Logging

CloudWatch Integration

Lambda Logs:

  • Automatic logging to CloudWatch
  • Log group: /aws/lambda/serverless-todo-api-dev
  • Structured logging with request/response data

API Gateway Metrics:

  • Request count, latency, error rates
  • 4xx and 5xx error tracking
  • Integration with CloudWatch dashboards

DynamoDB Metrics:

  • Read/write capacity utilization
  • Throttled requests
  • Error rates and latency

Best Practices Implemented

  1. Separation of Concerns: Each service has a specific responsibility
  2. Error Handling: Comprehensive error handling in Lambda
  3. Input Validation: Validates all user inputs
  4. Idempotency: Safe to retry operations
  5. Monitoring: Built-in logging and metrics
  6. Security: IAM roles and HTTPS
  7. Cost Optimization: Pay-per-request model
  8. Infrastructure as Code: Reproducible deployments

Future Enhancements

  1. Authentication: Add AWS Cognito for user management
  2. Caching: Add CloudFront for static content
  3. Monitoring: Enhanced CloudWatch dashboards
  4. CI/CD: Automated deployment pipeline
  5. Frontend: React/Vue.js web application
  6. Advanced Features: Task categories, search, filtering