@aws-blocks/bb-compute

中文文档
0.2.0发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 8242

The Compute Building Block is the single surface for choosing where a workload runs. State the compute type and its options; Blocks maps the type to the AWS service that backs it. The service name never appears in your code.

原始使用文档
文档目录8 节
  1. Placing a whole scope
  2. Compute types
  3. Options
  4. serverless
  5. container
  6. API namespaces
  7. Local development
  8. When to use

@aws-blocks/bb-compute

The Compute Building Block is the single surface for choosing where a workload runs. State the compute type and its options; Blocks maps the type to the AWS service that backs it. The service name never appears in your code.

import { Compute } from '@aws-blocks/blocks';

// Serverless (Lambda today): pay-as-you-go, per-request.
const api = new Compute(scope, 'api', { type: 'serverless', memory: 512 });

// Container (Fargate today): a long-running process.
const worker = new Compute(scope, 'worker', {
  type: 'container',
  size: { vcpu: 1, memory: 2048 },
  scaling: { minInstances: 1, maxInstances: 10 },
});

Hand a Compute to a workload that supports it:

new AsyncJob(scope, 'reports', { compute: worker, timeoutSeconds: 1800, handler });

export const reports = new ApiNamespace(scope, 'reports', (context) => ({ /* methods */ }), { compute: api });

new CronJob(scope, 'nightly', { compute: api, schedule: 'cron(0 2 * * ? *)', handler: async () => { /* … */ } });
WorkloadOptionCompute types
AsyncJobcomputeserverless, container
ApiNamespacecompute (4th argument)serverless
CronJobcomputeserverless
Scope (everything under it)computeserverless, container (see below)

Placing a whole scope

Set compute on a Scope to place everything created under it, including in its child scopes, without passing compute to each block:

const reportsCompute = new Compute(scope, 'reports-compute', { type: 'serverless', memory: 4096 });
const reports = new Scope('reports', { parent: scope, compute: reportsCompute });

export const reportsApi = new ApiNamespace(reports, 'reports', (context) => ({ /* methods */ }));
new AsyncJob(reports, 'rebuild', { handler: async () => { /* … */ } });
new CronJob(reports, 'nightly', { schedule: 'cron(0 2 * * ? *)', handler: async () => { /* … */ } });
  • Give the Compute and the Scope different ids when they share a parent: both are child constructs of it.
  • Nearest wins. A block's own compute option, then the nearest scope that sets compute, then the app's default compute.
  • A container scope places its AsyncJobs on the container. An ApiNamespace or CronJob under it fails synth with an error naming the compute, because they run only on a serverless compute for now: give them their own serverless compute, or create them outside that scope. Setting an AsyncJob's maxConcurrencyPerCPU still needs the container passed as the job's own compute, so the option is typed for it.
  • Realtime and RawRoute stay on the default compute, inside such a scope too. Realtime's publish() works from any compute.

Compute types

TypeModelBacked by
serverlessPay-as-you-go, per-request, short-livedLambda
containerLong-running / long-lived processFargate

Type names are provisional; vm and kubernetes are planned. See docs/design/compute-selection-api.md.

Options

Options are scoped to the type — an attribute that doesn't apply to the chosen type is a compile error.

serverless

  • memory? — function memory (MB); CPU scales with it.
  • maxTimeoutSeconds? — the function's inherent runtime ceiling (≤ 900). A job's own timeoutSeconds must fit under it.

container

  • size? — a valid { vcpu, memory } combination. Each vCPU permits only a fixed set of memory values (an invalid pair won't type-check).
  • scaling? — { minInstances, maxInstances, strategy? }. Wires real Application Auto Scaling. strategy is one or more of cpu/memory/queue-depth signals; omit it and Blocks infers one (queue-depth for a job worker, CPU otherwise).
  • image? — the container image to run (public or ECR URI), used verbatim (pulled by ECS at task start). Blocks builds no image and deploying needs no local Docker: your application code is shipped separately as an S3 asset and fetched at boot. So a custom image only supplies the runtime environment — it must provide a Node 24 runtime on PATH, plus any system libraries your handlers need (e.g. media tooling). Defaults to public.ecr.aws/docker/library/node:24-slim (arm64-compatible). See DESIGN.md for the boot mechanism.

API namespaces

Every ApiNamespace runs on the app's default compute unless you pass one:

const reportsCompute = new Compute(scope, 'reports', { type: 'serverless', memory: 4096 });

export const reports = new ApiNamespace(scope, 'reports', (context) => ({
  async build(month: string) {
    // …
  },
}), { compute: reportsCompute });
  • Clients don't change. reports.build('2026-10') still calls POST /aws-blocks/api/reports — from the TypeScript client and from the Swift, Kotlin and Dart clients alike — and the stack's shared API gateway routes that path to the namespace's compute. It works the same on every apiFrontDoor tier and behind Hosting, so the API keeps one domain. A call to the bare /aws-blocks/api endpoint — a raw HTTP call, or one from a native client released before per-namespace paths — still runs on the default compute.
  • The gateway's limits still apply. An API call is capped at ~29 seconds whatever the compute's maxTimeoutSeconds; run longer work in an AsyncJob.
  • It's placement, not isolation. Every compute runs the same backend bundle under the app's one execution role, so a separate compute is not a security boundary.
  • Routing follows the export name — the name your frontend imports (export const reports = … → reports), not the second ApiNamespace argument.
  • Share a compute across namespaces and AsyncJobs by passing the same Compute to each; they then run in the same function.
  • Only serverless computes serve API namespaces today; passing a container compute is a type error.
  • Everything else stays on the default compute: namespaces with no compute of their own or from their scope, RawRoutes, and the auth HTTP routes (/aws-blocks/auth/*).

Local development

Compute assignment is transparent in local dev — handlers run in-process regardless of the assigned compute. Attributes that only matter to deployed infrastructure (size, scaling) have no effect in the mock.

When to use

Assign a Compute when a workload needs a runtime other than the app default (serverless) — most commonly a long-running or high-memory background job that a container can serve but a serverless function cannot, or an API namespace that needs more memory (and CPU) or its own scaling separate from the rest of the API. Omit compute and the workload uses the app default.

版本与发布记录

@aws-blocks/bb-compute 共发布 2 个版本,首次发布于 2026/11/02;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/11/025 天前
0.0.0-stage2026/11/025 天前

关于 @aws-blocks/bb-compute 的常见问题

@aws-blocks/bb-compute 是什么?

The Compute Building Block is the single surface for choosing where a workload runs. State the compute type and its options; Blocks maps the type to the AWS service that backs it. The service name never appears in your code.

如何安装 @aws-blocks/bb-compute?

在项目目录运行 npm install @aws-blocks/bb-compute,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:constructs(^10.6.0)、aws-cdk-lib(^2.257.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws-blocks/bb-compute 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-blocks/bb-compute 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aws-blocks/bb-compute 可以商用吗?

@aws-blocks/bb-compute 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws-blocks/bb-compute 的下载量有多少?

根据 npm 官方下载统计,@aws-blocks/bb-compute 在 2026/10/07 – 2026/11/05 这 30 天内被下载 8,242 次,日均约 275 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-blocks/bb-compute 还在维护吗?

最新版本 0.2.0 发布于 2026/11/02(5 天前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/11/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。