aws-lambda-web-functions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAWS Lambda Web Functions
AWS Lambda Web Functions
The AWS MCP server is recommended for sandboxed execution and audit logging.Ifis not recognized, update the AWS CLI (see references/deployment.md). A missing command means an outdated CLI, not an unavailable feature.aws lambda-web
Help users build and deploy Node.js HTTP applications on AWS Lambda Web Functions. Your app listens on a port, handles HTTP natively, and gets a global HTTPS URL. Develop locally with , deploy the same code to AWS.
node index.js推荐使用 AWS MCP server,以便进行沙箱执行和审计日志记录。如果无法识别,请更新 AWS CLI(参见 references/deployment.md)。 命令缺失意味着 CLI 版本过旧,而不是该功能不可用。aws lambda-web
帮助用户构建并部署 AWS Lambda Web Functions 上的 Node.js HTTP 应用。应用在端口上监听,原生处理 HTTP 请求,并获得一个全球 HTTPS URL。可在本地通过 开发,并将同一份代码部署到 AWS。
node index.jsWhen to Use Web Functions
何时使用 Web Functions
- Interactive HTTP traffic someone is waiting on: web app, HTTP API, or SSR site.
- The app is already a port-listening HTTP server (Express, Fastify, Hono) and should stay one — no handler rewrite, no adapter library.
- Handlers are I/O-bound (database, cache, another API, a model endpoint), so many requests share one execution environment and time spent waiting on I/O is not billed as CPU.
- Response streaming matters: SSE, chunked transfer, or proxying LLM tokens as they are produced.
- The endpoint should be reachable worldwide on one hostname without assembling CloudFront, API Gateway and Route53 by hand.
- Traffic is spiky or unpredictable and should scale to zero.
- 有人正在等待响应的交互式 HTTP 流量:Web 应用、HTTP API 或 SSR 网站。
- 应用已经是一个监听端口的 HTTP 服务器(Express、Fastify、Hono),并且希望保持这种形态——无需重写 handler,也无需 adapter library。
- Handler 属于 I/O 密集型(数据库、缓存、另一个 API 或模型端点),因此许多请求可以共享同一个执行环境,且等待 I/O 的时间不会按 CPU 计费。
- 响应流式传输很重要:SSE、分块传输,或在生成 LLM token 时将其代理到客户端。
- 端点应通过一个 hostname 在全球范围内访问,而无需手动组装 CloudFront、API Gateway 和 Route53。
- 流量存在突发性或不可预测,需要自动缩容至零。
Hard preconditions
硬性前提条件
Any "no" rules Web Functions out. Invoke the skill to find a suitable service.
aws-serverless- Runtime is Node.js — . Python, Java, Go, .NET and Rust are not supported. Confirm the supported runtime list with
nodejs24.xor the AWS docs before routing a caller away.aws lambda-web help - Every native dependency has an arm64 (Graviton) build.
- The work is an HTTP request/response (or stream) that a caller is waiting on. Queue, event-stream source (Kinesis, DynamoDB Streams), schedule, or event trigger are not supported.
- One request completes within the 900 s (15 min) request timeout.
- The app fits a 512 MB package and a 2 vCPU / 2 GB environment (see Service Limits).
- Environments are stateless — any environment can serve any request. There is no affinity between an end user's session and a specific environment.
- The app tolerates sharing an environment with concurrent requests. Strict per-request isolation is not supported.
只要有任意一项不满足,Web Functions 就不适用。调用 skill 以寻找合适的服务。
aws-serverless- Runtime 为 Node.js——。不支持 Python、Java、Go、.NET 和 Rust。在将调用方转交给其他方案前,请通过
nodejs24.x或 AWS 文档确认受支持的 runtime 列表。aws lambda-web help - 每个原生依赖都有 arm64 (Graviton) 构建版本。
- 工作负载是调用方正在等待完成的 HTTP request/response(或 stream)。不支持队列、事件流源(Kinesis、DynamoDB Streams)、定时任务或事件触发器。
- 单个请求需在 900 s(15 min)请求超时内完成。
- 应用需适配 512 MB 部署包和 2 vCPU / 2 GB 环境(参见服务限制)。
- 环境是无状态的——任何环境都可以处理任何请求。终端用户会话与特定环境之间不存在亲和性。
- 应用能够承受与并发请求共享同一环境。不支持严格的逐请求隔离。
Use Cases
使用场景
"What I'm building" → how it lands here.
| The user is building | Shape on Web Functions |
|---|---|
| JSON HTTP API or REST backend | Express or Hono router, I/O-bound handlers, DynamoDB or RDS behind it → architecture-patterns.md |
| Server-side-rendered site or app | Render HTML per request from |
| LLM or AI gateway streaming tokens to a browser | SSE or chunked responses; raise |
| Webhook receiver (Stripe, GitHub, Slack) | Verify the sender's signature against a signing secret from Secrets Manager before acting on the payload, then acknowledge fast and finish the work after responding — the environment stays running → architecture-patterns.md |
| Mobile or web backend-for-frontend aggregating several services | One environment fans several requests out concurrently, and time spent waiting is not billed as CPU |
| Single-page app plus its API from one deployment | |
| Internal service-to-service HTTP endpoint | |
| Globally low-latency endpoint on a single hostname | |
| App that calls RDS, ElastiCache or other private VPC resources | An egress network connector referenced from the revision; the endpoint stays public → networking.md |
“用户正在构建什么” → “如何映射到 Web Functions”。
| 用户正在构建 | 在 Web Functions 上的形态 |
|---|---|
| JSON HTTP API 或 REST 后端 | Express 或 Hono router、I/O 密集型 handler,以及后端的 DynamoDB 或 RDS → architecture-patterns.md |
| 服务端渲染的网站或应用 | 在每次请求时通过 |
| 将 LLM 或 AI gateway 的 token 流式传输到浏览器 | 使用 SSE 或分块响应;将 |
| Webhook 接收器(Stripe、GitHub、Slack) | 在处理 payload 前,使用 Secrets Manager 中的 signing secret 验证发送方签名,然后快速确认响应,并在响应后继续处理工作——环境会保持运行 → architecture-patterns.md |
| 聚合多个服务的移动端或 Web backend-for-frontend | 一个环境并发发出多个请求,且等待时间不按 CPU 计费 |
| 通过一次部署同时提供单页应用及其 API | 使用 |
| 内部服务间 HTTP 端点 | 使用 |
| 通过单一 hostname 提供全球低延迟端点 | 使用 |
| 调用 RDS、ElastiCache 或其他私有 VPC 资源的应用 | 在 revision 中引用 egress network connector;端点仍保持公开 → networking.md |
Workflow
工作流
Default to the CLI — it's the quickest path. Use when the user asks for a single command or says they already deploy with it. It packages, uploads and creates or updates a project directory in one command, and scaffolds and deploys a starter app (see Deploy (CLI) below). Otherwise, including for updates to an existing function, use the steps below. Use CloudFormation, CDK, SAM or Terraform when the user wants infrastructure-as-code or a repeatable production stack (see references/infrastructure-as-code.md).
aws lambda-webaws lambda-web deploy--hello-world- Scaffold - Express is inline below; for Hono or Fastify, read references/frameworks.md
- Choose endpoint auth — ask the user before deploying (see below); endpoints are public unless they pick
IamAuth - Test locally —
node index.js - Package — ZIP with production deps (up to 512 MB)
- Deploy —
aws lambda-web create-web-function - Verify — get endpoint URL, then curl
- Iterate — create a new revision
Public by default. Aendpoint is reachable by anyone on the internet with the URL — there is no AWS-layer auth and no built-in WAF, and the URL is not a secret. Before deploying, ask the user which they want:ApplicationManaged
(public) — the app must implement its own authentication/authorization, input validation, and rate limiting.ApplicationManaged (internal) — only callers with valid IAM credentials (SigV4-signed) can reach it.IamAuthIf the endpoint is public, the app owns its security. See references/iam-and-security.md for the security checklist (auth, input validation, rate limiting, WAF/CloudFront, least-privilege role, secrets).
默认使用 CLI——这是最快的方式。当用户要求使用单条命令,或表示自己已使用该命令部署时,使用 。它通过一条命令完成打包、上传,并创建或更新项目目录; 则会生成并部署一个入门应用(见下文的“部署(CLI)”)。其他情况,包括更新现有 function,均使用以下步骤。当用户需要 infrastructure-as-code 或可复现的生产技术栈时,使用 CloudFormation、CDK、SAM 或 Terraform(参见 references/infrastructure-as-code.md)。
aws lambda-webaws lambda-web deploy--hello-world- 创建脚手架——Express 的代码内联如下;对于 Hono 或 Fastify,请阅读 references/frameworks.md
- 选择 endpoint auth——部署前询问用户(见下文);除非用户选择 ,否则 endpoint 均为公开
IamAuth - 在本地测试——
node index.js - 打包——使用生产依赖生成 ZIP(最大 512 MB)
- 部署——
aws lambda-web create-web-function - 验证——获取 endpoint URL,然后执行 curl
- 迭代——创建新的 revision
默认公开。endpoint 可由互联网上任何知道该 URL 的人访问——AWS 层没有 auth,也没有内置 WAF,而且 URL 并非秘密。部署前,询问用户选择哪一种:ApplicationManaged
(公开)——应用必须自行实现身份验证和授权、输入验证以及速率限制。ApplicationManaged (内部)——只有持有有效 IAM 凭据(SigV4 签名)的调用方可以访问。IamAuth如果 endpoint 公开,安全由应用负责。参见 references/iam-and-security.md 中的安全检查清单(auth、输入验证、速率限制、WAF/CloudFront、最小权限 role、secrets)。
Quick Start (Express)
快速入门(Express)
javascript
// index.js
import express from 'express';
import helmet from 'helmet';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
// The runtime probes this exact host:port for readiness.
const [HOST, PORT] = (process.env.AWS_LAMBDA_HTTP_ENDPOINT || '0.0.0.0:3000').split(':');
app.use(helmet());
app.use(express.static(join(__dirname, 'public')));
app.use(express.json({ limit: '10mb' }));
app.get('/api/health', (req, res) => res.json({ status: 'ok' }));
app.listen(PORT, HOST, () => console.log(`Listening on ${HOST}:${PORT}`));json
{
"name": "my-web-app",
"type": "module",
"scripts": { "start": "node index.js", "dev": "node --watch index.js" },
"dependencies": { "express": "^5.0.0", "helmet": "^8.0.0" }
}helmetX-Content-Type-OptionsX-Frame-OptionsReferrer-PolicyApplicationManagedjavascript
// index.js
import express from 'express';
import helmet from 'helmet';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
// The runtime probes this exact host:port for readiness.
const [HOST, PORT] = (process.env.AWS_LAMBDA_HTTP_ENDPOINT || '0.0.0.0:3000').split(':');
app.use(helmet());
app.use(express.static(join(__dirname, 'public')));
app.use(express.json({ limit: '10mb' }));
app.get('/api/health', (req, res) => res.json({ status: 'ok' }));
app.listen(PORT, HOST, () => console.log(`Listening on ${HOST}:${PORT}`));json
{
"name": "my-web-app",
"type": "module",
"scripts": { "start": "node index.js", "dev": "node --watch index.js" },
"dependencies": { "express": "^5.0.0", "helmet": "^8.0.0" }
}helmetX-Content-Type-OptionsX-Frame-OptionsReferrer-PolicyApplicationManagedResource Naming
资源命名
All resources are project-scoped. Derive names from the function name:
| Resource | Default Pattern | Example |
|---|---|---|
| Function | | |
| IAM Role | | |
| S3 bucket | | |
| S3 key | | |
| DynamoDB | | |
Function names must be unique across all Regions in the account — the same name cannot exist in two Regions, and a collision fails at create time with a conflict error.
Ask the user which S3 strategy they prefer:
- Separate bucket per project (default) — fully isolated, easy cleanup
- Shared bucket across projects (e.g., ) — fewer resources, key-prefix isolation
{account}-web-code - Custom — user provides their own bucket name and key prefix
aws lambda-web deployawscli-lambdaweb-{name}awscli-lambdaweb-{account}-{region}-an所有资源均按项目划分范围。请根据 function 名称派生资源名称:
| 资源 | 默认模式 | 示例 |
|---|---|---|
| Function | | |
| IAM Role | | |
| S3 bucket | | |
| S3 key | | |
| DynamoDB | | |
Function 名称在同一账户的所有 Region 中必须唯一——同一名称不能存在于两个 Region 中,否则创建时会因冲突而失败。
询问用户偏好的 S3 策略:
- 每个项目使用独立 bucket(默认)——完全隔离,便于清理
- 多个项目共享 bucket(例如 )——资源更少,通过 key prefix 隔离
{account}-web-code - 自定义——由用户提供自己的 bucket 名称和 key prefix
aws lambda-web deployawscli-lambdaweb-{name}awscli-lambdaweb-{account}-{region}-anDeploy (CLI)
部署(CLI)
The CLI is the primary, supported path — use it by default. The flow is:
one-time S3 code bucket (versioning + Lambda read access), then →
→ → with and
, then wait for the endpoint to reach . If the project has a
build step, run it before the prune — removes the bundler.
aws lambda-webnpm ci --omit=devzipaws s3 cpaws lambda-web create-web-function--revision-config--endpoint-configActivenpm ci --omit=devWhen the user asks for one command or already deploys with it, (ask the user which auth type) zips, uploads, and creates the function, a code bucket, a role and a endpoint; rerun it without to ship changes. Install dependencies first.
aws lambda-web deploy --name {name} --code . --create --auth-type {auth-type}dev--createFor the full commands, and field rules, packaging, timeout,
concurrency, environment variables, logging config and every flag, see
references/deployment.md. First-time setup (bucket, execution
role, first deploy) is in references/getting-started.md.
For CloudFormation, CDK or SAM, see
references/infrastructure-as-code.md.
EndpointConfigRevisionConfigdeployaws lambda-webnpm ci --omit=devzipaws s3 cp--revision-config--endpoint-configaws lambda-web create-web-functionActivenpm ci --omit=dev当用户要求使用一条命令完成,或表示自己已使用该命令部署时,运行 (让用户选择 auth type),即可完成 zip、上传,并创建 function、代码 bucket、role 和 endpoint;再次运行时不带 即可发布变更。请先安装依赖。
aws lambda-web deploy --name {name} --code . --create --auth-type {auth-type}dev--create有关完整命令、 和 字段规则、打包、超时、并发、环境变量、日志配置以及每个 flag,请参阅
references/deployment.md。首次设置(bucket、execution role、首次部署)请参阅 references/getting-started.md。
有关 CloudFormation、CDK 或 SAM,请参阅
references/infrastructure-as-code.md。
EndpointConfigRevisionConfigdeployMulti-Region
多区域
Multi-region is supported: a endpoint serves your revision from several
regions behind one hostname, with Route53 routing each caller to the nearest region.
additionally gives each region its own domain name. Both require
, and the app must be region-agnostic — consecutive requests
from one client can land in different regions, so keep state in a replicated store, never
in the execution environment or .
MultiRegionPerRegionautoDeploymentMode: Disabled/tmpSee references/domains-and-routing.md for endpoint
types, domains, traffic routing and the multi-region setup commands.
支持多区域部署: endpoint 会通过 一个 hostname 在多个 Region 中提供 revision,并由 Route53 将每个调用方路由到最近的 Region。 还会为每个 Region 分配各自的域名。两者都要求设置 ,并且应用必须与 Region 无关——同一客户端发出的连续请求可能落在不同 Region,因此应将状态保存在复制的存储中,绝不能保存在执行环境或 中。
MultiRegionPerRegionautoDeploymentMode: Disabled/tmp有关 endpoint 类型、域名、流量路由和多区域设置命令,请参阅 references/domains-and-routing.md。
VPC Egress
VPC Egress
By default a revision reaches the public internet over IPv4 and nothing private. To reach your VPC, put a version-qualified egress network connector ARN (, compute type ) in the revision's , serve it from a endpoint, and read the field back after every revision. See references/networking.md, or references/infrastructure-as-code.md to declare the connector and the revision in one stack.
aws lambda-core create-network-connectorWebFunctionserviceConfig.egressNetworkConnectorArnHomeRegion默认情况下,revision 通过 IPv4 访问公共互联网,无法访问任何私有资源。要访问 VPC,请在 revision 的 中设置带版本限定符的 egress network connector ARN(,compute type 为 ),通过 endpoint 提供服务,并在每次 revision 后回读该字段。参见 references/networking.md,或参阅 references/infrastructure-as-code.md,在同一个技术栈中声明 connector 和 revision。
serviceConfig.egressNetworkConnectorArnaws lambda-core create-network-connectorWebFunctionHomeRegionVerify
验证
bash
aws lambda-web get-web-function-endpoint \
--function-name {name} --endpoint-name default --region {region}bash
aws lambda-web get-web-function-endpoint \
--function-name {name} --endpoint-name default --region {region}curl the domainName from the response
curl the domainName from the response
curl https://{domainName}/api/health
A revision that fails to start still goes `Active`, so curl a real route after every deploy. `aws lambda-web deploy` prints the endpoint as `endpointUrl` and names its first endpoint `dev`.
> Prefer waiters (`aws lambda-web wait ...`) over polling loops or `sleep`. Available: `web-function-active`, `web-function-revision-active`, `web-function-endpoint-active`, `web-function-endpoint-updated`, `web-function-endpoint-deleted`, `web-function-deleted`curl https://{domainName}/api/health
启动失败的 revision 仍会变为 `Active`,因此每次部署后都应 curl 一个真实路由。`aws lambda-web deploy` 会将 endpoint 打印为 `endpointUrl`,并将其首个 endpoint 命名为 `dev`。
> 优先使用 waiters(`aws lambda-web wait ...`),而不是 polling loop 或 `sleep`。可用项包括:`web-function-active`、`web-function-revision-active`、`web-function-endpoint-active`、`web-function-endpoint-updated`、`web-function-endpoint-deleted`、`web-function-deleted`Update Code
更新代码
bash
zip -r function.zip index.js package.json node_modules/ public/ \
-x "*.test.*" -x ".git/*" -x ".env" -x "node_modules/.cache/*"
aws s3 cp function.zip s3://{bucket}/{name}/function.zip
aws lambda-web create-web-function-revision \
--function-name {name} \
--build-config '{
"codeConfig": {"s3Object": {"bucket": "{bucket}", "key": "{name}/function.zip"}},
"runtimeConfig": {"runtime": "nodejs24.x"}
}' \
--service-config '{"executionRoleArn": "arn:aws:iam::{account}:role/{name}-web-role"}' \
--region {region}bash
zip -r function.zip index.js package.json node_modules/ public/ \
-x "*.test.*" -x ".git/*" -x ".env" -x "node_modules/.cache/*"
aws s3 cp function.zip s3://{bucket}/{name}/function.zip
aws lambda-web create-web-function-revision \
--function-name {name} \
--build-config '{
"codeConfig": {
"s3Object": {"bucket": "{bucket}", "key": "{name}/function.zip"}
},
"runtimeConfig": {"runtime": "nodejs24.x"}
}' \
--service-config '{"executionRoleArn": "arn:aws:iam::{account}:role/{name}-web-role"}' \
--region {region}Service Limits
服务限制
Subject to change — check the
Lambda quotas page
for the authoritative values.
| Scope | Resource | Limit |
|---|---|---|
| Account | Regional vCPU limit | 2,000 |
| Account | Regional account rate limit | 100,000 RPS |
| Account | Regional per-endpoint maximum rate limit | 10,000 RPS |
| Per function | Endpoints | 10 |
| Per function | Revisions retained | 50 (oldest unreferenced revisions are deleted automatically) |
| Per function | Environment variables | 32 KB total |
| Per function | Deployment package | 512 MB |
| Per function | Function and endpoint name | 64 characters |
| Per endpoint | Endpoint rate limit | Limited by Regional per-endpoint maximum rate limit (above). User can optionally configure a lower rate limit on individual endpoints via |
| Per endpoint | Active revisions | 2 |
| Per environment | vCPU | 2 |
| Per environment | Memory | 2 GB |
| Per environment | Concurrency | 1–128 requests (see scaling-and-concurrency.md) |
| Per environment | Network bandwidth | 1 Gbps |
| Per environment | | 512 MB |
| Per environment | File descriptors | 1,024 |
| Per environment | Processes and threads | 1,024 |
| Per environment | Init timeout | 10 seconds |
| Per request | Timeout | 3–900 s (default 30 s) |
| Per request | Header count | 6,300 |
以下限制可能发生变化——请以
Lambda quotas page
中的权威数值为准。
| 范围 | 资源 | 限制 |
|---|---|---|
| Account | 区域 vCPU 限额 | 2,000 |
| Account | 区域账户速率限制 | 100,000 RPS |
| Account | 区域单 endpoint 最大速率限制 | 10,000 RPS |
| 每个 function | Endpoint 数量 | 10 |
| 每个 function | 保留的 revision 数量 | 50(最旧的未引用 revision 会自动删除) |
| 每个 function | 环境变量 | 总计 32 KB |
| 每个 function | 部署包 | 512 MB |
| 每个 function | Function 和 endpoint 名称 | 64 个字符 |
| 每个 endpoint | Endpoint 速率限制 | 受上方的区域单 endpoint 最大速率限制约束。用户也可以通过 |
| 每个 endpoint | 活跃 revision | 2 |
| 每个环境 | vCPU | 2 |
| 每个环境 | 内存 | 2 GB |
| 每个环境 | 并发数 | 1–128 个请求(参见 scaling-and-concurrency.md) |
| 每个环境 | 网络带宽 | 1 Gbps |
| 每个环境 | | 512 MB |
| 每个环境 | 文件描述符 | 1,024 |
| 每个环境 | 进程和线程 | 1,024 |
| 每个环境 | 初始化超时 | 10 秒 |
| 每个请求 | 超时 | 3–900 s(默认 30 s) |
| 每个请求 | 标头数量 | 6,300 |
Cost Model
成本模型
Four billing dimensions: CPU, memory, requests, and data transfer. Check the
Lambda pricing page for current rates — do not quote
rates from memory. What decides an architecture is the shape of the bill:
- Active CPU, not wall-clock duration. Time a request spends blocked on I/O — a database call, another API, a model endpoint — is not billed as CPU. Event Functions bill GB-seconds of wall-clock duration instead, so the same handler bills differently on each.
- Memory is billed on consumption, not on a provisioned setting. An environment is 2 vCPU and 2 GB (see Service Limits) and there is no memory dial, so the familiar Event Functions move of raising memory to shorten duration has no equivalent here.
- Concurrency is the density lever. One environment serves up to requests, so an I/O-bound app keeps many requests in flight per environment instead of occupying one environment each.
maxConcurrencyPerEnvironment
Two consequences worth planning around:
- I/O-bound workloads are the economical case. A handler that spends most of its life waiting on the network, sharing an environment with other requests, costs less here than the same handler billed per-invoke on wall-clock duration.
- CPU-bound workloads give that advantage back. Heavy rendering, image processing or crypto burns billable CPU wherever it runs, and an environment is allocated 2 vCPU — of which a single-threaded Node process uses only one (see Anti-Patterns). For sustained high-volume CPU work, especially where committed-use discounts apply, compare .
aws-lambda-managed-instances
An endpoint's scaling controls and can be used to limit potential cost. See scaling-and-concurrency.md for more details.
scalingConfig.maxEnvironmentsthrottleConfig.rateLimit计费有四个维度:CPU、内存、请求和数据传输。请查看 Lambda pricing page 获取当前费率——不要凭记忆引用费率。决定架构的是账单形态:
- 按活跃 CPU 计费,而非 wall-clock duration。 请求等待 I/O 的时间——例如数据库调用、另一个 API 或模型端点——不按 CPU 计费。Event Functions 则按 wall-clock duration 的 GB-seconds 计费,因此同一个 handler 在两种模式下的计费方式不同。
- 按内存消耗量计费,而非预置设置。 环境为 2 vCPU 和 2 GB(参见服务限制),没有可调节的内存设置,因此 Event Functions 中通过提高内存来缩短耗时的常见做法在此没有对应方案。
- 并发数是密度调节杠杆。 一个环境最多可处理 个请求,因此 I/O 密集型应用可以在每个环境中保持许多请求并行,而无需为每个请求独占一个环境。
maxConcurrencyPerEnvironment
有两个值得提前规划的结果:
- I/O 密集型工作负载更具成本优势。 如果 handler 大部分时间都在等待网络,并与其他请求共享环境,那么其成本会低于按 wall-clock duration 对每次调用计费的同一 handler。
- CPU 密集型工作负载会失去这一优势。 繁重的渲染、图像处理或加密无论在哪里运行都会消耗可计费 CPU,而每个环境分配了 2 vCPU——其中单线程 Node 进程只能使用一个(参见反模式)。对于持续的大规模 CPU 工作,尤其在可使用 committed-use discount 的情况下,应比较 。
aws-lambda-managed-instances
可通过 endpoint 的 和 控制扩展能力并限制潜在成本。更多细节请参阅 scaling-and-concurrency.md。
scalingConfig.maxEnvironmentsthrottleConfig.rateLimitIAM Setup
IAM 设置
Trust policy principal:
lambda.amazonaws.comIAM actions use the prefix. Resource ARN:
lambda:arn:aws:lambda:{region}:{account}:web-function/{name}Action names match the API operations — , , , , plus the , , and variants. See references/iam-and-security.md for the full policy.
lambda:CreateWebFunctionlambda:CreateWebFunctionRevisionlambda:CreateWebFunctionEndpointlambda:UpdateWebFunctionEndpointGet*List*Delete*Resource ARN must include wildcard for endpoint/revision operations:
/*"Resource": ["arn:aws:lambda:{region}:{account}:web-function/{name}", "arn:aws:lambda:{region}:{account}:web-function/{name}/*"]The caller also needs + on the code bucket (not just the execution role).
s3:GetObjects3:GetObjectVersionSee references/iam-and-security.md for full policy examples.
信任策略 principal:
lambda.amazonaws.comIAM 操作使用 前缀。Resource ARN:
lambda:arn:aws:lambda:{region}:{account}:web-function/{name}操作名称与 API 操作一致——、、、,以及相应的 、 和 变体。完整策略请参阅 references/iam-and-security.md。
lambda:CreateWebFunctionlambda:CreateWebFunctionRevisionlambda:CreateWebFunctionEndpointlambda:UpdateWebFunctionEndpointGet*List*Delete*针对 endpoint/revision 操作,Resource ARN 必须包含 通配符:
/*"Resource": ["arn:aws:lambda:{region}:{account}:web-function/{name}", "arn:aws:lambda:{region}:{account}:web-function/{name}/*"]调用方还需要针对代码 bucket 的 和 权限,而不只是 execution role 需要这些权限。
s3:GetObjects3:GetObjectVersion完整策略示例请参阅 references/iam-and-security.md。
Anti-Patterns
反模式
What it looks like → why it fails → what to do instead.
| Anti-pattern | Why it fails | Instead |
|---|---|---|
| Expecting a CPU-bound handler to use the whole environment | An execution environment is allowed 2 vCPU, but Node is single-threaded, so one process uses one of them and the second sits idle | For CPU-bound work, use |
Zipping a | Execution environments are arm64, so an x86 native binary will not load. Packages such as | Install and build on arm64 — an arm64 CI runner, a Graviton host, or a container run with |
| Writing response bytes while the request body is still uploading (full-duplex streaming) | The request path is half-duplex: the whole request body is forwarded before any response is read, so a large upload that expects an interleaved response stalls until the timeout. Small bodies fit socket buffers and appear to work, which hides it in testing | Read the request to completion, then stream the response. Keep streaming in the server-to-client direction |
Wrapping the app in a handler adapter ( | There is no handler to export. The runtime loads | Delete the adapter and call |
Hardcoding | The runtime probes the exact address it advertises. A mismatched port or a loopback-only bind never answers that probe | Parse |
Module-level mutable state holding per-request data — a | One execution environment serves many concurrent requests in a single process, so requests read each other's values. This is a correctness and data-leak bug, not a performance one | Keep per-request data in the request scope; share only immutable config and SDK clients at module level |
Writing session or upload state to | | Keep state in DynamoDB, S3 or ElastiCache; use unique filenames for genuinely temporary files |
| Treating the endpoint URL as access control — "nobody knows this domain" | An | Implement auth in the app, or use |
Secrets in | | Keep the secret in Secrets Manager or SSM, pass only its name or path, fetch at runtime |
Running | Pruning devDependencies deletes the bundler, so the build then fails with | Build first, prune afterwards — or zip the bundled output → deployment.md |
Expecting an | All of those live on the immutable revision | Create a new revision. Only endpoint-level settings (auth, weights, scaling, throttle) update in place |
| Re-uploading to the same S3 key and expecting a redeploy | A revision is pinned to an object version; CloudFormation reports "No changes to deploy" | Use a new key, or pass the new |
Concluding the feature does not exist because | Absence means an outdated CLI, not a missing capability | Update the AWS CLI → deployment.md |
| Reusing one function name in two Regions | Function names are unique across all Regions in an account; the second create fails with a conflict | Suffix the Region, or use one function with a |
| path-to-regexp v8 rejects a bare | Use |
具体表现 → 失败原因 → 替代方案。
| 反模式 | 失败原因 | 替代方案 |
|---|---|---|
| 期望 CPU 密集型 handler 使用整个环境 | 执行环境允许使用 2 vCPU,但 Node 是单线程的,因此一个进程只会使用其中一个,另一个会闲置 | 对于 CPU 密集型工作,使用 |
打包在 x86 机器(笔记本或 x86 CI)上构建的 | 执行环境为 arm64,因此无法加载 x86 原生二进制文件。诸如 | 在 arm64 上安装和构建——使用 arm64 CI runner、Graviton 主机,或通过 |
| 在请求体仍在上传时写入响应字节(全双工流式传输) | 请求路径是半双工的:完整请求体会先被转发,之后才会读取任何响应,因此大型上传若期望交错响应,会一直停滞直至超时。小型请求体能够装入 socket 缓冲区,看起来可以正常工作,从而在测试中掩盖问题 | 先完整读取请求,再流式发送响应。保持从服务器到客户端方向上的流式传输 |
使用 handler adapter 包装应用( | 没有可导出的 handler。运行时加载 | 删除 adapter,并根据 |
硬编码 | 运行时会对它声明的确切地址进行探测。端口不匹配或仅绑定 loopback 地址都永远无法响应探测 | 解析 |
在模块级可变状态中保存逐请求数据——例如 | 一个执行环境在单个进程中服务许多并发请求,因此请求会读取彼此的值。这不仅是性能问题,更是正确性和数据泄漏问题 | 将逐请求数据保留在请求作用域内;模块级仅共享不可变配置和 SDK client |
将 session 或 upload 状态写入 | | 将状态保存在 DynamoDB、S3 或 ElastiCache 中;真正临时文件使用唯一文件名 |
| 将 endpoint URL 当作访问控制——认为“没人知道这个域名” | | 在应用中实现 auth,或使用 |
在 | | 将 secret 保存在 Secrets Manager 或 SSM 中,只传递其名称或路径,并在运行时获取 |
在 build step 之前运行 | 裁剪 devDependencies 会删除 bundler,导致 build 失败并出现 | 先执行 build,再执行 prune——或者直接打包 bundle 后的输出 → deployment.md |
期望通过 | 所有这些配置都位于不可变的 revision 中 | 创建新的 revision。只有 endpoint 级设置(auth、weights、scaling、throttle)可以原地更新 |
| 重新上传到同一个 S3 key 后期望重新部署 | Revision 固定到特定的 object version;CloudFormation 会报告 | 使用新的 key,或传入新的 |
因为缺少 | 缺少命令意味着 CLI 版本过旧,而不是缺少该能力 | 更新 AWS CLI → deployment.md |
| 在两个 Region 中复用同一个 function 名称 | Function 名称在同一账户的所有 Region 中必须唯一;第二次创建会因冲突而失败 | 添加 Region 后缀,或者使用一个 function 配合 |
在 Express 5 中使用 | path-to-regexp v8 不接受裸 | 使用 |
Troubleshooting — Start Here
故障排查——从这里开始
The five that account for most failed first deploys. Full symptom → cause → fix tables are in
troubleshooting.md.
| Symptom | Cause | Fix | Verify |
|---|---|---|---|
Endpoint never reaches | The app never bound the advertised host and port, so readiness never passed | Bind the host and port from | |
500 with | Entry point is not | Deploy with | |
Stuck in | Execution-role trust policy principal is not | Fix the trust policy, then create a new revision | |
| The caller lacks | Add both to the caller's policy | The revision reaches |
| Outdated AWS CLI; or PascalCase JSON keys | Update the CLI; use camelCase ( | |
以下五项占首次部署失败的大多数。完整的“症状 → 原因 → 修复”对照表位于 troubleshooting.md。
| 症状 | 原因 | 修复 | 验证 |
|---|---|---|---|
Endpoint 始终未达到 | 应用从未绑定所声明的 host 和 port,因此就绪检查始终无法通过 | 根据 | |
返回 500,并显示 | 入口点不是 | 使用 | |
超过 10 分钟后仍停留在 | Execution-role 信任策略的 principal 不是 | 修复信任策略,然后创建新的 revision | |
| 调用方缺少针对代码 bucket 的 | 在调用方策略中添加这两个权限 | Revision 达到 |
| AWS CLI 版本过旧;或者 JSON 键使用了 PascalCase | 更新 CLI;使用 camelCase( | |
Migration
迁移
Moving an existing Node.js HTTP app here is mostly a delete-code exercise — the adapter, the
handler export and the API Gateway wiring all go away, and the router itself does not change.
Four source shapes each have a step-by-step path, a preflight checklist, the features with no
equivalent, and a traffic-cutover procedure: an adapter-wrapped app, API Gateway + Lambda
proxy, a Lambda function URL, and a container on Fargate, App Runner or EC2.
See migration.md.
将现有 Node.js HTTP 应用迁移到这里,主要就是删除代码——adapter、handler export 和 API Gateway wiring 都会消失,而 router 本身无需改动。
对于四种 source shape,每一种都有分步迁移路径、preflight checklist、没有对应能力的功能以及流量切换流程:使用 adapter 包装的应用、API Gateway + Lambda proxy、Lambda function URL,以及运行在 Fargate、App Runner 或 EC2 上的 container。
请参阅 migration.md。
When to Load Reference Files
何时加载参考文件
Match on meaning, not exact wording — any trigger word in a row should pull that file.
| Read this | Trigger words |
|---|---|
| getting-started.md | first deploy, get started, set up, prerequisites, hello world, |
| frameworks.md | Express, Fastify, Hono, TypeScript, |
| deployment.md | deploy, redeploy, ship, release, CLI, |
| scaling-and-concurrency.md | scaling controls, concurrency, |
| infrastructure-as-code.md | CloudFormation, CFN, CDK, |
| static-assets.md | static files, static assets, CSS, JS bundle, images, favicon, |
| domains-and-routing.md | multi-region, multiregion, global, latency routing, Route53, domain, custom domain, hostname, HTTPS, TLS, endpoint type, |
| iam-and-security.md | IAM, policy, permissions, role, trust policy, |
| observability.md | logs, logging, log group, log level, CloudWatch, metrics, monitoring, alarms, tracing, CloudTrail, audit, structured logging, health check, which region served, debug in production |
| architecture-patterns.md | SSR, server-side rendering, React, JSX, streaming, SSE, server-sent events, LLM, AI gateway, token streaming, long-lived connection, webhook, background task, fire and forget, API backend, DynamoDB, connection pooling, thread safety, concurrency model |
| troubleshooting.md | error, fails, failing, broken, not working, 500, 502, 403, 404, |
| networking.md | VPC, private subnet, RDS, Aurora, ElastiCache, private resources, VPC endpoint, PrivateLink, network connector, egress, |
| migration.md | migrate, migration, move, port, switch, convert, coming from, replace, |
请按含义匹配,而不是按确切措辞——一行中的任一 trigger word 都应加载对应文件。
| 读取此文件 | Trigger words |
|---|---|
| getting-started.md | first deploy, get started, set up, prerequisites, hello world, |
| frameworks.md | Express, Fastify, Hono, TypeScript, |
| deployment.md | deploy, redeploy, ship, release, CLI, |
| scaling-and-concurrency.md | scaling controls, concurrency, |
| infrastructure-as-code.md | CloudFormation, CFN, CDK, |
| static-assets.md | static files, static assets, CSS, JS bundle, images, favicon, |
| domains-and-routing.md | multi-region, multiregion, global, latency routing, Route53, domain, custom domain, hostname, HTTPS, TLS, endpoint type, |
| iam-and-security.md | IAM, policy, permissions, role, trust policy, |
| observability.md | logs, logging, log group, log level, CloudWatch, metrics, monitoring, alarms, tracing, CloudTrail, audit, structured logging, health check, which region served, debug in production |
| architecture-patterns.md | SSR, server-side rendering, React, JSX, streaming, SSE, server-sent events, LLM, AI gateway, token streaming, long-lived connection, webhook, background task, fire and forget, API backend, DynamoDB, connection pooling, thread safety, concurrency model |
| troubleshooting.md | error, fails, failing, broken, not working, 500, 502, 403, 404, |
| networking.md | VPC, private subnet, RDS, Aurora, ElastiCache, private resources, VPC endpoint, PrivateLink, network connector, egress, |
| migration.md | migrate, migration, move, port, switch, convert, coming from, replace, |
Guardrail — where this skill's own files live (MCP vs local install)
护栏——此 skill 自身的文件存放在哪里(MCP 与本地安装)
This skill can be loaded two ways, and they resolve the skill's own bundled files — the documents — from different places. Determine how the skill was loaded before you read a reference:
references/- Loaded through the AWS MCP tool call. The skill is not installed on the local filesystem; its reference files do not exist on disk. You MUST fetch each reference through the same
retrieve_skilltool by passing theretrieve_skillparameter (for example,file). Do NOTfile="references/deployment.md"these paths from the local or working directory, and do NOT search the filesystem for them — they are not there, and any local file that happens to match the name is unrelated to this skill.file_read - Installed locally (the skill lives in a local skills directory such as ,
.claude/skills/aws-lambda-web-functions/, or~/.claude/skills/aws-lambda-web-functions/). Read references from the local skill directory using the relative paths shown throughout this document..kiro/skills/aws-lambda-web-functions/
This distinction applies only to the skill's own packaged files. The user's application code — , , , the you build — is always read from and written to the user's working directory regardless of how the skill was loaded. Never fetch or write customer data through .
index.jspackage.jsonpublic/function.zipretrieve_skill此 skill 可以通过两种方式加载,它们会从不同位置解析该 skill 自带的文件——即 文档。读取 reference 前,请先确定 skill 的加载方式:
references/- 通过 AWS MCP 工具调用加载。 Skill 并未安装在本地文件系统上;其 reference 文件在磁盘上不存在。必须通过同一个
retrieve_skill工具获取每个 reference,并传入retrieve_skill参数(例如file)。不要通过file="references/deployment.md"读取这些路径,也不要在本地文件系统中搜索它们——这些文件并不存在,任何恰好同名的本地文件都与此 skill 无关。file_read - 在本地安装(skill 位于本地 skills 目录,例如 、
.claude/skills/aws-lambda-web-functions/或~/.claude/skills/aws-lambda-web-functions/)。使用本文档各处所示的相对路径,从本地 skill 目录读取 reference。.kiro/skills/aws-lambda-web-functions/
此区别仅适用于该 skill 自身打包的文件。用户的应用代码——、、 以及你构建的 ——无论 skill 如何加载,始终从用户的工作目录读取并写入。切勿通过 获取或写入客户数据。
index.jspackage.jsonpublic/function.zipretrieve_skillCritical Rules
关键规则
- Node 24 only — is the only runtime; runtime support can change, so confirm with
nodejs24.xor the AWS docs before assuming a newer version is unavailableaws lambda-web help - Entry point is — the runtime loads
index.js, not/var/task/index.js. When your entry file has another name (.mjs,server.js),app.mjsdetects it or takesaws lambda-web deployand ships a small--entry-pointthat imports it; a ZIP you build yourself needsindex.jsat its rootindex.js - Bind the host and port from — the runtime probes that exact address, so a hardcoded port that does not match it, or a loopback-only bind, fails readiness and the environment is not usable. There is no
AWS_LAMBDA_HTTP_ENDPOINTenv varPORT - in package.json — required for ESM imports from
"type": "module"index.js - No handler exports — your app is an HTTP server
- Write thread-safe code — one execution environment serves many concurrent requests; avoid global mutable state
- Code, runtime, timeout, env vars and concurrency all live on the revision — changing any of them requires a new revision, not an in-place update
- Background tasks work natively — the environment stays running after response
- Streaming native — chunked transfer-encoding, SSE out of the box
- TypeScript works natively — Node 24 type stripping, use shim +
index.jsimport type - Code comes only from a ZIP in S3 — builds and uploads it for you; otherwise supply it through
deployand bundle shared code into the package. For container images and a layers equivalent, confirm againstcodeConfig.s3Objector the API reference rather than assuming either is availableaws lambda-web create-web-function-revision help
- 仅支持 Node 24——是唯一 runtime;runtime 支持情况可能变化,因此不要在假定更新版本不可用之前,先通过
nodejs24.x或 AWS 文档进行确认aws lambda-web help - 入口点是 ——runtime 加载
index.js,而不是/var/task/index.js。当入口文件使用其他名称(.mjs、server.js)时,app.mjs会检测到它或采用aws lambda-web deploy,并随包发送一个导入它的小型--entry-point;自行构建 ZIP 时,根目录中必须有index.jsindex.js - 从 绑定 host 和 port——runtime 会对该确切地址进行探测,因此硬编码不匹配的端口或仅绑定 loopback 地址都会导致就绪检查失败,环境也不可用。不存在
AWS_LAMBDA_HTTP_ENDPOINT环境变量PORT - package.json 中需要 ——
"type": "module"使用 ESM imports 时必需index.js - 不导出 handler——应用是 HTTP server
- 编写 thread-safe code——一个执行环境会服务许多并发请求;避免使用全局可变状态
- Code、runtime、timeout、env vars 和 concurrency 全部位于 revision 中——修改其中任何一项都需要创建新的 revision,而不是原地更新
- 原生支持后台任务——响应完成后环境会继续运行
- 原生支持流式传输——原生支持 chunked transfer-encoding 和 SSE
- 原生支持 TypeScript——使用 Node 24 type stripping,并使用 shim +
index.jsimport type - 代码只能来自 S3 中的 ZIP——会为你构建并上传代码;否则应通过
deploy提供代码,并将共享代码打包进 package。对于 container image 以及 layers 的等效能力,请对照codeConfig.s3Object或 API reference 进行确认,不要假定二者可用aws lambda-web create-web-function-revision help