Terraform 基础资源创建

LinuxBeginner
立即练习

介绍

Terraform 是由 HashiCorp 创建的开源基础设施即代码(Infrastructure as Code, IaC)工具。它允许你使用声明性配置文件来定义和部署数据中心基础设施。这意味着你需要在配置文件中描述你期望的基础设施,然后 Terraform 会计算出如何创建、更新或删除资源以匹配该状态。

核心 Terraform 工作流程包含三个主要阶段:

  1. 编写 (Write):编写基础设施即代码。
  2. 规划 (Plan):在应用更改之前预览这些更改。
  3. 应用 (Apply):部署和管理你的基础设施。

在这个 Lab 中,你将通过创建一个非常简单的资源——一个本地文件——来走查整个工作流程。使用 local provider 是学习 Terraform 机制的绝佳方式,因为它不需要云 provider 的凭证。你将定义一个文件,规划其创建,应用更改,并验证结果。

这是一个实验(Guided Lab),提供逐步指导来帮助你学习和实践。请仔细按照说明完成每个步骤,获得实际操作经验。根据历史数据,这是一个 初级 级别的实验,完成率为 98%。获得了学习者 97% 的好评率。

main.tf 中定义 local_file 资源

在这一步,你将开始在你的 Terraform 配置文件中定义一个资源。所有 Terraform 配置文件都以 .tf 扩展名结尾的文件中编写。主配置文件通常命名为 main.tf

resource 块是声明基础设施对象的首要语法。它定义了一个给定类型(例如 local_file)和给定本地名称(例如 example)的资源。类型和名称的组合在一个模块内必须是唯一的。

首先,使用 nano 文本编辑器打开你 ~/project 目录下的 main.tf 文件。

nano main.tf

现在,将以下代码添加到文件中。这定义了一个类型为 local_file 的资源,并为其指定本地名称 example。目前,该资源块是空的。

resource "local_file" "example" {
}

添加代码后,按 Ctrl+X,然后按 Y,最后按 Enter 键保存文件并退出 nano

在资源块中指定文件名和内容

在这一步,你将向资源块中添加参数(arguments),以指定要创建的文件的属性。参数是资源块内部的键值对,用于定义其配置。对于 local_file 资源,最重要的两个参数是 filenamecontent

  • filename: 将要创建的文件的路径。
  • content: 将写入文件中的内容。

再次使用 nano 打开 main.tf 文件。

nano main.tf

修改 local_file 资源块,包含 filenamecontent 参数,如下所示。

resource "local_file" "example" {
  content  = "Hello, Terraform!"
  filename = "${path.module}/hello.txt"
}

在这里,我们告诉 Terraform 在当前项目目录中创建一个名为 hello.txt 的文件。${path.module} 是一个特殊的 Terraform 表达式,它解析为当前模块的路径,在本例中即为 ~/project。文件的内容将是字符串 "Hello, Terraform!"。

Ctrl+X,然后按 Y,最后按 Enter 键保存文件并退出 nano

初始化 Terraform 并预览更改

在这一步,你将首先初始化你的 Terraform 项目,然后运行 terraform plan 命令来创建一个执行计划。

在你可以计划任何更改之前,你必须初始化项目。terraform init 命令会扫描你的配置,下载所需的提供者(在本例中是 local_file 的提供者),并设置后端(backend)。

在你的终端中,首先运行 terraform init

terraform init

初始化成功后,运行 terraform plan。这个命令是 Terraform 工作流程中的一个关键部分,因为它提供了一个更改的“试运行”(dry run)。它会向你展示 Terraform 将对你的基础设施执行的操作,而不会实际进行任何更改。这使得你可以在应用操作之前进行审查和验证。

terraform plan

Terraform 将读取你的配置文件,并将其与你基础设施的当前状态(目前为空)进行比较。然后它将显示一个操作计划。你应该会看到类似以下内容的输出,表明将要创建一个资源。

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  ## local_file.example will be created
  + resource "local_file" "example" {
      + content              = "Hello, Terraform!"
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "./hello.txt"
      + id                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

resource "local_file" "example" 旁边的 + 符号表示将创建此资源。输出详细说明了将在新文件上设置的所有属性。

运行 terraform apply 以创建资源

在这一步,你将使用 terraform apply 命令来实际创建文件,从而应用这些更改。此命令会执行 terraform plan 输出中提议的操作。

默认情况下,terraform apply 会再次向你展示计划,并在继续之前要求交互式确认。为了使其非交互式(这对于实验和自动化很有用),你可以使用 -auto-approve 标志。

在你的终端中运行以下命令:

terraform apply -auto-approve

Terraform 现在将执行该计划。输出将首先再次显示计划,然后是应用过程的进度。完成后,你将看到一条确认消息。

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  ## local_file.example will be created
  + resource "local_file" "example" {
      + content              = "Hello, Terraform!"
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "./hello.txt"
      + id                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.
local_file.example: Creating...
local_file.example: Creation complete after 0s [id=42086c02e03bf671ddf621ed9922f52f2c7a605c]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

"Apply complete!" 消息确认 Terraform 已根据你的配置成功创建了资源。

验证文件已在文件系统中创建

在最后一步,你将验证 Terraform 是否已按照你的指定成功在本地文件系统上创建了文件。由于你使用了 local_file 资源,结果是你的项目目录中出现了一个具体的文件。

首先,使用 ls 命令列出当前目录中的文件。你应该会看到 hello.txt 与你的 Terraform 配置文件并列。

ls

预期输出:

hello.txt  main.tf  terraform.tfstate

接下来,使用 cat 命令显示新创建的 hello.txt 文件内容。

cat hello.txt

你应该会看到你在 main.tf 中定义的内容。

Hello, Terraform!

这确认了你的 Terraform 配置已成功应用,并且资源已按照你在代码中定义的方式创建。

总结

恭喜!你已成功完成了你的第一个 Terraform 项目。

在这个实验中,你学习了 Terraform 的基本工作流程:

  1. 编写 (Write):你在 main.tf 配置文件中定义了一个 local_file 资源。
  2. 计划 (Plan):你使用了 terraform plan 来预览文件的创建过程,而没有进行任何更改。
  3. 应用 (Apply):你使用了 terraform apply 来执行计划,并在本地文件系统上创建了该文件。

创建本地文件的这个简单示例展示了基础设施即代码 (Infrastructure as Code) 的核心能力。你用配置文件声明了资源所需的期望状态,而 Terraform 负责处理实现该状态的逻辑。同样的原则也适用于管理更复杂的云基础设施,例如虚拟机、网络和数据库。