CDK基础设施
AWS CDK 是一个通过代码定义云基础设施并通过 AWS CloudFormation 进行部署的框架。
TypeScript 基础设施生成器会创建一个用 TypeScript 编写的 AWS CDK 基础设施应用。生成的应用程序通过 Checkov 安全检查集成了安全最佳实践。
生成基础设施项目
Section titled “生成基础设施项目”您可以通过两种方式生成新的基础设施项目:
- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)
在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#infra
- 填写必需参数
- 点击
Generate
pnpm nx g @aws/nx-plugin:ts#infra
yarn nx g @aws/nx-plugin:ts#infra
npx nx g @aws/nx-plugin:ts#infra
bunx nx g @aws/nx-plugin:ts#infra
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
name 必需 | string | - | The name of the application. |
directory | string | packages | The directory of the new application. |
生成器将在 <directory>/<name>
目录下创建以下项目结构:
文件夹src
- main.ts 实例化 CDK 部署阶段的应用程序入口点
文件夹stages CDK 阶段定义
- application-stage.ts 定义阶段中要部署的堆栈集合
文件夹stacks CDK 堆栈定义
- application-stack.ts 主应用堆栈
- cdk.json CDK 配置文件
- project.json 项目配置和构建目标
- checkov.yml Checkov 配置文件
实现 CDK 基础设施
Section titled “实现 CDK 基础设施”您可以在 src/stacks/application-stack.ts
中开始编写 CDK 基础设施代码,例如:
import { Stack, StackProps } from 'aws-cdk-lib';import { Bucket } from 'aws-cdk-lib/aws-s3'import { Construct } from 'constructs';
export class ApplicationStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props);
// 在此声明您的基础设施 new Bucket(this, 'MyBucket'); }}
您会注意到顶层的 src/main.ts
文件实例化了一个名为 <namespace>-sandbox
的 CDK 阶段。该阶段用于您自己的开发和测试。
new ApplicationStage(app, 'project-sandbox', { env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION, },});
您可以添加更多阶段,例如定义 beta
和 prod
阶段以部署到不同环境:
new ApplicationStage(app, 'project-beta', { env: { account: '123456789012', // beta 账户 region: 'us-west-2', },});new ApplicationStage(app, 'project-prod', { env: { account: '098765432109', // 生产账户 region: 'us-west-2', },});
阶段定义了应一起部署的堆栈集合。您可以在一个阶段内实例化任意数量的堆栈,例如:
import { Stage, StageProps } from 'aws-cdk-lib';import { Construct } from 'constructs';import { BackendStack } from '../stacks/backend-stack.js';import { FrontendStack } from '../stacks/frontend-stack.js';
/** * 定义构成应用程序的 CDK 堆栈集合 */export class ApplicationStage extends Stage { constructor(scope: Construct, id: string, props?: StageProps) { super(scope, id, props);
new BackendStack(this, 'Backend', { crossRegionReferences: true, })
new FrontendStack(this, 'Frontend', { crossRegionReferences: true, }); }}
API 基础设施
Section titled “API 基础设施”如果您使用过 tRPC API 或 FastAPI 生成器创建 API,您会注意到 packages/common/constructs
中已有一些可用于部署它们的构造。
例如,如果您创建了一个名为 my-api
的 tRPC API,只需导入并实例化该构造即可添加部署所需的所有基础设施:
import * as cdk from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyApi } from ':my-scope/common-constructs';
export class ApplicationStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);
// 添加 API 的基础设施 new MyApi(this, 'MyApi', { integrations: MyApi.defaultIntegrations(this).build(), }); }}
网站基础设施
Section titled “网站基础设施”如果您使用过 CloudScape 网站 生成器,您会注意到 packages/common/constructs
中已有部署所需的构造。例如:
import * as cdk from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyWebsite } from ':my-scope/common-constructs';
export class ApplicationStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);
// 添加网站的基础设施 new MyWebsite(this, 'MyWebsite'); }}
为确保网站 运行时配置 包含所有 API 配置,必须在所有 API 构造之后声明网站。
合成基础设施
Section titled “合成基础设施”作为 build
目标的一部分,除了运行 默认的编译、检查和测试目标 外,您的基础设施项目会被 合成 为 CloudFormation。也可以通过单独执行 synth
目标来运行:
pnpm nx run <my-infra>:synth
yarn nx run <my-infra>:synth
npx nx run <my-infra>:synth
bunx nx run <my-infra>:synth
您可以在根目录的 dist
文件夹下找到合成的云装配,路径为 dist/packages/<my-infra-project>/cdk.out
。
项目中添加了 checkov
目标,使用 Checkov 对基础设施进行安全检查。
pnpm nx run <my-infra>:checkov
yarn nx run <my-infra>:checkov
npx nx run <my-infra>:checkov
bunx nx run <my-infra>:checkov
安全测试结果位于根目录的 dist
文件夹下,路径为 dist/packages/<my-infra-project>/checkov
。
抑制 Checkov 检查
Section titled “抑制 Checkov 检查”在某些情况下,您可能需要抑制特定资源上的某些规则。可通过两种方式实现:
在指定构造上抑制规则
Section titled “在指定构造上抑制规则”import { suppressRules } from ':my-scope/common-constructs';
// 抑制指定构造的 CKV_AWS_XXX 规则suppressRules(construct, ['CKV_AWS_XXX'], '原因说明');
在派生构造上抑制规则
Section titled “在派生构造上抑制规则”import { suppressRules } from ':my-scope/common-constructs';
// 如果构造或其派生构造是 Bucket 实例,则抑制 CKV_AWS_XXX 规则suppressRules(construct, ['CKV_AWS_XXX'], '原因说明', (construct) => construct instanceof Bucket);
引导 AWS 账户
Section titled “引导 AWS 账户”如果是首次向 AWS 账户部署 CDK 应用,需要先进行引导。
首先,确保已 为 AWS 账户配置凭证。
然后,使用 cdk bootstrap
命令:
npx cdk bootstrap aws://<account-id>/<region>
更多详情请参考 CDK 文档。
部署到 AWS
Section titled “部署到 AWS”构建完成后,可通过 deploy
目标将基础设施部署到 AWS。
首先,确保已 为 AWS 账户配置凭证。
然后运行部署目标:
pnpm nx run <my-infra>:deploy <my-infra>-sandbox/*
yarn nx run <my-infra>:deploy <my-infra>-sandbox/*
npx nx run <my-infra>:deploy <my-infra>-sandbox/*
bunx nx run <my-infra>:deploy <my-infra>-sandbox/*
在 CI/CD 流水线中部署到 AWS
Section titled “在 CI/CD 流水线中部署到 AWS”在 CI/CD 流水线中部署时,请使用 deploy-ci
目标。
pnpm nx run <my-infra>:deploy-ci my-stage/*
yarn nx run <my-infra>:deploy-ci my-stage/*
npx nx run <my-infra>:deploy-ci my-stage/*
bunx nx run <my-infra>:deploy-ci my-stage/*
该目标与常规 deploy
目标的区别在于,它确保部署预合成的云装配,而不是动态合成。这有助于避免因包版本变化导致的非确定性问题,确保每个流水线阶段使用相同的云装配进行部署。
销毁 AWS 基础设施
Section titled “销毁 AWS 基础设施”使用 destroy
目标来销毁资源:
pnpm nx run <my-infra>:destroy <my-infra>-sandbox/*
yarn nx run <my-infra>:destroy <my-infra>-sandbox/*
npx nx run <my-infra>:destroy <my-infra>-sandbox/*
bunx nx run <my-infra>:destroy <my-infra>-sandbox/*