Welcome to the serverless world, one of the biggest mindset shifts in the cloud. Until now, there was always a server involved: you started it, configured it, scaled it, repaired it. With AWS Lambda you forget about all that: you just write the code you want to run, and AWS takes care of the rest. In this subchapter, you'll understand how this model works.

The mindset shift: from server to function

Let's remember what you've done so far with EC2 (Chapter 4) and Auto Scaling Groups (Chapter 13): you worried about servers. Even though AWS managed the hardware, you had to think about how many instances, what type, how to scale them, how to repair them.

With Lambda, the unit is no longer the server: it's the function. You upload a piece of code (a function), and AWS runs it when needed. There is no server to manage. That's where the name "serverless" comes from: it's not that there are no servers, it's that they're not your problem; AWS adds and removes them for you, invisibly.

Important clarification: "serverless" does not mean "no servers." Servers exist, but they are completely hidden and managed by AWS. You don't see them, you don't configure them, and you don't pay to have them running idle.

How Lambda works: on-demand execution

The Lambda model is easy to understand with one idea: your code only runs when something happens, and only for as long as it takes to do its job.

   An event arrives (a request, an uploaded file, a message...)
        │
        ▼
   AWS starts your function ──► runs your code ──► returns the result
        │
        ▼
   Done: AWS "turns off" everything. Nothing is left running, waiting.

Let's compare it to a traditional server:

  • An EC2 server is always on, waiting for requests. You pay for all the time it's on, even if no requests arrive.
  • A Lambda function only "exists" when there's work. If no one invokes it, it doesn't run and you pay nothing.

Analogy: a server is like having a taxi with the engine running waiting all day at the door: you pay for the gas even if you don't go anywhere. Lambda is like ordering a taxi via app: it shows up just when you need it, takes you, and you only pay for that ride. The rest of the time, you pay nothing.

The three big advantages

  1. You don't manage servers

There's no operating system to patch, no instances to size, nothing to repair. AWS takes care of everything "under the hood." You only focus on your code.

  1. Automatic and instant scaling

If 1 request arrives, AWS runs 1 function. If 1,000 arrive at once, AWS runs 1,000 functions in parallel, automatically. You don't configure Auto Scaling Groups or policies (Chapter 13): scaling is immediate and transparent, managed by AWS.

1 request     → 1 Lambda execution
1,000 at once → 1,000 parallel executions (automatic)
0 requests    → 0 executions (and 0 cost)

  1. You only pay for what you use

This is the star economic advantage. You pay for the number of executions and for the time each one takes (in milliseconds). If your function doesn't run, you pay nothing. This can greatly reduce costs for intermittent or unpredictable workloads.

What does a Lambda function look like inside?

A Lambda function is basically a function in your favorite language (Python, Node.js, Java, Go...) with a specific shape: it receives an event as input and returns a response. Here's a simple example in Python:

def handler(event, context):
    nombre = event.get("nombre", "mundo")
    return {
        "mensaje": f"¡Hola, {nombre}!"
    }
  • event: the input data (what triggered the function and with what information).
  • context: information about the execution (how much time is left, identifiers, etc.).
  • What return returns is the response.

AWS calls this function every time the event that triggers it occurs. You don't worry about where it runs or how many copies there are.

When Lambda fits (and when it doesn't)

Lambda shines in many scenarios, but it's not for everything:

Fits very well with Lambda Better with servers/containers
Short and occasional tasks Very long processes (minutes/hours)
Intermittent or unpredictable traffic Constant, very high 24/7 traffic
Event processing (an uploaded file, a message) Applications with persistent in-memory state
Lightweight APIs and backends Needs fine-grained control of the environment

We'll see the limits and anti-patterns in detail in subchapter 14.5. For now, keep the general idea.

What you should remember

  • Lambda changes the unit of work: from the server to the function. You upload code and AWS runs it; you don't manage servers.
  • Serverless doesn't mean "no servers," but that the servers are invisible and managed by AWS: they're not your problem.
  • The model is on-demand: your code only runs when an event occurs, and only for as long as it takes. If it's not invoked, it doesn't run.
  • Three advantages: you don't manage servers, automatic and instant scaling (1 or 1,000 parallel executions), and you only pay for what you use (if it doesn't run, you don't pay).
  • A function receives an event as input and returns a response; it fits short tasks, intermittent traffic, and event processing.

In the next subchapter, we'll see what can trigger a Lambda function: the triggers (API Gateway, S3, DynamoDB, SQS...).

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved