@anvil-cloud/sdk

中文文档
0.1.26发布于 2026/11/07Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 2095

Anvil — secure-by-default cloud infrastructure components

原始使用文档
文档目录9 节
  1. Install
  2. Secure by default
  3. The App class
  4. Forward references ( ctx.ref )
  5. Grants
  6. SvelteKit deployment
  7. Escape hatch
  8. Links
  9. License

@anvil-cloud/sdk

Cloud infrastructure that's secure by default — not by accident.

Anvil wraps raw cloud resources into opinionated, production-ready components. No 200-line Terraform modules. No copy-pasting security configs. Just declare what you need and Anvil handles the rest.

Built on Pulumi.

Install

npm install @anvil-cloud/sdk

Secure by default

Every Anvil component ships with defaults aligned to production from day one — public access blocked, encryption enforced, cost tags applied. The goal isn't to make compliance automatic, but to make it a platform you can actually build on.

The App class

Every Anvil program starts with new App(). The run callback receives a Context with:

  • ctx.stage — current deployment stage (defaults to your OS username for dev isolation)
  • ctx.project — project name from anvil.yaml
  • ctx.export(name, value) — export stack outputs
  • ctx.providers — named cloud providers for multi-region / multi-account
  • ctx.ref(id) — reference a resource declared elsewhere (see below)

Forward references (ctx.ref)

Normally you reference a resource through the variable it's assigned to, which means it must be declared first. ctx.ref('id') lets you reference a resource by its logical name — even one declared later in the program:

import { App } from '@anvil-cloud/sdk';
import * as anvil from '@anvil-cloud/sdk';

export default new App({
  run(ctx) {
    const fn = new anvil.aws.Lambda('processor', {
      runtime: 'nodejs20.x',
      handler: 'src/index.handler',
      environment: {
        // forward reference — `db` is declared below
        DSQL_ENDPOINT: ctx.ref<anvil.aws.DSQL>('db').endpoint,
      },
    });

    const db = new anvil.aws.DSQL('db', { /* ... */ });
  },
});

ctx.ref('db').endpoint resolves to the same output as db.endpoint — they're interchangeable. The generic (<anvil.aws.DSQL>) is just for autocomplete.

This is an escape hatch for cases where declaration order is awkward; most code references resources directly. Two things to know:

  • Keep references flowing one direction. Because you can point either way, you can accidentally create a cycle (A references B's output and B references A's). That deadlocks the deploy rather than erroring cleanly — so don't do it.
  • A typo or never-declared id fails with a clear error before anything deploys.

Grants

Grants are how Anvil wires permissions between resources. Instead of writing IAM policies by hand, you call .grant() on a resource and Anvil handles both the IAM role policy and the environment variable injection automatically.

A Lambda reading from a Bucket:

import { App } from '@anvil-cloud/sdk';
import * as anvil from '@anvil-cloud/sdk';

export default new App({
  run(ctx) {
    const bucket = new anvil.aws.Bucket('uploads', {
      dataClassification: 'sensitive',
    });

    const fn = new anvil.aws.Lambda('processor', {
      runtime: 'nodejs20.x',
      handler: 'src/index.handler',
    });

    // Grants the Lambda read access to the bucket and scopes down to specific bucket paths
    // Anvil creates the IAM policy and injects UPLOADS_BUCKET_NAME
    // into the Lambda's environment automatically.
    //
    bucket.grant(fn, { actions: ['read'], path: ['user/*'] });
  },
});

What Anvil does under the hood:

  • Creates an IAM RolePolicy scoped to the exact actions requested
  • Injects the resource identifier as an environment variable on the target (e.g. UPLOADS_BUCKET_NAME)
  • No manual ARN wiring, no forgotten permissions

SvelteKit deployment

Deploy a SvelteKit app to AWS with a single component. Anvil provisions S3, CloudFront, ACM, Lambda (via Lambda Web Adapter), and Route53 — with HTTPS and a custom domain out of the box:

import { App } from '@anvil-cloud/sdk';
import * as anvil from '@anvil-cloud/sdk';

export default new App({
  run(ctx) {
    const site = new anvil.aws.SvelteKitSite('web', {
      path: 'web', // the SvelteKit project folder — Anvil runs the build
      domain: 'myapp.com',
    });

    ctx.export('url', site.url);
  },
});

Escape hatch

Every component accepts a hatch argument — an escape hatch to override the underlying resource config when you need to break from the defaults:

const bucket = new anvil.aws.Bucket('custom', {
  dataClassification: 'non-sensitive',
  hatch: {
    bucket: { forceDestroy: true, tags: { env: 'dev' } },
  },
});

License

Apache-2.0

版本与发布记录

@anvil-cloud/sdk 共发布 33 个版本,首次发布于 2026/03/21;过去 12 个月发布了 33 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.26latest2026/11/07今天
0.1.252026/10/2711 天前
0.1.242026/10/2513 天前
0.1.232026/10/2414 天前
0.1.222026/10/2414 天前
0.1.212026/10/1721 天前

关于 @anvil-cloud/sdk 的常见问题

@anvil-cloud/sdk 是什么?

Anvil — secure-by-default cloud infrastructure components

如何安装 @anvil-cloud/sdk?

在项目目录运行 npm install @anvil-cloud/sdk,或使用 pnpm add / yarn add / bun add。

@anvil-cloud/sdk 支持 TypeScript 吗?

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

@anvil-cloud/sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@anvil-cloud/sdk 可以商用吗?

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

@anvil-cloud/sdk 的下载量有多少?

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

@anvil-cloud/sdk 还在维护吗?

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