AWS Lambda
AWS Lambda is a functions-as-a-service (FaaS) platform. You upload a function (a handler in Node.js, Python, Java, Go, .NET or a container image), connect it to an event source, and AWS runs it whenever an event arrives: an HTTP request, a file landing in S3, a message on a queue, a row changing in DynamoDB. You never provision or patch a server, and you pay only for the time your code is actually running.
The key properties to keep in mind:
- Event-driven: Functions don't run continuously. Each invocation handles one event (or one batch of events) and returns.
- Automatic scaling, including to zero: Lambda runs as many copies of your function as there are concurrent events, up to your account's limits, and runs none when there's no traffic.
- Stateless and ephemeral: Each copy runs in an isolated execution environment that may be reused for later requests or thrown away at any time. Anything that must survive goes to an external store.
- Pay per use: Billing is per request plus per millisecond of execution, scaled by the memory you allocate. Idle time costs nothing.
- Bounded: An invocation can run for at most 15 minutes, with up to 10 GB of memory. Lambda is built for short tasks, not long-running servers.
In interviews, Lambda comes up in two ways. Sometimes it's the natural glue for event-driven pieces of a design: "when a video is uploaded to S3, a function kicks off transcoding." And sometimes the interviewer asks whether the whole service could be serverless, which is really a question about cold starts, concurrency limits, cost at scale and connection management. Knowing those trade-offs well is what separates a good answer from "we'll just use Lambda."
The Execution Model
The unit of work in Lambda is an execution environment: a small, isolated sandbox that holds your function's code, its language runtime and a slice of memory and CPU. AWS runs each environment inside a Firecracker microVM, a lightweight virtual machine that boots in a fraction of a second and gives each tenant its own kernel. That's why Lambda can safely pack functions from many customers onto the same physical hosts.
An environment goes through three phases:
- Init: Lambda downloads your code, starts the environment, boots the runtime (the Node.js process, the JVM) and runs your initialization code, meaning everything outside the handler: imports, SDK clients, database connections, loading a model file.
- Invoke: Lambda calls your handler with the event. When it returns, the environment is frozen, paused with its memory intact.
- Shutdown: After some period of idleness, or whenever AWS decides to recycle it, the environment is destroyed.
The most important rule: each environment handles one request at a time. If 300 requests are in flight at the same moment, Lambda needs 300 environments. A new request that arrives while all existing environments are busy gets a brand-new one, and that new one has to go through Init first. That's a cold start. A request that lands on an idle, frozen environment skips Init entirely and runs immediately. That's a warm start.
Here's what that means for how you write a function:
import os, boto3
# Init phase: runs once per execution environment, then reused while warm.
dynamo = boto3.resource("dynamodb")
table = dynamo.Table(os.environ["TABLE"])
def handler(event, context):
# Invoke phase: runs on every request.
item = table.get_item(Key={"id": event["pathParameters"]["id"]}).get("Item")
if item is None:
return {"statusCode": 404}
return {"statusCode": 200, "body": item["body"]}
Creating the client at module level means warm invocations reuse its connection pool. Creating it inside the handler would pay connection setup on every request. But you also can't rely on anything in memory persisting: an environment can disappear between any two requests, and each concurrent request sees a different environment. Global variables are a cache, never a source of truth.