AWS Lambda

AWS Lambda

Run code without servers. Pay only when it runs.

serverlessFree Tierbeginner
15 min
Max Timeout
Per invocation
10 GB
Max Memory
Proportional CPU
1M Free
Requests/Month
400,000 GB-seconds
1ms
Billing
Per millisecond

What is Lambda?

Run code without managing servers. Upload a function, it executes when triggered. Scales from 0 to thousands automatically. Pay per millisecond of execution.

Think of it like hiring someone for a single task

You don't pay for idle time. Code runs only when triggered. Scales from zero to thousands automatically.

Key Features

server

Zero Servers

No EC2 to manage. AWS handles everything.

trending-up

Auto Scaling

Handles 1 to 10,000 requests. No config needed.

dollar-sign

Pay Per Use

Billed per millisecond. Free tier: 1M requests/month.

code

Multiple Languages

Python, Node.js, Java, Go, .NET, Ruby, or containers.

git-branch

Event Triggers

API Gateway, S3, SQS, DynamoDB, CloudWatch, 200+ sources.

zap

Provisioned Concurrency

Eliminates cold starts. Costs more.

When to Use

  • API backends
  • Event processing (S3, SQS triggers)
  • Scheduled tasks (cron jobs)
  • Real-time file processing
  • Webhooks and callbacks
  • Microservices

When Not to Use

  • Tasks over 15 minutes → Step Functions/ECS
  • Need persistent connections → EC2/ECS
  • Heavy GPU workloads → EC2
  • Large deployment packages → ECS
  • Consistent high traffic → EC2/Fargate
  • Need full OS access → EC2

Prerequisites

  • An AWS account (free tier: 1M requests/month)
  • AWS CLI installed
  • Basic Python, Node.js, or Java knowledge

AWS Console Steps

1

Open Lambda Console

Navigate to Lambda in the AWS Console and click 'Create function'

2

Choose Blueprint

Select 'Author from scratch' and choose a runtime (Python, Node.js, etc.)

3

Configure Function

Name your function and create/select an execution role

4

Write Code

Edit the function code in the inline editor or upload a ZIP file

5

Test

Create a test event and invoke your function

6

Add Trigger

Connect to API Gateway, S3, or other event sources

AWS CLI Quickstart

Create Lambda function with AWS CLI

Create and invoke a Lambda function using the AWS CLI

cli
# Create a deployment package
zip function.zip index.py

# Create the Lambda function
aws lambda create-function \
  --function-name my-function \
  --runtime python3.12 \
  --handler index.handler \
  --zip-file fileb://function.zip \
  --role arn:aws:iam::123456789012:role/lambda-role

# Invoke the function
# ...

This creates a Python Lambda function and invokes it with a test payload.

First Project Ideas

  • Create a REST API with API Gateway
  • Process S3 uploads (resize images, extract metadata)
  • Send notifications via SNS/SES
  • Scheduled cleanup tasks with EventBridge
  • Webhook handler for external services

Pro Tips8

Cold starts: minimize package size

performance

Cold starts happen on first invocation or scaling. Use smaller packages and Provisioned Concurrency for latency-sensitive APIs.

Keep packages small. Use Provisioned Concurrency for APIs
Don't include dev dependencies in production packages

More memory = more CPU

performance

CPU scales with memory. At 1,769 MB you get 1 vCPU. Faster execution can cost less.

Use Lambda Power Tuning to find optimal memory
Don't default to 128 MB - often slower AND costlier

Initialize connections outside handler

performance

Code outside handler runs once per cold start. Reused across warm invocations.

Put DB connections and SDK clients at module level
Don't create connections inside the handler

Environment variables: 4KB limit

security

Total env vars cannot exceed 4 KB. Use Secrets Manager for secrets.

Use Parameter Store for large configs
Don't store secrets directly in env vars

VPC Lambda needs NAT for internet

security

Lambda in VPC has no internet by default. Use NAT Gateway or VPC endpoints.

Use VPC endpoints for AWS services to avoid NAT costs
Don't put Lambda in VPC unless accessing VPC resources

Layers share code across functions

general

Up to 5 layers per function. 250 MB total unzipped including function code.

Use layers for large, stable dependencies
Don't use layers for frequently changing code

Async invocations retry automatically

reliability

Async retries twice on failure. Make handlers idempotent.

Use DynamoDB conditional writes for idempotency
Don't assume events run exactly once

Default timeout is 3 seconds

reliability

Max is 15 minutes. API Gateway has its own 29-second limit.

Set timeout based on P99 execution time
Don't set timeout to max 15 min 'just in case'

Key Facts8

Max timeout: 15 minutes

Default is 3 seconds. Use Step Functions for longer tasks.

limit

Max memory: 10 GB

Range is 128 MB to 10 GB. CPU scales with memory.

limit

Max zip size: 50 MB (direct upload)

Use S3 for larger packages. Must be under 250 MB unzipped.

limit

Max unzipped size: 250 MB

Includes function code and all layers combined.

limit

Default concurrency: 1,000 per region

Shared across ALL functions. Request increase via AWS Support.

default

/tmp storage: up to 10 GB

Default is 512 MB. Ephemeral - cleared on cold start.

limit

Sync payload: 6 MB max

Request and response both limited to 6 MB.

limit

Async payload: 256 KB max

Store large data in S3 and pass the reference.

limit

AWS Certification Practice4

mediumsaa-c03scs-c02

What configuration is required?

mediumsaa-c03dva-c02

How to eliminate cold starts?

hardsaa-c03dva-c02

What is the BEST solution?

easysaa-c03dva-c02

Why is Lambda NOT suitable?