Terraform 基础设施
Terraform 是一个开源的基础设施即代码软件工具,使您能够安全且可预测地创建、更改和改进基础设施。
Terraform 基础设施生成器创建一个 Terraform 基础设施项目。生成的应用程序通过 Checkov 安全检查包含安全最佳实践。
生成 Terraform 项目
Section titled “生成 Terraform 项目”您可以通过两种方式生成新的 Terraform 项目:
pnpm nx g @aws/nx-plugin:terraform#project --name=tf-infrayarn nx g @aws/nx-plugin:terraform#project --name=tf-infranpx nx g @aws/nx-plugin:terraform#project --name=tf-infrabunx nx g @aws/nx-plugin:terraform#project --name=tf-infra您还可以执行试运行以查看哪些文件会被更改
pnpm nx g @aws/nx-plugin:terraform#project --name=tf-infra --dry-runyarn nx g @aws/nx-plugin:terraform#project --name=tf-infra --dry-runnpx nx g @aws/nx-plugin:terraform#project --name=tf-infra --dry-runbunx nx g @aws/nx-plugin:terraform#project --name=tf-infra --dry-run- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - terraform#project - 填写必需参数
- name: tf-infra
- 点击
Generate
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| name 必需 | string | - | 项目的名称。 |
| type | application | library | application | 项目类型,是 terraform 库(可重用模块)还是应用(可部署)。 |
| directory | string | packages | 新项目的目录。 |
| subDirectory | string | - | 项目所在的子目录。默认为项目名称。 |
| preferInstallDependencies | boolean | true | 是否在生成器运行后优先安装依赖项。设置为 false 可在批量运行多个生成器时延迟安装(如果后续生成器需要计算 Nx 项目图,仍会运行安装);在最后统一安装一次。 |
生成器根据项目类型创建不同的文件结构:
应用程序类型
Section titled “应用程序类型”对于应用程序项目(--type=application),生成器创建一个完整的 Terraform 应用程序,具有远程状态管理:
文件夹src
- main.tf 主 Terraform 配置文件
- providers.tf 带有 S3 后端的提供程序配置
- variables.tf 输入变量定义
- outputs.tf 输出值定义
文件夹env 特定环境的变量文件
- dev.tfvars 开发环境变量
文件夹bootstrap 远程状态的引导配置
- main.tf 用于状态存储的 S3 存储桶和策略
- providers.tf AWS 提供程序配置
- variables.tf 引导变量定义
文件夹scripts 由 nx
bootstrap和init目标运行的 Node 辅助程序- aws-config.ts 通过 AWS SDK 凭证链解析账户 + 区域
- bootstrap.ts 拉取/推送引导 tfstate 并运行
terraform apply - init.ts 使用 S3 后端配置运行
terraform init
- project.json 项目配置和构建目标
对于库项目(--type=library),生成器为可重用的 Terraform 模块创建一个更简单的结构:
文件夹src
- main.tf 主 Terraform 模块文件
- project.json 项目配置和构建目标
实现您的 Terraform 基础设施
Section titled “实现您的 Terraform 基础设施”您可以在 src/main.tf 中开始编写您的 Terraform 基础设施,例如:
locals { account_id = data.aws_caller_identity.current.account_id aws_region = data.aws_region.current.id}
resource "null_resource" "print_info" { # triggers = { # always_run = timestamp() # }
provisioner "local-exec" { command = "echo 'AWS Region: ${local.aws_region}, AWS Account: ${local.account_id}, Environment: ${var.environment}'" }}
# Declare your infrastructure hereresource "aws_s3_bucket" "my_bucket" { bucket = "my-unique-bucket-name"}如果您想从单独的项目(库)执行模块,可以按如下方式操作:
module "lib_module" { source = "../../path/to/my-lib/src"}这将自动更新 Nx 图,在您的使用应用程序和您的库之间添加依赖关系。
在 src/env/*.tfvars 文件中配置特定环境的变量。
要添加新环境,请创建一个新的 src/env/<environment>.tfvars 文件,其中包含特定环境的变量,并在 project.json 中为新环境配置添加 apply, destroy, init, plan 的新条目。例如,假设我们想添加一个 prod 环境:
# Production environment variablesenvironment = "prod"region = "us-west-2"{ "targets": { "apply": { "executor": "nx:run-commands", "defaultConfiguration": "dev", "configurations": { "dev": { "command": "terraform apply ../../../dist/packages/infra/terraform/dev.tfplan" }, "prod": { "command": "terraform apply ../../../dist/packages/infra/terraform/prod.tfplan" } }, "options": { "forwardAllArgs": true, "cwd": "{projectRoot}/src" }, "dependsOn": ["plan"] }, "destroy": { "executor": "nx:run-commands", "defaultConfiguration": "dev", "configurations": { "dev": { "command": "terraform destroy -var-file=env/dev.tfvars" }, "prod": { "command": "terraform destroy -var-file=env/prod.tfvars" } }, "options": { "forwardAllArgs": true, "cwd":"{projectRoot}/src" }, "dependsOn": ["init"] }, "init": { "executor": "nx:run-commands", "defaultConfiguration": "dev", "configurations": { "dev": { "env": { "TF_ENV": "dev" } }, "prod": { "env": { "TF_ENV": "prod" } } }, "options": { "forwardAllArgs": true, "commands": ["tsx {projectRoot}/scripts/init.ts {projectRoot}"], "cwd": "{workspaceRoot}" } }, "plan": { "executor": "nx:run-commands", "defaultConfiguration": "dev", "configurations": { "dev": { "command": "terraform plan -var-file=env/dev.tfvars -out=../../../dist/packages/infra/terraform/dev.tfplan" }, "prod": { "command": "terraform plan -var-file=env/dev.tfvars -out=../../../dist/packages/infra/terraform/prod.tfplan" } }, "options": { "forwardAllArgs": true, "cwd": "{projectRoot}/src" }, "dependsOn": ["init"] } }}远程状态引导(仅限应用程序项目)
Section titled “远程状态引导(仅限应用程序项目)”在部署基础设施之前,您需要引导远程状态后端。这将创建一个 S3 存储桶来存储您的 Terraform 状态文件:
pnpm nx bootstrap tf-infrayarn nx bootstrap tf-infranpx nx bootstrap tf-infrabunx nx bootstrap tf-infra可用目标取决于您的项目类型:
通用目标(应用程序和库)
Section titled “通用目标(应用程序和库)”验证您的基础设施
Section titled “验证您的基础设施”您可以使用 validate 目标验证您的 Terraform 配置:
pnpm nx validate tf-infrayarn nx validate tf-infranpx nx validate tf-infrabunx nx validate tf-infra格式化您的代码
Section titled “格式化您的代码”使用 fmt 目标格式化您的 Terraform 代码:
pnpm nx fmt tf-infrayarn nx fmt tf-infranpx nx fmt tf-infrabunx nx fmt tf-infra使用 Checkov 通过 test 目标对您的基础设施运行安全检查:
pnpm nx test tf-infrayarn nx test tf-infranpx nx test tf-infrabunx nx test tf-infra您将在根 dist 文件夹下的 dist/packages/<my-terraform-project>/checkov 中找到您的安全测试结果。
仅限应用程序的目标
Section titled “仅限应用程序的目标”以下目标仅适用于应用程序类型项目:
规划您的基础设施
Section titled “规划您的基础设施”在应用更改之前,您可以通过运行 plan 目标来查看 Terraform 将执行的操作:
pnpm nx plan tf-infrayarn nx plan tf-infranpx nx plan tf-infrabunx nx plan tf-infra这将在 dist/packages/<my-terraform-project>/terraform/dev.tfplan 中创建一个计划文件。
初始化 Terraform
Section titled “初始化 Terraform”使用 init 目标初始化您的 Terraform 工作目录:
pnpm nx run tf-infra:inityarn nx run tf-infra:initnpx nx run tf-infra:initbunx nx run tf-infra:init部署到 AWS
Section titled “部署到 AWS”规划后,您可以使用 apply 目标将基础设施部署到 AWS:
pnpm nx apply tf-infrayarn nx apply tf-infranpx nx apply tf-infrabunx nx apply tf-infra从您的 Terraform 配置中检索输出值:
pnpm nx output tf-infrayarn nx output tf-infranpx nx output tf-infrabunx nx output tf-infra销毁基础设施
Section titled “销毁基础设施”当您需要拆除基础设施时,使用 destroy 目标:
pnpm nx destroy tf-infrayarn nx destroy tf-infranpx nx destroy tf-infrabunx nx destroy tf-infra销毁引导资源
Section titled “销毁引导资源”要清理引导资源(用于状态存储的 S3 存储桶):
pnpm nx bootstrap-destroy tf-infrayarn nx bootstrap-destroy tf-infranpx nx bootstrap-destroy tf-infrabunx nx bootstrap-destroy tf-infra有关 Terraform 的更多信息,请参阅 Terraform 文档和 AWS 提供程序文档。