附註:
- 此教學課程需要存取 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 狀態檔。
其中一個重要的是,匯入區塊可與 CI/CD 管線搭配運作。此外,它也可讓您在修改狀態之前預覽匯入作業。最後,這一切都取決於您和使用案例的複雜性。
目標
- 手動建立資源 (OCI Compute Virtual Machine) 並匯入至現有的 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 Compute VM
在任務 1 中建立其他 OCI 資源的相同區域中建立 OCI Compute VM。對於此教學課程,我們正在將它建立在作業 1 的其中一個子網路中 (不需要在其中一個子網路中)。建立之後,系統會取得運算 Oracle Cloud Identifier (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。
- 目標:資源將在您狀態檔中的執行處理位址。
我們將使用 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 Help Center 。
Import Oracle Cloud Infrastructure Resources into a Terraform State File
F96422-01
April 2024