After this lesson, you will be able to: Build a Lambda function: triggers, execution model, cold starts, and connecting to other AWS services with the SDK.
Lambda is the original serverless. Pay per invocation, no servers to manage. This lesson covers the model + the gotchas.
Write a function in Node / Python / Java / Go / Ruby / .NET / custom runtime. AWS runs it on demand: in response to an event (HTTP, SQS message, S3 upload, schedule). You pay for: invocation count + (memory * duration). Free tier: 1M invocations + 400K GB-seconds per month. Limits: 15 min max duration; 10 GB memory; 50 MB zip / 250 MB layer; 6 MB sync payload / 256 KB async.
The handler is the entry point AWS calls on each invocation.
// index.mjsexport const handler = async (event, context) => {console.log("Event:", JSON.stringify(event));// For an API Gateway HTTP trigger:const name = event.queryStringParameters?.name ?? "world";return {statusCode: 200,headers: { "Content-Type": "application/json" },body: JSON.stringify({ message: `Hello, ${name}` }),};};// Deploy via:// - Console upload (zip the file)// - AWS SAM (sam deploy)// - Serverless Framework// - Terraform / Pulumi / CDK// - Just docker push to ECR + select 'container image' as the deploy method
API Gateway: synchronous HTTP. Build REST or HTTP APIs. Function URL: simpler than API Gateway; one URL per function. Free; built-in. S3: object created/deleted. Async. SQS / SNS / EventBridge: queues + events. Async. CloudWatch Events / Scheduler: cron. DynamoDB Streams: react to DB changes. Direct invoke from SDK: call from other AWS services or your own apps.
When Lambda spins up a new container (first invocation, or after idle), the runtime + your code load. That's a cold start: 100-500 ms for Node/Python, 1-5s for Java. Subsequent invocations on a warm container are sub-10ms. Mitigations: keep code small; lazy-load big modules; pin a SDK client outside the handler (shared across warm invocations); use Provisioned Concurrency (pre-warmed instances; costs money). For interactive workloads (API), cold starts are real UX. For async (queue consumers), usually fine.
Long-running jobs (> 15 min). Use Fargate or batch services. Heavy CPU or GPU. EC2 or specialised instances are cheaper per second of work. Stateful workloads needing local disk. Lambda has 512MB-10GB /tmp; ephemeral. Sub-10ms latency requirements. Cold starts blow that budget. Apps with thousands of concurrent users where you'd pay less for always-on capacity.
Initializing the AWS SDK client inside the handler. Cold-start hit on every invocation. Move OUTSIDE the handler to share across invocations. Forgetting timeouts. Default is 3 seconds; many functions need longer. Set explicitly. No DLQ (dead-letter queue) on async invokes. Failed events silently dropped. Lambda + RDS without a connection pooler. Hits Postgres max_connections fast. Use RDS Proxy. Mounting deps in the deploy zip. Use Layers for shared dependencies across functions.
Pick the best practice.
Sign in and purchase access to unlock this lesson.