AWS Lambda Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| import json
import boto3
from typing import Any
def handler(event: dict, context: Any) -> dict:
"""
API Gateway Lambda handler with proper error handling.
"""
try:
# Parse request
body = json.loads(event.get('body', '{}'))
# Process request
result = process_request(body)
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps(result)
}
except ValueError as e:
return {
'statusCode': 400,
'body': json.dumps({'error': str(e)})
}
except Exception as e:
# Log error for CloudWatch
print(f"Error: {e}")
return {
'statusCode': 500,
'body': json.dumps({'error': 'Internal server error'})
}
# SQS Trigger Handler
def sqs_handler(event: dict, context: Any) -> dict:
"""Process messages from SQS queue."""
for record in event['Records']:
message = json.loads(record['body'])
process_message(message)
return {'batchItemFailures': []}
|
Serverless Architecture
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| βββββββββββββββ βββββββββββββββββββββββββββββββββββββββ
β Client ββββββΆβ API Gateway β
βββββββββββββββ βββββββββββββββββ¬ββββββββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββββ
β Lambda Function β
β (Request Processing) β
βββββββββββββββββ¬ββββββββββββββββββ
β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β DynamoDB β β SQS β β S3 β
β (Data Store) β β (Queue Jobs) β β (Storage) β
βββββββββββββββββββ ββββββββββ¬βββββββββ βββββββββββββββββββ
β
βββββββββββββββΌββββββββββββββ
β Lambda Function β
β (Background Processing) β
βββββββββββββββββββββββββββββ
|
Frequently Asked Questions
What is serverless development?
Serverless development involves building applications that run on managed infrastructure where you only pay for execution time. This includes AWS Lambda, Google Cloud Functions, Azure Functions, and services like AWS Fargate, Cloud Run. You focus on code; the cloud handles scaling.
How much does serverless development cost?
Serverless development typically costs $100-150 per hour. A basic serverless API starts around $8,000-15,000, while complex event-driven architectures range from $30,000-80,000+. Serverless often reduces infrastructure costs significantly for variable workloads.
Serverless vs containers: which should I choose?
Choose serverless for: variable/spiky workloads, cost optimization at low scale, event-driven processing, or quick deployments. Choose containers for: consistent workloads, specific runtime needs, long-running processes, or when you need more control. Many projects use both.
What are serverless cold start issues?
Cold starts occur when a function hasn’t been invoked recently and needs to initialize. This adds latency (100ms-2s). I mitigate with: provisioned concurrency, smaller bundles, lazy loading, and architecture patterns that tolerate latency. Cold starts matter less than people think.
Do you work with the Serverless Framework?
Yes. I use Serverless Framework, AWS SAM, and CDK for serverless deployments. These enable: infrastructure as code, local testing, CI/CD integration, and consistent deployments. The choice depends on team preferences and existing infrastructure.