@awboost/cfn-template-builder

中文文档
0.4.3发布于 2024/04/27MITTypeScriptES Module近 30 天下载 3194

Zero-dependency package for building AWS CloudFormation Templates

原始使用文档
文档目录4 节
  1. Raw Template Types
  2. Building a Stack
  3. Using Assets
  4. Intrinsic Functions and Pseudo Parameters

@awboost/cfn-template-builder

Zero-dependency package for building AWS CloudFormation Templates.

Raw Template Types

TypeScript types are available for the plain AWS CloudFormation Template by importing from @awboost/cfn-template-builder/template. For performance reasons (there are thousands of resource types), the resource properties are not strongly typed. Strong types for resources are available from @awboost/cfn-resource-types.

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:...:...",
      },
    },
  },
};

Building a Stack

A template can be constructed by combining one or more template items using the Stack class:

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" });

Using Assets

Additional files can be used by the stack by using the Asset class. These will automatically be output alongside the template when the stack is emitted.

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:...:...",
  }),
);

Intrinsic Functions and Pseudo Parameters

Intrinsic Functions and Pseudo Parameters are available via Fn and AwsParam classes respectively.

Additionally the Fn class has a join$ method which can construct a value via a tagged template string. This uses the Fn::Join function under the hood to construct a string from the input.

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`,
      },
    },
  },
};

Here the Fn.join$ call is equivalent to:

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

This ultimately outputs the following JSON in the template:

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

版本与发布记录

@awboost/cfn-template-builder 共发布 13 个版本,首次发布于 2023/12/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.3latest2024/04/272 年前
0.4.22024/03/312 年前
0.4.12024/03/312 年前
0.4.02024/03/292 年前
0.3.52024/02/142 年前
0.3.42024/02/132 年前

关于 @awboost/cfn-template-builder 的常见问题

@awboost/cfn-template-builder 是什么?

Zero-dependency package for building AWS CloudFormation Templates

如何安装 @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 和提交记录判断。