Serverless in AWS | AWS Lambda
Serverless in AWS β Overview
Serverless computing lets you build and run applications without managing servers. AWS handles the infrastructure, scaling, and high availability.
What is AWS Lambda?
AWS Lambda is a compute service that runs your code in response to events, automatically scaling and charging only for usage.
- No server provisioning
- Supports multiple languages (Python, Java, Node.js, Go, .NET, Ruby)
- Common triggers: API Gateway, S3, SQS, DynamoDB, EventBridge
AWS Lambda Pricing β Example
You pay for:
- Requests: First 1M/month free, $0.20 per additional million
- Duration: $0.00001667 per GB-second
Example:
- Memory: 512 MB (0.5 GB)
- Execution time: 1 second
- Invocations: 1 million/month
Cost = (0.5 GB * 1 sec * 1M) = 500,000 GB-seconds
500,000 Γ $0.00001667 = $8.34
AWS Lambda Limits (Per Region)
Limit | Default |
---|---|
Max function timeout | 15 minutes (900 sec) |
Max memory allocation | 10 GB |
Max package size (zipped) | 50 MB (direct), 250 MB (with layers) |
Concurrency limit | 1,000 (can be increased) |
Environment variables | 4 KB |
Ephemeral /tmp storage |
512 MB (default), up to 10 GB |
Execution role session duration | 1 hour |
SnapStart supported only in Java 11 and Java 17 |
Lambda SnapStart
- Only for Java (11/17)
- Snapshots the initialized execution state and reuses it across invocations
- Reduces cold start time dramatically
- Best for APIs and high-performance Java workloads
Customization at the Edge
Used with Amazon CloudFront to run code at AWS Edge locations.
CloudFront Functions
- Ultra-lightweight JavaScript at the edge
- Use cases: URL rewrites, header manipulation, redirects
- Runs before cache at the viewer request phase
- Lightning fast (<1ms)
Lambda@Edge
- Full Lambda functions run at edge locations
- Supports Node.js and Python
- Runs at:
- Viewer Request
- Viewer Response
- Origin Request
- Origin Response
Ideal for authentication, A/B testing, user-based content delivery
CloudFront Functions vs Lambda@Edge
Feature | CloudFront Functions | Lambda@Edge |
---|---|---|
Languages | JavaScript | Node.js, Python |
Use Case | Lightweight logic | Complex processing |
Runtime Limit | 1ms | Up to 5 seconds |
Size Limit | 10 KB | 50 MB |
Execution Location | Viewer Request only | All 4 edge phases |
Latency & Cost | Lower | Higher |
Lambda by Default
- Runs in a shared VPC
- Cannot access RDS, EC2, or private resources unless configured
- Internet access: β by default
- VPC access: β unless explicitly configured
Lambda in a VPC
- Required when Lambda needs access to:
- RDS databases
- Private subnets
- Redis/Memcached (ElastiCache)
π§ Needs:
- VPC configuration (subnet + security group)
- NAT Gateway if outbound internet access is needed
Lambda with RDS Proxy
- Improves connection management to RDS (especially MySQL/PostgreSQL)
- Benefits:
- Efficient connection pooling
- Helps avoid βtoo many connectionsβ errors
- Better performance under load
Invoking Lambda from RDS & Aurora
You can invoke Lambda from the database using:
-
Amazon Aurora (MySQL/PostgreSQL) via native SQL function
aws_lambda.invoke()
- Use cases:
- Real-time triggers
- Stream-to-Lambda data processing
- Audit logging, notifications
RDS Event Notifications
Use Amazon SNS or Lambda to react to RDS events, such as:
Event Type | Example |
---|---|
Availability | DB instance restart |
Security | Credential changes |
Backup | Snapshot completion |
Maintenance | OS or DB patching |
Subscribe via:
- SNS topics
- Lambda functions for automation
- EventBridge rules for routing
Summary
Topic | Summary |
---|---|
AWS Lambda | Run code without servers |
Pricing | Pay-per-use (requests + duration) |
Limits | 15 min timeout, 10 GB memory |
SnapStart | Fast startup for Java |
CloudFront Functions | JS logic at edge (lightweight) |
Lambda@Edge | Full Lambda power at edge |
Lambda in VPC | Needed for private resource access |
RDS Proxy | Efficient Lambda DB connection mgmt |
Invoke Lambda from RDS | Aurora trigger to Lambda |
RDS Notifications | Event-based actions via SNS/Lambda |
Β