0/174
AWS Lambda runs your code in response to an invocation without requiring you to manage a fleet of servers for the standard on-demand model. A bookstore can calculate an order total when an order event arrives. The handler is the entry point; the event carries the input and the context provides invocation information. This chapter covers ordinary event functions, not durable workflows, web-function hosting or Lambda Managed Instances, whose execution and billing models differ.
Save this Python example as lambda_function.py. The handler setting for a ZIP deployment would be lambda_function.lambda_handler. No SDK, account or network is needed for the local example. Call lambda_handler({"quantity": 3}, None) to get {"total_cents": 750}. The price is fictional and in integer cents. A zero, negative, Boolean or missing quantity raises ValueError. The return value is a Python dictionary; HTTP integrations may require a different response contract.
def lambda_handler(event, context): quantity = event.get("quantity") if type(quantity) is not int or quantity < 1: raise ValueError("quantity must be a positive integer") return {"total_cents": quantity * 250}Lambda prepares an execution environment, loads the runtime and runs initialization code before handling an event. Starting a new environment can add latency (a cold start). Later invocations may reuse an environment, including module-level objects and temporary files. Reuse is an optimization, not a promise: environments can be replaced at any time. Keep reusable clients outside the handler where appropriate, but never store a customer’s private request state in a shared global variable. Persist important state in a durable external store.
For standard event functions, choose a supported runtime and CPU architecture. A ZIP archive contains code and dependencies; compatible layers can supply additional libraries for ZIP functions. A container image is a separate packaging option, not a continuously running server. Native libraries must match the target architecture and OS. Environment variables hold configuration such as a table name; use an approved secret store for sensitive values. The execution role governs AWS actions performed by the code, while the invoker needs separate permission to invoke it. A VPC attachment can enable private database connectivity but does not itself grant IAM access or internet access.
Try quantities 1, 4 and 0; predict 250, 1000 and a validation error. Calling the same valid event twice returns the same total because the example has no side effects. Do not infer that repeated real order submissions are safe. The next lessons explain concurrent invocations and durable duplicate protection. No deployment is required.
Solidify your learning with the interactive assessment directly below this lesson.
Reason about fictional Lambda workloads without an AWS account.
What does the event argument carry?
A warm environment handled an earlier request. What can you assume?
What gives the handler permission to read an AWS table?
A native dependency was built for the wrong CPU architecture. What is the correct remedy?
What does the example return for quantity 4?