What is AWS Lambda?
AWS Lambda is a compute service that runs your code only when something happens, then gets out of your way. You upload a small piece of code (called a “function”), tell AWS what should trigger it, and AWS runs it on demand. You never log into a server, patch an operating system, or decide how many machines to keep running. This model is called FaaS (Function as a Service), and it matters because you stop paying for idle servers and only pay while your code is actually working.
The core idea: code that runs on events
A Lambda function sits and waits. When an “event” arrives, Lambda starts a tiny, isolated environment, runs your function, and returns the result. An event can be almost anything: a file uploaded to Amazon S3 (Simple Storage Service, AWS’s object storage), an HTTP request through API Gateway, a message on a queue, or a scheduled timer.
The big win is automatic scaling. If one request arrives, Lambda runs one copy of your function. If 5,000 requests arrive at the same second, Lambda runs many copies in parallel without you configuring anything. When traffic drops to zero, you run nothing and pay nothing.
When to use Lambda (and when not to)
| Workload | Good fit for Lambda? | Why |
|---|---|---|
| API backends, webhooks | Yes | Short, bursty, scales to zero |
| Image/file processing on upload | Yes | Event-driven, parallel |
| Scheduled jobs (cron-style) | Yes | No always-on server needed |
| Glue between AWS services | Yes | Tiny event handlers |
| Long-running batch jobs (> 15 min) | No | Hard 15-minute timeout |
| Stateful apps holding sessions in memory | No | Environments are ephemeral |
| Steady, high-volume 24/7 traffic | Maybe not | A container/EC2 can be cheaper at scale |
Tip: Reach for Lambda when work is short, spiky, or infrequent. For constant heavy load, do the math against AWS Fargate or EC2 (Elastic Compute Cloud, AWS’s virtual machines) before committing.
How you are billed
Lambda billing has two parts: the number of requests (invocations) and the compute duration, measured in gigabyte-seconds. You pick how much memory the function gets (from 128 MB up to 10,240 MB), and CPU power scales with that memory. You are billed in 1-millisecond increments for how long the function runs.
A concrete example: a function with 512 MB of memory that runs for 200 ms, called 1 million times a month, costs only a few US dollars. The first 1 million requests and 400,000 GB-seconds per month are free under the AWS Free Tier. This is why Lambda is so cheap for low and medium traffic: an idle function costs nothing.
Supported runtimes
A “runtime” is the language environment Lambda uses to run your code. As of 2026, Lambda provides managed runtimes for:
| Language | Example identifiers |
|---|---|
| Node.js | nodejs20.x, nodejs22.x |
| Python | python3.12, python3.13 |
| Java | java17, java21 |
| .NET | dotnet8 |
| Ruby | ruby3.3 |
| Go / Rust / C++ | provided.al2023 (custom runtime) |
You can also package a function as a container image (up to 10 GB), which lets you bring any language or large dependency you like.
The execution model
Understanding the lifecycle prevents painful bugs:
- Cold start — On the first call (or after idle time), Lambda creates a fresh environment, downloads your code, and starts the runtime. This adds a small delay.
- Init phase — Code outside your handler function runs once. This is where you should open database connections or load config.
- Invoke phase — Your handler runs for each event.
- Reuse (warm) — The environment may stay alive and serve the next event without a cold start.
- Shutdown — After idle time, AWS quietly destroys the environment.
The key word is stateless. Each invocation should behave as if it knows nothing from the last one. Anything you write to disk lives only in /tmp (512 MB by default, configurable up to 10,240 MB) and disappears when the environment is recycled.
Gotcha: The execution environment is often reused. Global variables and
/tmpfiles set by a previous invocation can still be there on the next call in the same environment. This is great for caching a database connection, but dangerous if you accidentally leak one user’s data into another’s request. Treat global state as a cache, never as a source of truth.
Create your first function
Console steps
- Open the AWS Management Console and go to the Lambda service.
- Click Create function, then choose Author from scratch.
- Enter a Function name (for example,
hello-world). - Pick a Runtime (for example,
python3.13). - Leave the default execution role to let AWS create one, then click Create function.
- In the inline code editor, edit the handler, then click Deploy.
- Click Test, create a sample event, and run it.
AWS CLI (v2)
First package the code, then create the function. The --role is the ARN (Amazon Resource Name, a unique ID for an AWS resource) of an execution role.
zip function.zip lambda_function.py
aws lambda create-function \
--function-name hello-world \
--runtime python3.13 \
--handler lambda_function.lambda_handler \
--memory-size 256 \
--timeout 10 \
--role arn:aws:iam::123456789012:role/lambda-basic-exec \
--zip-file fileb://function.zip
Output:
{
"FunctionName": "hello-world",
"FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:hello-world",
"Runtime": "python3.13",
"Handler": "lambda_function.lambda_handler",
"MemorySize": 256,
"Timeout": 10,
"State": "Active"
}
Invoke it directly to confirm it works:
aws lambda invoke \
--function-name hello-world \
--payload '{"name":"DevCraftly"}' \
--cli-binary-format raw-in-base64-out \
response.json
Output:
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
Best Practices
- Keep functions small and single-purpose; one function should do one job well.
- Put connection setup and config loading outside the handler so it runs once per environment and is reused.
- Never rely on
/tmpor global variables to persist data between invocations — store state in DynamoDB, S3, or another service. - Set a realistic
--timeout(the default is 3 seconds) and right-size memory to balance speed and cost. - Use environment variables for configuration and AWS Secrets Manager for secrets, not hard-coded values.
- Watch the 15-minute maximum timeout; if a job needs longer, split it or move it to Fargate or Step Functions.
- Enable Amazon CloudWatch logs and set log retention so you are not paying to store logs forever.