Stop 11 · Serverless & Event-Driven AppsTopics · 1/10

AWS Lambda Basics

Part of the AWS Roadmap

In short

A function is the smallest thing you can deploy to the cloud: one handler, a few lines, triggered by an event. Learn its shape once and use it on AWS, Google Cloud or Azure.

AWS Lambda runs your function code in response to events without you managing servers, and you pay for requests and compute time. A function has a runtime such as Node.js or Python, a handler, a memory setting and an execution role for permissions.

The blob character presses a button on a chai vending machine as a cup fills.

Think of it like this

The office chai machine

Press the button, the machine wakes up, makes one cup and goes idle. Ten people press at once? A bigger office would have ten machines. It does one small job per press and never sits there brewing for nobody.

Watch it happen

One request through a function

Browser
API Gateway
Lambda

GET /hello?name=Riya

1/5The browser calls a public URL. The gateway owns that URL, not your code.

The big ideas

What you need to know

Handler shape

Every FaaS platform gives your function an event (what happened) and context, and expects a result back.

Many triggers

HTTP, storage uploads, queue messages, database changes and cron schedules can all invoke a function.

Stateless

Don't keep data in memory or local disk between calls. Store it in a database or object storage.

Same idea, three clouds

AWS Lambda, Google Cloud Run functions and Azure Functions differ in limits and tooling, not in the basic model.

See it in code

Walk through the code

js
1export const handler = async (event) => {2  const name = event.queryStringParameters?.name ?? "friend";3 4  if (name.length > 40) {5    return { statusCode: 400, body: "Name too long" };6  }7 8  return {9    statusCode: 200,10    headers: { "content-type": "application/json" },11    body: JSON.stringify({ message: `Hello, ${name}` }),12  };13};

1/4Lambda calls this exported async function once per event.

A Node.js Lambda handler for an HTTP API. The body must be a string, so objects go through JSON.stringify.

Save yourself the pain

Common mistakes

Oops

Writing a temp file in one invocation and expecting it in the next.

Do this instead

Each call may land on a different copy. Use S3 or a database for anything you need later.

Oops

Putting a whole Express app with 300 routes into one giant function.

Do this instead

Fine for small apps, but large ones get slow starts and messy permissions. Split by area when it grows.

Your mission

Write and break a function

~30 min

Quick check

Why should a Lambda function avoid storing user sessions in a global variable?

Go deeper

  1. 1Write a small Lambda function in the console and test it with a sample event.
  2. 2Change the memory setting and note how it also changes available CPU.
  3. 3Give the function's execution role only the permissions it needs.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge