Introduction

Amazon Web Services Lambda, commonly written as AWS Lambda, is a serverless compute service. The term serverless describes a model in which the underlying servers are provisioned, scaled, and maintained by the cloud provider, and the customer is charged only for the compute that is actually consumed. The term compute refers to the processing capacity that is used to run application code. This guide explains what AWS Lambda is, the problem it was created to solve, how it operates internally, and the situations in which its use is and is not appropriate.

What is it?

AWS Lambda is a service on which code is run in response to events, without any server being provisioned or managed by the developer. A single unit of deployed code is called a function. A function is executed only when it is triggered by an event, which is a structured message that describes something that has happened, for example an HTTP request arriving at an endpoint or a file being uploaded to storage. When no events are being processed, no function instances are run and no charge is incurred.

Because the developer supplies only the function code and its configuration, the responsibility for the operating system, the servers, and the scaling of capacity is transferred to AWS. The developer is left to focus on the logic of the function itself.

Why does it exist?

Before serverless compute became widely available, application code was typically run on servers that had to be arranged in advance. A server had to be sized for peak demand, kept running continuously, patched for security, and paid for even while it remained idle. Scaling was performed manually or through additional configuration, and a sudden increase in traffic could overwhelm a fixed set of servers.

AWS Lambda was created to remove this operational burden. Capacity is allocated automatically as demand rises and is released as demand falls. Billing is based on the number of requests and the duration of each execution rather than on reserved server time. As a result, small workloads can be run at very low cost, and large workloads can be scaled without capacity being planned in advance.

How it works

When an event is delivered to Lambda, an execution environment for the corresponding function is located or created. An execution environment is an isolated, temporary container that holds the language runtime and the function code. A runtime is the software that interprets and runs code written in a particular language, such as Node.js, Python, or Java. The function code contains a handler, which is the specific method that Lambda calls and to which the event is passed.

If a warm execution environment is already available, the handler is invoked immediately. If no environment is available, a new one is initialized first. This initialization step is known as a cold start, and it adds delay to the first invocation. This delay is referred to as latency, which is the time that passes between a request being made and a response being produced.

Scaling is achieved by many execution environments being run in parallel. The number of environments that are active at the same moment is called concurrency. If one thousand events arrive at once, up to one thousand environments may be run concurrently, subject to the limits that have been configured for the account. Each function is billed according to the number of invocations and the total execution duration, which is measured in milliseconds and multiplied by the amount of memory that has been allocated.

Architecture diagram

The relationship between an event source, the Lambda service, and the downstream services that a function calls is shown below. An event source is any service or client that produces the events that trigger a function.

Event source AWS Lambda service Downstream services (API Gateway, S3, --> (locates or creates an --> (DynamoDB, S3, EventBridge, SDK) execution environment, other APIs) then runs your handler) | v Logs and metrics are sent to Amazon CloudWatch

Advantages

Disadvantages

Common use cases

Best practices

Common mistakes

AWS Lambda is rarely used in isolation. It is most often combined with the services below. Dedicated guides for each are being added to this library.

Further reading in this library

Frequently Asked Questions

Which programming languages are supported by AWS Lambda?
AWS Lambda provides managed runtimes for Node.js, Python, Java, .NET, Go, and Ruby. Languages that are not provided as managed runtimes can be supported through a custom runtime or a container image.
How is AWS Lambda priced?
Charges are based on the number of requests and on the duration of each execution. Duration is measured in milliseconds and is multiplied by the amount of memory that is allocated. When no events are being processed, no charge is incurred.
What is a cold start, and how can it be reduced?
A cold start is the additional delay that occurs when a new execution environment must be initialized because no warm environment is available. It can be reduced by keeping deployment packages small, by choosing a runtime that initializes quickly, and, for latency-sensitive workloads, by using provisioned concurrency, which keeps a number of environments initialized in advance.
What is the maximum execution time of a Lambda function?
A single invocation may run for a maximum of fifteen minutes. Work that is expected to exceed this limit should be divided into smaller steps or coordinated by AWS Step Functions.
Is AWS Lambda always the cheapest option?
No. For intermittent or unpredictable workloads, Lambda is often the most cost-effective choice. For workloads that run continuously at high, steady volume, a container or server-based service may be less expensive.
AWS Serverless Architecture Handbook cover
Go deeper ยท Book as a Service™
AWS Serverless Architecture Handbook

This article is the summary. The book is the full, continuously updated reference: production patterns, security, cost control, and complete architectures for Lambda and the wider serverless stack.

View the book