Skip to main content

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

FeatureAWS LambdaGoogle Cloud Functions
Max DurationSee AWS Lambda limits540s (1st gen). 2nd gen: 60 min (HTTP), 1800s (scheduled/Task queue), 540s (event-driven)
Memory OptionsSee AWS Lambda limits8 GiB (1st gen), 32 GiB (2nd gen)
Cold StartVaries by runtime/configurationVaries by runtime/configuration
PricingSee AWS Lambda pricingSee Cloud Functions pricing
Free TierSee AWS Lambda pricingSee Cloud Functions pricing
Local TestingSAM CLI (requires Docker)Functions Framework (lightweight)
DeploymentCloudFormation/SAM or Consolegcloud CLI or Console
Environment VariablesVia CloudFormation/ConsoleVia 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:

  1. Code editor — Write and test code
  2. Configuration — Memory, timeout, environment variables
  3. Monitoring — CloudWatch metrics and logs
  4. Triggers — Which events invoke this function

Your First Lambda (Guided)

  1. Go to AWS Lambda > Create Function
  2. Choose "Author from scratch"
  3. Runtime: Python
  4. Handler: lambda_function.lambda_handler
  5. Click Deploy
  6. 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:

  1. Describe the vendor-neutral architecture (event source, compute, state, observability).
  2. Map each component to AWS or GCP services.
  3. Explain why each service is the right fit for cost, performance, and simplicity.

If you want feedback, email your write-up to [email protected].


References