注意:
- 此教程需要访问 Oracle Cloud。要注册免费账户,请参阅开始使用 Oracle Cloud Infrastructure Free Tier 。
- 它使用 Oracle Cloud Infrastructure 身份证明、租户和区间示例值。完成实验室时,请将这些值替换为特定于您的云环境的值。
将 Oracle Cloud Infrastructure 资源导入到 Terraform 状态文件中
简介
使用 Terraform 管理 Oracle Cloud Infrastructure (OCI) 资源具有许多优势。其中之一是它为基础设施部署带来了一致性和可重现性。另一种是,Terraform 配置可以模块化并在项目中重复使用,从而轻松管理和扩展基础设施配置。同样,通过 Terraform,基础设施预配和管理可以实现自动化,从而实现一致且可重复的部署。另一个重要方面是 Terraform 维护一个状态文件来跟踪基础结构的当前状态。此状态文件用于计划和应用更改,以便 Terraform 确定需要进行哪些更改才能达到所需状态。
但是,在某些情况下,Oracle Cloud Infrastructure (OCI) 中的并非所有资源都由 Terraform 管理。这通常发生在您使用 Terraform 预配资源时,这些资源随后会显示在状态文件中,而其他资源则由其他人手动创建。稍后,您可能会发现您需要这些手动创建的资源才能由 Terraform 管理。
考虑以下事项:
-
创建具有 Terraform 的 VCN,以便您具有管理此 VCN 的状态文件和 Terraform 配置文件。
-
在 OCI 租户中手动添加新资源(计算)。
如果您希望使用您已有的 Terraform 配置和状态文件管理新添加的计算,可以使用 Terraform 导入命令,但还需要执行更多步骤,您必须更新 Terraform 配置文件。
Terraform v1.5.0
及更高版本支持导入块。使用此方法,您的现有 Terraform 代码保持不变。您可能需要选择将其集成到现有的 Terraform 配置文件中,但这不是必需的。目标是将 OCI 资源导入不由 Terraform 管理的 Terraform 状态文件。
一个重要方面是 import block 可用于 CI/CD 管道。此外,它还允许您在修改状态之前预览导入操作。最终,这一切都取决于您和用例的复杂性。
目标
- 手动创建资源(OCI 计算虚拟机),然后导入现有 Terraform 状态文件以供 Terraform 进一步管理。
先决条件
-
Terraform
v1.5.0
及更高版本。 -
由 Terraform 创建和管理的基础设施环境。在本教程中,我们有一个 VCN,其中包含一些子网、安全列表、路由表等。
任务 1:检查使用 Terraform 创建的现有基础结构
-
运行
terraform state list
命令以检查由 Terraform 配置文件管理的 OCI 资源。terraform state list module.network.data.oci_core_services.all_oci_services module.network.oci_core_internet_gateway.igw["igw"] module.network.oci_core_nat_gateway.ngw["ngw"] module.network.oci_core_route_table.route_table["rt_priv"] module.network.oci_core_route_table.route_table["rt_pub"] module.network.oci_core_security_list.sl["sl_priv"] module.network.oci_core_security_list.sl["sl_public"] module.network.oci_core_service_gateway.this["sgw"] module.network.oci_core_subnet.subnets["sn1_priv"] module.network.oci_core_subnet.subnets["sn1_pub"] module.network.oci_core_subnet.subnets["sn2_priv"] module.network.oci_core_subnet.subnets["sn3_priv"] module.network.oci_core_virtual_network.vcn["vcn1"]
如您所见,我们提供了 VCN、子网、安全列表和路由表。
-
以下是我的 Terraform 配置文件。
~/workORCL/_MY_OCI_INFRA/oci-vcn> ls -l total 120 -rwxr-xr-x@ 1 fvass staff 849 Jul 10 2023 main.tf drwxr-xr-x@ 3 fvass staff 96 May 25 2023 modules -rwxr-xr-x@ 1 fvass staff 387 May 25 2023 output.tf -rw-r--r-- 1 fvass staff 379 Apr 3 09:18 provider.auto.tfvars -rw-r--r-- 1 fvass staff 29040 Apr 3 11:00 terraform.tfstate -rw-r--r-- 1 fvass staff 182 Apr 3 11:00 terraform.tfstate.backup -rwxr-xr-x@ 1 fvass staff 6007 Apr 3 10:59 terraform.tfvars -rwxr-xr-x@ 1 fvass staff 2869 May 25 2023 variables.tf
任务 2:使用 OCI 控制台创建 OCI 计算 VM
在任务 1 中创建其他 OCI 资源的同一区域中创建 OCI 计算 VM。对于本教程,我们将在任务 1 中存在的一个子网中创建该子网(不需要位于其中一个子网中)。创建后,将获得计算 Oracle Cloud 标识符 (OCID),因为这在后续任务中是必需的。
注:此 VM 目前不受 Terraform 管理。
任务 3:创建 import_block.tf
文件
在 Terraform 代码所在的同一文件夹中创建名为 import_block.tf
的文件(您可以为其指定其他名称)。
-
import_block.tf
.import { to = oci_core_instance.demo_inst_1 id = "ocid1.instance.oc1.iad.anuwcljswe6j4fqcqxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }
- id:在任务 2 中创建的计算的 OCID。
- to:资源在状态文件中将具有的实例地址。
我们将使用 oci_core_instance
类型导入计算资源。资源名称将为 demo_inst_1
。运行 terraform plan
命令时,Terraform 将读取此文件并识别它是否需要将新资源导入到现有状态文件中。
任务 4:运行 terraform plan
命令
运行以下命令。此参数 generate-config-out
将为在任务 2 中创建的计算生成包含 Terraform 配置的文件。
terraform plan --generate-config-out=compute.tf
输出应类似于:
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
╷
│ Warning: Config generation is experimental
│
│ Generating configuration during import is currently experimental, and the generated configuration format may change in future versions.
╵
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Terraform has generated configuration and written it to compute.tf. Please review the configuration and edit it as necessary before adding it to version control.
现在,我们有一个 compute.tf
文件。
~/workORCL/_MY_OCI_INFRA/oci-vcn> ls -ltr
total 144
-rwxr-xr-x@ 1 fvass staff 387 May 25 2023 output.tf
drwxr-xr-x@ 3 fvass staff 96 May 25 2023 modules
-rwxr-xr-x@ 1 fvass staff 2869 May 25 2023 variables.tf
-rw-r--r-- 1 fvass staff 379 Apr 3 09:18 provider.auto.tfvars
-rwxr-xr-x@ 1 fvass staff 6007 Apr 3 10:59 terraform.tfvars
-rw-r--r-- 1 fvass staff 182 Apr 3 11:00 terraform.tfstate.backup
-rw-r--r-- 1 fvass staff 29040 Apr 3 11:00 terraform.tfstate
-rwxr-xr-x@ 1 fvass staff 855 Apr 3 11:12 main.tf
-rw-r--r-- 1 fvass staff 142 Apr 3 11:12 import_block.tf
-rw-r--r-- 1 fvass staff 4421 Apr 3 11:17 compute.tf
任务 5:运行 terraform apply
命令
运行以下命令以将计算添加到 Terraform 状态文件。
terraform apply
输出应类似于:
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
现在,应用配置。
任务 6:检查状态文件
运行 terraform state list
命令并查看是否存在 oci_core_instance.demo_inst_1
。
~/workORCL/_MY_OCI_INFRA/oci-vcn> terraform state list
oci_core_instance.demo_inst_1
module.network.data.oci_core_services.all_oci_services
module.network.oci_core_internet_gateway.igw["igw"]
module.network.oci_core_nat_gateway.ngw["ngw"]
module.network.oci_core_route_table.route_table["rt_priv"]
module.network.oci_core_route_table.route_table["rt_pub"]
module.network.oci_core_security_list.sl["sl_priv"]
module.network.oci_core_security_list.sl["sl_public"]
module.network.oci_core_service_gateway.this["sgw"]
module.network.oci_core_subnet.subnets["sn1_priv"]
module.network.oci_core_subnet.subnets["sn1_pub"]
module.network.oci_core_subnet.subnets["sn2_priv"]
module.network.oci_core_subnet.subnets["sn3_priv"]
module.network.oci_core_virtual_network.vcn["vcn1"]
任务 7:删除 import_block.tf
文件
此时,我们可以安全地删除 import_block.tf
文件。反正 Terraform 会忽略它。
任务 8:计算测试
您可以更改计算的配置。在 compute.tf
中,从 shape_config
更改 memory_in_gbs
,然后运行 terraform plan
查看更改。
输出应类似于:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# oci_core_instance.demo_inst_1 will be updated in-place
~ resource "oci_core_instance" "demo_inst_1" {
id = "ocid1.instance.oc1.iad.anuwcljswe.........."
# (20 unchanged attributes hidden)
~ shape_config {
~ memory_in_gbs = 16 -> 32
# (8 unchanged attributes hidden)
}
# (7 unchanged blocks hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
相关链接
确认
- 作者 - Francisc Vass
更多学习资源
浏览 docs.oracle.com/learn 上的其他实验室,或者通过 Oracle Learning YouTube 频道访问更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 以成为 Oracle Learning Explorer。
有关产品文档,请访问 Oracle 帮助中心。
Import Oracle Cloud Infrastructure Resources into a Terraform State File
F96421-01
April 2024