401 — Create and Invoke a Lambda Function

Beginner

Create an IAM execution role, write a minimal Python handler, zip and deploy it with aws lambda create-function, invoke synchronously, and clean up — all from the CLI.

Learning Objectives

1
Explain the Lambda execution model: event-driven, stateless, billed by invocation count and duration
2
Create an IAM execution role with the lambda.amazonaws.com trust policy
3
Write a minimal Python Lambda handler function
4
Package the handler into a zip file and deploy it with aws lambda create-function
5
Invoke the function synchronously and inspect the response payload
6
Delete the function and the execution role cleanly
Step 1

Understand the Lambda execution model

Lambda runs your code in response to events — no server to provision, patch, or manage. Before deploying your first function, confirm you have the local tools you need: the AWS CLI, Python 3, and the zip command.

Commands to Run

aws lambda list-functions --query 'Functions[*].FunctionName' --output table
python3 --version
zip --version | head -1

What This Does

Lambda functions are designed for stateless processing: each invocation runs in its own function call scope, and AWS discards all in-memory state when the execution environment is recycled.

AWS may reuse an execution environment across invocations (warm starts), so module-level code outside the handler can persist between warm calls — but your handler must never rely on that persistence for correctness.

AWS manages capacity and scaling automatically; you pay only for the time your code actually runs, billed in 1 ms increments.

The first command lists any existing functions in your account (an empty table is fine).

The Python and zip checks confirm your local tools are ready for the packaging step.

Expected Outcome

The function list is empty or shows any functions you already have — either is correct.

python3 --version prints: Python 3.x.x (any 3.x version is fine).

zip --version prints a version information line.

Pro Tips

  • 1
    Billing callout: Lambda has an always-free allowance — 1 million requests/month and 400,000 GB-seconds of compute/month — that never expires and applies on both current Free Plan and legacy accounts. These exercises use a tiny fraction of that budget.
  • 2
    Lambda functions are region-specific. list-functions shows only functions in the region configured in aws configure. Verify your active region: aws configure get region
Was this step helpful?

All Steps (0 / 9 completed)