@awboost/cfn-template-buildinger (英语)
建筑零依赖性套装 AWS 云格式模板。 。 。 .
原始模板类型
为平原提供类型脚本 AWS 云格式模板 从 @awboost/cfn-template-builder/template. 由于业绩原因(有上千种资源类型),资源属性没有强烈打入. 资源有很强的种类: @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:...:...",
},
},
},
};
构建堆栈
一个模板可以通过合并一个或多个模板项目来构建 Stack 类 :
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 课。 当堆栈被释放时, 这些会自动与模板一起输出 .
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 参数
内向函数 财务报告和审定财务报表 修多参数 可通过 Fn 财务报告和审定财务报表 AwsParam 课程.
此外 Fn 类有一个 join$ 方法,可以通过 标记模板 绳子。 这个用的是 Fn::Join 函数 在引擎盖下从输入中构造一个字符串.
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$ 电话相当于:
Fn.join("", [AwsParam.StackName, "-MyFunction"]);
这最终会在模板中输出以下JSON:
{
"Fn::Join": ["", [{ "Ref": "AWS::StackName" }, "-MyFunction"]]
}