Skip to Content

AWS Lambda

What AWS Lambda is and why it differs from EC2, its languages and container images, its main integrations, two classic examples, and how it's priced.

What it is

AWS Lambda runs code as functions in response to events, without servers.

EC2 versus Lambda: EC2 gives virtual servers limited by RAM and CPU that run all the time and that scale by adding or removing servers; Lambda gives virtual functions with no servers, limited to short runs, run on demand and scaled automatically EC2 versus Lambda: EC2 gives virtual servers limited by RAM and CPU that run all the time and that scale by adding or removing servers; Lambda gives virtual functions with no servers, limited to short runs, run on demand and scaled automatically

Servers that run all the time, or functions that run when needed.
  • Amazon EC2: virtual servers in the cloud, limited by RAM and CPU, running continuously. Scaling means adding or removing servers.
  • AWS Lambda: virtual functions, with no servers to manage. Limited by time (short executions). Runs on demand. Scaling is automatic.

Benefits

  • Pay-per-use pricing: per request and per compute time. The free tier covers 1,000,000 requests and 400,000 GB-seconds of compute every month.
  • Integrated with the whole AWS suite of services.
  • Many programming languages.
  • Monitoring through CloudWatch (logs and metrics built in).
  • More resources on request per function: up to 10 GB of RAM.
  • More RAM also gives more CPU and network: CPU is allocated in proportion to memory.

Languages

  • Node.js (JavaScript), Python, Java, C# (.NET) and PowerShell, Ruby.
  • Custom runtimes through the OS-only runtime and the Runtime API, for languages such as Rust or Go.
  • Lambda container images: package a function as a container image (up to 10 GB). The image must implement the Lambda Runtime API. For running arbitrary Docker images, ECS or Fargate is preferred.

Main integrations

Lambda is triggered by, or works with:

  • API Gateway (REST APIs), Kinesis (streams), DynamoDB (Streams), S3 (events), CloudFront (Lambda@Edge);
  • EventBridge (events and schedules), CloudWatch Logs, SNS, SQS, Cognito.

Example: serverless thumbnails

A serverless thumbnail pipeline: a new image in Amazon S3 triggers a Lambda function that writes a thumbnail back to S3 and saves the image name, size and date to DynamoDB A serverless thumbnail pipeline: a new image in Amazon S3 triggers a Lambda function that writes a thumbnail back to S3 and saves the image name, size and date to DynamoDB

An upload triggers code that does the work.
  1. A new image is uploaded to S3.
  2. The S3 event notification triggers a Lambda function.
  3. The function creates a thumbnail and saves it to S3, and writes the image's metadata (name, size, creation date) to DynamoDB.

Example: serverless cron job

An EventBridge schedule triggers a Lambda function every hour to perform a task, such as cleaning up old records. No EC2 instance has to run all day just for a scheduled script.

Pricing

  • Per request: the first 1,000,000 requests a month are free; then $0.20 per million requests.
  • Per duration, billed in 1 ms increments, in GB-seconds (memory × time):
    • 400,000 GB-seconds a month are free: that's 400,000 seconds for a 1 GB function, or 3,200,000 seconds for a 128 MB one;
    • after that, $0.0000166667 per GB-second, about $1.00 for 60,000 GB-seconds (x86 prices in us-east-1; Arm/Graviton functions are cheaper).
  • Lambda is usually very cheap to run, so it's very popular.

Updated since the slides

The slides say "$1.00 for 600,000 GB-seconds". At AWS's published price of $0.0000166667 per GB-second, $1.00 buys about 60,000 GB-seconds. The exam doesn't ask for this calculation, but the model matters: requests plus memory × duration.

Real-world examples

  • A photo app creates thumbnails of every upload with Lambda, paying only for the seconds each resize takes.
  • A company runs 200 small scheduled scripts as Lambda functions instead of a fleet of cron servers.
  • A start-up's API on API Gateway and Lambda costs a few dollars a month until traffic grows.

Exam tips

  • "Run code in response to events without managing servers" → Lambda.
  • "Process files as they're uploaded to S3" → S3 event → Lambda.
  • "Run a script every hour without a server" → EventBridge schedule → Lambda.
  • "Function needs more CPU" → increase its memory.
  • "Run a Docker image as a function" → Lambda container image (if it implements the Runtime API); otherwise ECS/Fargate.

Common traps

  • "Lambda for a long-running process that runs all day." Lambda is for short executions; use EC2, ECS or Fargate.
  • "Choose the number of vCPUs for a Lambda function." Memory is the setting; CPU follows.

Hands-on lab: a function triggered by S3

Goal: create a Lambda function that logs every object uploaded to a bucket.

Costs money

A few invocations and a small object cost far less than a cent. Any Region works.

Create the function

Lambda → Create function → Author from scratch. Name saa-s3-logger, runtime Python 3.13 (or the latest), architecture arm64. Create. Replace the code with:

def lambda_handler(event, context):
    for record in event["Records"]:
        print("New object:", record["s3"]["object"]["key"])

Choose Deploy.

Add the S3 trigger

Add trigger → S3, choose a lab bucket, event type All object create events, tick the recursive-invocation acknowledgement. Add.

Upload and check the logs

Upload a file to the bucket. In the function, open Monitor → View CloudWatch logs, then the newest log stream: you see New object: <your file>, plus a REPORT line with the duration and memory used (the billed time).

Expected result: every upload runs your code within a second or two, with no servers.

Clean up: delete the trigger and the function, and the function's log group in CloudWatch Logs.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt