Serverless Functions: AWS Lambda & Google Cloud Functions
What Are Serverless Functions?
A serverless function is a piece of code that runs in response to events, without you managing servers.
You write code, upload it, and the cloud provider handles:
- Provisioning compute
- Running your code
- Auto-scaling
- Patching runtimes
Simple Explanation
What it is
Serverless functions are tiny programs that wake up when something happens, do one job, then turn off.
Why we need it
It keeps you focused on business logic instead of server setup and scaling rules.
Benefits
- On-demand compute with no idle cost.
- Simple deployment of small, focused functions.
- Built-in scaling for unpredictable traffic.
Tradeoffs
- Cold start latency for infrequent traffic.
- Runtime limits on memory and execution time.
- Less control over the underlying server environment.
Real-world examples (architecture only)
- HTTP request → Function → Read/write database.
- File uploaded → Function → Resize image → Store output.
Core Concepts (Both Clouds)
Handler
The function that the cloud provider invokes. It receives an event and returns a response.
Runtime
The language environment:
- AWS: Python, Java, Go, Ruby, .NET
- GCP: Python, Go, Java, .NET
Memory & CPU
- AWS: See AWS Lambda limits (memory and CPU vary by region/runtime)
- GCP: 8 GiB (1st gen), 32 GiB (2nd gen)
Execution Duration
- AWS: See AWS Lambda limits
- GCP: 540s (1st gen). 2nd gen: 60 min (HTTP), 1800s (scheduled/Task queue), 540s (event-driven)
AWS Lambda Implementation
Creating a Lambda Function
# lambda_function.py - AWS Lambda handler
def lambda_handler(event, context):
print(f"Event: {event}")
return {
'statusCode': 200,
'body': 'Hello from Lambda!'
}
What this does: Logs the incoming event, then returns a simple HTTP-style response. This pattern maps cleanly to API Gateway or any event source that expects a response payload.
Deployment (AWS SAM)
# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.12
Handler: lambda_function.lambda_handler
CodeUri: ./
MemorySize: 256
Timeout: 30
Events:
ApiEvent:
Type: Api
Properties:
Path: /hello
Method: GET
RestApiId: !Ref HelloApi
HelloApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Deploy:
sam build
sam deploy --guided --stack-name my-function
Testing Locally (AWS)
sam local start-api
curl http://localhost:3000/hello
Google Cloud Functions Implementation
Creating a Cloud Function
# main.py - Google Cloud Function
def hello(request):
print(f"Request: {request.args}")
return {
'message': 'Hello from Cloud Functions!',
'status': 200
}
What this does: Reads query parameters from the incoming HTTP request, then returns a JSON response. This is the baseline for HTTP-triggered serverless APIs.
Deployment (Google Cloud)
# Deploy via gcloud CLI
gcloud functions deploy hello \
--runtime python312 \
--trigger-http \
--allow-unauthenticated \
--entry-point=hello
Testing Locally (GCP)
# Install Functions Framework
pip install functions-framework
# Run locally
functions-framework --target=hello
# In another terminal
curl http://localhost:8080/
AWS vs. GCP Comparison
| Feature | AWS Lambda | Google Cloud Functions |
|---|---|---|
| Max Duration | See AWS Lambda limits | 540s (1st gen). 2nd gen: 60 min (HTTP), 1800s (scheduled/Task queue), 540s (event-driven) |
| Memory Options | See AWS Lambda limits | 8 GiB (1st gen), 32 GiB (2nd gen) |
| Cold Start | Varies by runtime/configuration | Varies by runtime/configuration |
| Pricing | See AWS Lambda pricing | See Cloud Functions pricing |
| Free Tier | See AWS Lambda pricing | See Cloud Functions pricing |
| Local Testing | SAM CLI (requires Docker) | Functions Framework (lightweight) |
| Deployment | CloudFormation/SAM or Console | gcloud CLI or Console |
| Environment Variables | Via CloudFormation/Console | Via YAML or gcloud |
Which Should You Use?
Use AWS Lambda if:
- You're already in AWS ecosystem
- You need consistent pricing across services
- You use EC2, RDS, DynamoDB
Use Google Cloud Functions if:
- You prefer simpler local development
- You need longer execution times (60 min)
- You're in Google Cloud ecosystem (Datastore, BigQuery)
Best Practice: Learn both. Most enterprises use multi-cloud.
4. Timeout
How long Lambda waits before ending your function (configurable up to the platform max). See AWS Lambda limits.
5. Cold Start
First invocation after deployment is slower because Lambda provisions the runtime.
Invocation Types
Synchronous
- You wait for the response
- Common for HTTP requests
- Errors are returned to caller
Asynchronous
- Fire and forget
- Response returned immediately
- Errors logged internally
- Common for background tasks
Lambda Permissions
Lambda needs IAM roles to access other AWS services.
Example: A Lambda that reads from DynamoDB needs:
Service: s3
Action: GetObject
Resource: arn:aws:s3:::my-bucket/*
Lambda Console
Key areas:
- Code editor — Write and test code
- Configuration — Memory, timeout, environment variables
- Monitoring — CloudWatch metrics and logs
- Triggers — Which events invoke this function
Your First Lambda (Guided)
- Go to AWS Lambda > Create Function
- Choose "Author from scratch"
- Runtime: Python
- Handler:
lambda_function.lambda_handler - Click Deploy
- Test with sample event
Key Takeaway
Lambda is the bridge between events and code. Events trigger your functions, Lambda manages the infrastructure, and you focus on business logic.
Project (Cloud-Agnostic)
Design a "Hello" API with one HTTP endpoint and one background event.
Deliverables:
- Describe the vendor-neutral architecture (event source, compute, state, observability).
- Map each component to AWS or GCP services.
- Explain why each service is the right fit for cost, performance, and simplicity.
If you want feedback, email your write-up to maarifaarchitect@gmail.com.
References
- AWS Lambda Developer Guide: https://docs.aws.amazon.com/lambda/latest/dg/welcome.html
- AWS Lambda Limits (service quotas): https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html
- AWS Lambda Pricing: https://aws.amazon.com/lambda/pricing/
- Google Cloud Functions Overview: https://cloud.google.com/functions/docs/concepts/overview
- Google Cloud Functions Quotas (limits): https://cloud.google.com/functions/quotas
- Google Cloud Functions Pricing: https://cloud.google.com/functions/pricing