# @awboost/cfn-template-builder 中文文档

规范地址：https://npmcn.com/package/%40awboost/cfn-template-builder/

版本：0\.4\.3
npm 元数据更新时间：2024-04-26T17:18:18.277Z
中文译文更新时间：2026-11-07T08:34:55.721Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40awboost/cfn-template-builder
原文对照：https://npmcn.com/package/%40awboost/cfn-template-builder/?lang=original
项目主页：https://github.com/awboost/cfn-template-builder#readme
源码仓库：https://github.com/awboost/cfn-template-builder

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：370 kB，127 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=18\.0\.0
- 运行依赖：1 个
- 首次发布：2023-12-24T15:28:39.020Z
- 最新版本发布：2024-04-26T17:18:18.048Z
- 近 30 天下载量：3194（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：13（近 12 个月 0 个）

## 中文简介

用于构建 AWS 云阵模板的零依赖软件包

## 安装

```sh
npm install @awboost/cfn-template-builder
```

也可以使用：`pnpm add @awboost/cfn-template-builder`、`yarn add @awboost/cfn-template-builder`、`bun add @awboost/cfn-template-builder`

## README

# @awboost/cfn-template-buildinger (英语)

建筑零依赖性套装 [AWS 云格式模板](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html)。 。 。 .

## 原始模板类型

为平原提供类型脚本 [AWS 云格式模板](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html) 从 `@awboost/cfn-template-builder/template`. 由于业绩原因(有上千种资源类型),资源属性没有强烈打入. 资源有很强的种类: [`@awboost/cfn-resource-types`](https://www.npmjs.com/package/@awboost/cfn-resource-types)。 。 。 .

```typescript
import { Template } from "@awboost/cfn-template-builder/template";

const template: Template = {
  Resources: {
    MyFunction: {
      // resource types are not strongly typed here
      Type: "AWS::Lambda::Function",
      Properties: {
        Code: {
          S3Bucket: "my-bucket",
          S3Key: "code",
        },
        Role: "arn:etc:...:...",
      },
    },
  },
};
```

## 构建堆栈

一个模板可以通过合并一个或多个模板项目来构建 `Stack` 类 :

```typescript
import { Stack } from "@awboost/cfn-template-builder/stack";
import { Parameter } from "@awboost/cfn-template-builder/template/Parameter";
import { LambdaFunction } from "@awboost/cfn-resource-types/AWS-Lambda-Function";

const stack = new Stack();

const bucketParam = stack.use(new Parameter("CodeBucket", "String"));
const objectParam = stack.use(new Parameter("CodeKey", "String"));

const lambda = stack.use(
  new LambdaFunction("MyFunction", {
    // properties have accurate types now
    Code: {
      S3Bucket: bucketParam.ref,
      S3Key: objectParam.ref,
    },
    Role: "arn:etc:...:...",
  }),
);

// now write the JSON template to the given path
const emitter = new FileSystemAssetEmitter({
  outputDirectory: "/path/to/output",
});

await stack.build(emitter, { templateFileName: "template.json" });
```

## 使用资产

附加文件可用于堆栈 `Asset` 课。 当堆栈被释放时, 这些会自动与模板一起输出 .

```typescript
import { Stack } from "@awboost/cfn-template-builder/stack";
import { Asset } from "@awboost/cfn-template-builder/template/Asset";
import { LambdaFunction } from "@awboost/cfn-resource-types/AWS-Lambda-Function";

const stack = new Stack();

const codeAsset = Asset.fromFile("LambdaCode", "my-code.zip");

const lambda = stack.use(
  new LambdaFunction("MyFunction", {
    // properties have accurate types now
    Code: codeAsset.ref,
    Role: "arn:etc:...:...",
  }),
);
```

## 内在函数和 Pseudo 参数

[内向函数](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html) 财务报告和审定财务报表 [修多参数](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html) 可通过 `Fn` 财务报告和审定财务报表 `AwsParam` 课程.

此外 `Fn` 类有一个 `join$` 方法,可以通过 [标记模板](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) 绳子。 这个用的是 [`Fn::Join` 函数](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-join.html) 在引擎盖下从输入中构造一个字符串.

```typescript
import { Fn } from "@awboost/cfn-template-builder/intrinsics";
import { AwsParam } from "@awboost/cfn-template-builder/pseudo";
import { Template } from "@awboost/cfn-template-builder/template";

const template: Template = {
  Parameters: {
    CodeBucket: { Type: "String" },
    CodeObject: { Type: "String" },
  },
  Resources: {
    MyFunction: {
      Type: "AWS::Lambda::Function",
      Properties: {
        Code: {
          S3Bucket: Fn.ref("CodeBucket"),
          S3Key: Fn.ref("CodeObject"),
        },
        Role: "arn:etc:...:...",
        FunctionName: Fn.join$`${AwsParam.StackName}-MyFunction`,
      },
    },
  },
};
```

给 `Fn.join$` 电话相当于:

```typescript
Fn.join("", [AwsParam.StackName, "-MyFunction"]);
```

这最终会在模板中输出以下JSON:

```json
{
  "Fn::Join": ["", [{ "Ref": "AWS::StackName" }, "-MyFunction"]]
}
```

## 常见问题

### @awboost/cfn-template-builder 是什么？

用于构建 AWS 云阵模板的零依赖软件包

### 如何安装 @awboost/cfn-template-builder？

在项目目录运行 npm install @awboost/cfn-template-builder，或使用 pnpm add / yarn add / bun add。

### @awboost/cfn-template-builder 支持 TypeScript 吗？

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

### @awboost/cfn-template-builder 支持 ESM 还是 CommonJS？

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

### @awboost/cfn-template-builder 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @awboost/cfn-template-builder 可以商用吗？

@awboost/cfn-template-builder 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @awboost/cfn-template-builder 的下载量有多少？

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

### @awboost/cfn-template-builder 还在维护吗？

最新版本 0\.4\.3 发布于 2024/04/27（2 年前）；至今共发布 13 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/12/24。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

