使用 Terraform 和 OCI 資源管理程式部署 Oracle NoSQL 表格

使用 Terraform 和資源管理程式的堆疊在 OCI (Oracle Cloud Infrastructure) 上輕鬆部署 NDCS 表格。我們將使用 OCI 資源管理程式 CLI 在 Oracle Cloud 上部署 NDCS 表格。在繼續本文之前,假設您瞭解 NoSQL Cloud Service 並瞭解其基本知識以及 Terraform。

Terraform 使用提供者連接 Terraform 引擎與支援的雲端平台。Oracle Cloud Infrastructure (OCI) Terraform 提供者是一個元件,可將 Terraform 連線至您要管理的 OCI 服務。您可以使用 OCI Terraform 提供者,包括 Terraform Cloud 和 OCI 資源管理程式

OCI 資源管理程式是以 Terraform 為基礎的 Oracle 管理服務,使用 Terraform 組態檔將 OCI Terraform 提供者支援的 OCI 資源部署和作業自動化。資源管理程式可讓您跨多個團隊和平台共用及管理基礎架構組態和狀態檔案。


使用 Terraform 部署 NoSQL 資料庫表格

  • 若要在 OCI 中建立資源,我們需要設定 terraform。建立 terraform 提供者定義、NoSQL 資源定義、認證以及輸入變數的基本 terraform 組態檔。
  • 決定地形配置檔的儲存位置。您可以將這些檔案儲存在不同的來源,例如本機資料夾或壓縮檔、物件儲存的儲存桶,以及來源控制系統,例如 GitHub 或 GitLab。
  • 執行「資源管理程式 CLI」命令以執行下列作業:
    • 建立堆疊。
    • 產生並複查執行計畫。
    • 執行「套用」工作以啟動設定 NoSQL 資源。
    • 視需要檢視日誌檔。

附註:

我們將使用 Oracle Cloud Infrastructure (OCI) 資源管理程式命令行介面 (CLI),並使用主控台在 Cloud Shell 中執行這些命令。這表示您需要一些關於雲端租用戶和其他項目的相關資訊,例如公鑰或私鑰對。若要在您的本機機器上設定 OCI CLI,請參閱此文件

本文包含下列主題:

必備條件

  • Terraform 的基本瞭解。點選此處閱讀簡介。
  • Oracle Cloud 帳戶和 Oracle NoSQL Database Cloud Service 的訂閱。如果您還沒有 Oracle Cloud 帳戶,請從此處開始。
  • OCI Terraform 提供者已安裝已設定

步驟 1:建立 NDCS 表格或索引的 Terraform 組態檔


建立 OCI Terraform 提供者組態

子步驟 1.1:建立 OCI Terraform 提供者組態

建立一個名為 "provider.tf " 的新檔案,其中包含 OCI Terraform 提供者定義以及關聯的變數定義。OCI Terraform 提供者只需要 region 引數。

不過,您可能必須根據認證方法,以 OCI 帳戶的認證證明資料設定其他引數。OCI Terraform 提供者支援三種認證方法:
  • API 金鑰認證
  • 執行處理主體授權
  • 安全權杖認證
region 引數指定在其中建立提供者資源的地理區域。若要以單一組態鎖定多個區域,您只需為每個區域建立提供者定義,然後使用提供者別名來區分,如下列範例所示。請注意,僅定義一個名為 "oci" 的提供者,但針對 us-phoenix-1 區域 (別名為 "phx") 輸入兩次 oci 提供者定義,並為區域 us-ashburn-1 輸入一次 (別名為 "iad")。
provider "oci" {
region = "us-phoenix-1"
   alias = "phx"
}
provider "oci" {
region = "us-ashburn-1"
alias = "iad"
}
在下面的範例中,OCI Terraform 提供者需要 region 引數。API 金鑰認證需要 tenancy_ociduser_ocidprivate_key_pathfingerprint 引數。您可以提供 region 和 API 金鑰認證金鑰 (tenancy_ociduser_ocidprivate_key_pathfingerprint) 的值作為環境變數,或在 Terraform 組態變數內 (如子步驟 1.3:載入 Terraform 組態變數中所述)。
variable "tenancy_ocid" {
}
variable "user_ocid" {
}
variable "fingerprint" {
}
variable "private_key_path" {
}
variable "region" {
}

provider "oci" {
   region = var.region
   tenancy_ocid = var.tenancy_ocid
   user_ocid = var.user_ocid
   fingerprint = var.fingerprint
   private_key_path = var.private_key_path
}
執行處理主要項目授權可讓您的提供者從 OCI 運算執行處理進行 API 呼叫,而不需要您提供者定義中的 tenancy_ociduser_ocidprivate_key_pathfingerprint 屬性。

附註:

執行處理主要授權僅適用於在 Oracle Cloud Infrastructure 中執行的執行處理。
在下面的範例中,OCI Terraform 提供者需要 region 引數,而執行處理主體授權需要 auth 引數。您可以提供 region 引數的值作為「環境變數」或 Terraform 組態變數 (如子步驟 1.3:載入 Terraform 組態變數中所述)。
variable "region" {
}
provider "oci" {
   auth = "InstancePrincipal"
   region = var.region
}
安全權杖認證可讓您使用用於 CLI 的權杖型驗證產生的權杖來執行 Terraform。

附註:

此權杖會在 1 小時後到期。佈建資源的時間超過 1 小時,請勿使用此認證方法。如需詳細資訊,請參閱重新整理記號
在下面的範例中,region 是 OCI Terraform 提供者的必要引數。Security Token 認證需要 authconfig_file_profile 引數。
variable "region" {
}
variable "config_file_profile" {
}
provider "oci" {
auth = "SecurityToken"
  config_file_profile = var.config_file_profile
   region = var.region
}

子步驟 1.2:建立 NoSQL Terraform 組態

建立一個名為 "nosql.tf" 的新檔案,其中包含用於建立 NoSQL Database Cloud Service 表格或索引的 NoSQL terraform 組態資源。如需有關 NoSQL 資料庫資源和資料來源的詳細資訊,請參閱 oci_nosql_table

在下面的範例中,我們建立 2 個 NoSQL 表格。NoSQL 資料庫資源 (例如表格和索引) 需要 compartment_ocid 引數。您可以提供 compartment_ocid 的值作為環境變數或 Terraform 組態變數 (如子步驟 1.3:載入 Terraform 組態變數中所述)。
variable "compartment_ocid" {
}

resource "oci_nosql_table" "nosql_demo" {
    compartment_id = var.compartment_ocid
    ddl_statement = "CREATE TABLE if not exists demo (ticketNo INTEGER, fullName STRING, contactPhone STRING, confNo STRING, gender STRING, bagInfo JSON, PRIMARY KEY (ticketNo))"
    name = "demo"
    table_limits {
        max_read_units = var.table_table_limits_max_read_units
        max_storage_in_gbs = var.table_table_limits_max_storage_in_gbs
        max_write_units = var.table_table_limits_max_write_units
    }
}

resource "oci_nosql_table" "nosql_demoKeyVal" {

    compartment_id = var.compartment_ocid
    ddl_statement = "CREATE TABLE if not exists demoKeyVal (key INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 NO CYCLE), value JSON, PRIMARY KEY (key))"
    name = "demoKeyVal"
    table_limits {
       max_read_units = var.table_table_limits_max_read_units
        max_storage_in_gbs = var.table_table_limits_max_storage_in_gbs
        max_write_units = var.table_table_limits_max_write_units
    }
}

子步驟 1.3:載入 Terraform 組態變數

下一步是建立名為 "terraform.tfvars" 的檔案,並根據認證方法提供必要的 OCI Terraform 提供者引數值。

提供 IAM 存取和秘密金鑰 (tenancy_ociduser_ocidprivate_key_pathfingerprint)、regioncompartment_ocid 引數的值。您應該已經有具備加密密碼和存取金鑰的 OCI IAM 使用者具備 NoSQL Database Cloud Service 的足夠權限。取得這些金鑰並將它們儲存到檔案中 。

舉例而言:
tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaaqljdu37xcfoqvyj47pf5dqutpxu4twoqc7hukwgpbavpdwkqxc6q"
user_ocid = "ocid1.user.oc1..aaaaaaaafxz473ypsc6oqiespihan6yi6obse3o4e4t5zmpm6rdln6fnkurq"
fingerprint = "2c:9b:ed:12:81:8d:e6:18:fe:1f:0d:c7:66:cc:03:3c"
private_key_path = "~/NoSQLLabPrivateKey.pem"
compartment_ocid = "ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya"
region = "us-phoenix-1"

提供 regioncompartment_ocid 引數的值。

舉例而言:
region = "us-phoenix-1"
compartment_ocid = "ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya"

提供 regioncompartment_ocidconfig_file_profile 引數的值。

舉例而言:
region = "us-phoenix-1"
compartment_ocid = "ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya"
config_file_profile = "PROFILE"

子步驟 1.4:宣告輸入變數

最後一個步驟是建立 "variables.tf",並將值指派給其中的輸入變數。按一下此處以參閱 Terraform 文件,檢查 NoSQL 資料庫可用的有效引數或特性。在此範例中,NoSQL 表格的讀取、寫入和儲存單位預設值分別設為 10101
variable "table_table_limits_max_read_units" {
default = 10
}
variable "table_table_limits_max_write_units" {
default = 10
}
variable "table_table_limits_max_storage_in_gbs" {
default = 1
}

我們使用主控台的 Cloud Shell 建立了提供者定義、NoSQL 資料庫資源、認證值及輸入變數的所有必要 terraform 組態檔。

在 Cloud Shell 中建立 Terraform 組態檔

步驟 2:儲存 Terraform 組態的位置


在哪裡儲存您的 terraform 組態檔

使用「資源管理程式」建立堆疊時,您可以從下列來源選取 Terraform 組態。

子步驟 2.1:建立遠端 Terraform 組態的組態來源提供者

若要使用由來源控制系統代管的遠端地形組態,例如 GitHub 和 GitLab,您必須建立來源組態提供者。

如需如何為遠端 Terraform 組態建立組態來源提供者的詳細資訊,請參閱管理組態來源提供者

步驟 3:從檔案建立堆疊


從檔案建立堆疊

使用與檔案位置相關的指令。如需「資源管理程式」支援的 Terraform 組態來源,請參閱儲存 Terraform 組態的位置

本教學課程將使用本機 terraform 組態壓縮檔 terraform.zip 中的執行處理主體認證方法建立堆疊。terraform.zip 檔案包含下列檔案:
  • provider.tf
  • nosql.tf
  • terraform.tfvars
  • variables.tf

附註:

在本教學課程中,我們使用 OCI Resource Manager CLI 命令來建立堆疊。您可以使用「OCI 資源管理程式主控台」執行相同作業。
從原始程式碼控制系統上代管的檔案 (例如 GitHub 和 GitLab) 建立堆疊。
oci resource-manager stack create-from-git-provider 
--compartment-id ocid1.tenancy.oc1..uniqueid 
--config-source-configuration-source-provider-id ocid.ormconfigsourceprovider.oc1..uniqueid 
--config-source-repository-url https://github.com/user/repo.git
--config-source-branch-name mybranch 
--display-name "My Stack from Git" 
--description "Create NoSQL Table" 
--variables file://variables.json
--working-directory ""
從物件儲存的儲存桶中的 Terraform 組態建立堆疊。
oci resource-manager stack create-from-object-storage 
--compartment-id ocid1.tenancy.oc1..uniqueid 
--config-source-namespace MyNamespace
--config-source-bucket-name MyBucket
--config-source-region PHX
--display-name "My Stack from Object Storage" 
--description "Create NoSQL Table" 
--variables file://variables.json
從上傳的組態檔 (.zip) 建立堆疊
oci resource-manager stack create 
--compartment-id ocid1.tenancy.oc1..uniqueid 
--config-source vcn.zip 
--variables file://variables.json
--display-name "My Example Stack" 
--description "Create NoSQL Table" 
--working-directory ""
其中,
  • --compartment-id 是您要建立堆疊之區間的 OCID。
  • --config-source 是包含一或多個 Terraform 組態檔的 .zip 檔案名稱。
  • (選擇性) --variables 是指定資源輸入變數的檔案路徑。

    Oracle Cloud Infrastructure Terraform 提供者在本機執行 Terraform 時需要額外的參數 (除非您使用執行處理主體)。如需在 Terraform 中使用變數的詳細資訊,請參閱輸入變數。另請參閱輸入變數組態

  • (選擇性) --display-name 是新堆疊的易記名稱。
  • (選擇性) --description 是新堆疊的描述。
  • (可選擇) --working-directory 是目錄中的根配置檔案。如果未指定,或在此範例中為空值,則服務會假設目錄中的最上層檔案是根組態檔。
舉例而言:
oci resource-manager stack create 
--compartment-id ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya 
--config-source terraform.zip
範例回應:
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya",
    "config-source": {
      "config-source-type": "ZIP_UPLOAD",
      "working-directory": null
    },
    "defined-tags": {},
    "description": null,
    "display-name": "ormstack20220117104810",
    "freeform-tags": {},
    "id": "ocid1.ormstack.oc1.phx.aaaaaaaa7jrci2s5iav5tdxpl6ucwo2dwazzrdkfhs6bxiohgcpkscmr57bq",
    "lifecycle-state": "ACTIVE",
    "stack-drift-status": "NOT_CHECKED",
    "terraform-version": "1.0.x",
    "time-created": "2022-01-17T10:48:10.878000+00:00",
    "time-drift-last-checked": null,
    "variables": {}
  },
  "etag": "dd62ace0b9e9d825d825c05d4588b73fede061e55b75de6436b84fb2bb794185"
}
我們已從 terraform 組態檔建立堆疊並產生堆疊 ID。在下一個步驟中,此堆疊 ID 是用來產生 NoSQL 表格建置的執行計畫。
"id": "ocid1.ormstack.oc1.phx.aaaaaaaa7jrci2s5iav5tdxpl6ucwo2dwazzrdkfhs6bxiohgcpkscmr57bq"

步驟 4:產生執行計畫


產生及複查執行計畫

附註:

在本教學課程中,我們使用 OCI Resource Manager CLI 命令來產生執行計畫。您可以使用「OCI 資源管理程式主控台」執行相同作業。
如果要產生執行計畫,請執行下列命令:
oci resource-manager job create-plan-job 
–-stack-id <stack_OCID> 
--display-name "<friendly_name>"
舉例而言:
oci resource-manager job create-plan-job 
--stack-id ocid1.ormstack.oc1.phx.aaaaaaaa7jrci2s5iav5tdxpl6ucwo2dwazzrdkfhs6bxiohgcpkscmr57bq
範例回應:
{
  "data": {
    "apply-job-plan-resolution": null,
    "cancellation-details": {
      "is-forced": false
    },
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya",
    "config-source": {
      "config-source-record-type": "ZIP_UPLOAD"
    },
    "defined-tags": {},
    "display-name": "ormjob20220117104856",
    "failure-details": null,
    "freeform-tags": {},
    "id": "ocid1.ormjob.oc1.phx.aaaaaaaacrylnpglae4yvwo4q2r2tk5z5x5v6bwjsoxgn26moyg3eqwnt2aq",
    "job-operation-details": {
      "operation": "PLAN",
      "terraform-advanced-options": {
        "detailed-log-level": null,
        "is-refresh-required": true,
        "parallelism": 10
      }
    },
    "lifecycle-state": "ACCEPTED",
    "operation": "PLAN",
    "resolved-plan-job-id": null,
    "stack-id": "ocid1.ormstack.oc1.phx.aaaaaaaa7jrci2s5iav5tdxpl6ucwo2dwazzrdkfhs6bxiohgcpkscmr57bq",
    "time-created": "2022-01-17T10:48:56.324000+00:00",
    "time-finished": null,
    "variables": {},
    "working-directory": null
  },
  "etag": "a6f75ec1e205cd9105705fd7c8d65bf262159a7e733b27148049e70ce6fc14fe"
}
我們已從堆疊產生執行計畫。「資源管理程式」會使用與此執行計畫對應的唯一 ID 來建立工作。此計畫工作 ID 之後可用來複查執行計畫詳細資訊,再執行套用作業在 OCI 雲端部署 NoSQL 資料庫資源。
"id": "ocid1.ormjob.oc1.phx.aaaaaaaacrylnpglae4yvwo4q2r2tk5z5x5v6bwjsoxgn26moyg3eqwnt2aq",
"job-operation-details": {
      "operation": "PLAN"
      ...
}

子步驟 4.1:複查執行計畫

如果要複查執行計畫,請執行下列命令:
oci resource-manager job get-job-logs 
--job-id <plan_job_OCID>
舉例而言:
oci resource-manager job get-job-logs 
--job-id ocid1.ormjob.oc1.phx.aaaaaaaacrylnpglae4yvwo4q2r2tk5z5x5v6bwjsoxgn26moyg3eqwnt2aq
範例回應:
...
    {
      "level": "INFO",
      "message": "Terraform used the selected providers to generate the following execution",
      "timestamp": "2022-01-17T10:49:21.634000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "plan. Resource actions are indicated with the following symbols:",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  + create",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "Terraform will perform the following actions:",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  # oci_nosql_table.nosql_demo will be created",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  + resource \"oci_nosql_table\" \"nosql_demo\" {",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + compartment_id      = \"ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya\"",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + ddl_statement       = \"CREATE TABLE if not exists demo (ticketNo INTEGER, fullName STRING, contactPhone STRING, confNo STRING, gender STRING, bagInfo JSON, PRIMARY KEY (ticketNo))\"",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + is_auto_reclaimable = true",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + name                = \"demo\"",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + table_limits {",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_read_units     = 10",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_storage_in_gbs = 1",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_write_units    = 10",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  # oci_nosql_table.nosql_demoKeyVal will be created",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  + resource \"oci_nosql_table\" \"nosql_demoKeyVal\" {",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + compartment_id      = \"ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya\"",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + ddl_statement       = \"CREATE TABLE if not exists demoKeyVal (key INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 NO CYCLE), value JSON, PRIMARY KEY (key))\"",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + is_auto_reclaimable = true",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + name                = \"demoKeyVal\"",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + table_limits {",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_read_units     = 10",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_storage_in_gbs = 1",
      "timestamp": "2022-01-17T10:49:21.635000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_write_units    = 10",
      "timestamp": "2022-01-17T10:49:21.636000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "Plan: 2 to add, 0 to change, 0 to destroy.",
      "timestamp": "2022-01-17T10:49:21.636000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
...
此步驟非常重要,因為它驗證堆疊程式碼是否包含任何語法錯誤,以及新增、更新或銷毀多少 OCI 資源。在教學課程中,我們正在部署兩個 NoSQL 表格:demodemoKeyVal
{
 ...
      "message": "Plan: 2 to add, 0 to change, 0 to destroy.",
 ...
}

步驟 5:執行套用工作


執行「套用」工作以佈建資源

  • 若要指定計畫工作 (套用執行計畫),請使用 FROM_PLAN_JOB_ID
    oci resource-manager job create-apply-job 
    --stack-id <stack_OCID> 
    --execution-plan-strategy FROM_PLAN_JOB_ID 
    --execution-plan-job-id <plan_job_OCID> 
    --display-name "Example Apply Job"
  • 若要自動核准套用工作 (未指定計畫工作),請使用 AUTO_APPROVED
    oci resource-manager job create-apply-job 
    --stack-id <stack_OCID> 
    --execution-plan-strategy AUTO_APPROVED 
    --display-name "Example Apply Job"
舉例而言:
oci resource-manager job create-apply-job 
--stack-id ocid1.ormstack.oc1.phx.aaaaaaaa7jrci2s5iav5tdxpl6ucwo2dwazzrdkfhs6bxiohgcpkscmr57bq 
--execution-plan-strategy AUTO_APPROVED 
--display-name "Create NoSQL Tables Using Terraform"
範例回應:
{
  "data": {
    "apply-job-plan-resolution": {
      "is-auto-approved": true,
      "is-use-latest-job-id": null,
      "plan-job-id": null
    },
    "cancellation-details": {
      "is-forced": false
    },
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya",
    "config-source": {
      "config-source-record-type": "ZIP_UPLOAD"
    },
    "defined-tags": {},
    "display-name": "Create NoSQL Tables Using Terraform",
    "failure-details": null,
    "freeform-tags": {},
    "id": "ocid1.ormjob.oc1.phx.aaaaaaaaqn4nsnfgi3th4rxolwqn3kftzzdpsw52pnfeyphi5dsxd6fhescq",
    "job-operation-details": {
      "execution-plan-job-id": null,
      "execution-plan-strategy": "AUTO_APPROVED",
      "operation": "APPLY",
      "terraform-advanced-options": {
        "detailed-log-level": null,
        "is-refresh-required": true,
        "parallelism": 10
      }
    },
    "lifecycle-state": "ACCEPTED",
    "operation": "APPLY",
    "resolved-plan-job-id": null,
    "stack-id": "ocid1.ormstack.oc1.phx.aaaaaaaa7jrci2s5iav5tdxpl6ucwo2dwazzrdkfhs6bxiohgcpkscmr57bq",
    "time-created": "2022-01-17T10:54:46.346000+00:00",
    "time-finished": null,
    "variables": {},
    "working-directory": null
  },
  "etag": "4042a300e8f678dd6da0f49ffeccefed66902b51331ebfbb559da8077a728126"
}
我們已經從堆疊對執行計畫執行套用作業。「資源管理程式」會建立具有唯一 ID 的工作,以執行套用作業。此套用工作 ID 之後可用來複查在 OCI 雲端部署 NoSQL 資料庫表格時產生的日誌。
"id": "ocid1.ormjob.oc1.phx.aaaaaaaaqn4nsnfgi3th4rxolwqn3kftzzdpsw52pnfeyphi5dsxd6fhescq",
"job-operation-details": {
      "operation": "APPLY"
      ...
}

附註:

如果您使用專用主機環境,請先設定環境變數 CLIENT_HOST_OVERRIDES 以指定自訂端點,再執行工作。範例:
export CLIENT_HOST_OVERRIDES=<oci_nosql.NosqlClient=https://ndcs.us-ashburn-1.oci.oc-test.com>

子步驟 5.1:驗證工作狀態

若要驗證工作的狀態,請執行下列命令:
oci resource-manager job get 
--job-id <job_OCID>
舉例而言:
oci resource-manager job get 
--job-id ocid1.ormjob.oc1.phx.aaaaaaaaqn4nsnfgi3th4rxolwqn3kftzzdpsw52pnfeyphi5dsxd6fhescq
範例回應:
{
  "data": {
    "apply-job-plan-resolution": {
      "is-auto-approved": true,
      "is-use-latest-job-id": null,
      "plan-job-id": null
    },
    "cancellation-details": {
      "is-forced": false
    },
    "compartment-id": "ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya",
    "config-source": {
      "config-source-record-type": "ZIP_UPLOAD"
    },
    "defined-tags": {},
    "display-name": "Create NoSQL Tables Using Terraform",
    "failure-details": null,
    "freeform-tags": {},
    "id": "ocid1.ormjob.oc1.phx.aaaaaaaaqn4nsnfgi3th4rxolwqn3kftzzdpsw52pnfeyphi5dsxd6fhescq",
    "job-operation-details": {
      "execution-plan-job-id": null,
      "execution-plan-strategy": "AUTO_APPROVED",
      "operation": "APPLY",
      "terraform-advanced-options": {
        "detailed-log-level": null,
        "is-refresh-required": true,
        "parallelism": 10
      }
    },
    "lifecycle-state": "SUCCEEDED",
    "operation": "APPLY",
    "resolved-plan-job-id": null,
    "stack-id": "ocid1.ormstack.oc1.phx.aaaaaaaa7jrci2s5iav5tdxpl6ucwo2dwazzrdkfhs6bxiohgcpkscmr57bq",
    "time-created": "2022-01-17T10:54:46.346000+00:00",
    "time-finished": "2022-01-17T10:55:28.853000+00:00",
    "variables": {},
    "working-directory": null
  },
  "etag": "9e9f524b87e3c47b3f3ea3bbb4c1f956172a48e4c2311a44840c8b96e318bcaf--gzip"
}
您可以檢查套用工作的狀態,以驗證工作是否為 SUCCESSFUL 或 FAILED。
{
...
      "lifecycle-state": "SUCCEEDED",
...
}

子步驟 5.2:檢視工作的日誌

若要檢視工作的記錄,請執行下列指令:
oci resource-manager job get-job-logs-content 
--job-id <job_OCID>
舉例而言:
oci resource-manager job get-job-logs-content
--job-id ocid1.ormjob.oc1.phx.aaaaaaaaqn4nsnfgi3th4rxolwqn3kftzzdpsw52pnfeyphi5dsxd6fhescq
範例回應:
...
    {
      "level": "INFO",
      "message": "Terraform will perform the following actions:",
      "timestamp": "2022-01-17T10:55:05.580000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "",
      "timestamp": "2022-01-17T10:55:05.580000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  # oci_nosql_table.nosql_demo will be created",
      "timestamp": "2022-01-17T10:55:05.580000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  + resource \"oci_nosql_table\" \"nosql_demo\" {",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + compartment_id      = \"ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya\"",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + ddl_statement       = \"CREATE TABLE if not exists demo (ticketNo INTEGER, fullName STRING, contactPhone STRING, confNo STRING, gender STRING, bagInfo JSON, PRIMARY KEY (ticketNo))\"",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + is_auto_reclaimable = true",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + name                = \"demo\"",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + table_limits {",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_read_units     = 10",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_storage_in_gbs = 1",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_write_units    = 10",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  # oci_nosql_table.nosql_demoKeyVal will be created",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "  + resource \"oci_nosql_table\" \"nosql_demoKeyVal\" {",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + compartment_id      = \"ocid1.compartment.oc1..aaaaaaaawrmvqjzoegxbsixp5k3b5554vlv2kxukobw3drjho3f7nf5ca3ya\"",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + ddl_statement       = \"CREATE TABLE if not exists demoKeyVal (key INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 NO CYCLE), value JSON, PRIMARY KEY (key))\"",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + is_auto_reclaimable = true",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + name                = \"demoKeyVal\"",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "      + table_limits {",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_read_units     = 10",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_storage_in_gbs = 1",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "          + max_write_units    = 10",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "Plan: 2 to add, 0 to change, 0 to destroy.",
      "timestamp": "2022-01-17T10:55:05.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "oci_nosql_table.nosql_demoKeyVal: Creating...",
      "timestamp": "2022-01-17T10:55:06.581000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "oci_nosql_table.nosql_demo: Creating...",
      "timestamp": "2022-01-17T10:55:06.582000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "oci_nosql_table.nosql_demoKeyVal: Creation complete after 6s [id=ocid1.nosqltable.oc1.phx.amaaaaaau7x7rfyaqgpbjucp3s6jjzpnar4lg5yudxhwlqrlbd54l3wdo7hq]",
      "timestamp": "2022-01-17T10:55:12.582000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "oci_nosql_table.nosql_demo: Creation complete after 9s [id=ocid1.nosqltable.oc1.phx.amaaaaaau7x7rfyasvdkoclhgryulgzox3nvlxb2bqtlxxsrvrc4zxr6lo4a]",
      "timestamp": "2022-01-17T10:55:15.583000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
    {
      "level": "INFO",
      "message": "Apply complete! Resources: 2 added, 0 changed, 0 destroyed.",
      "timestamp": "2022-01-17T10:55:15.583000+00:00",
      "type": "TERRAFORM_CONSOLE"
    },
...
此步驟非常重要,因為它確認新增、更新或銷毀了多少 OCI 資源。在教學課程中,我們已成功部署兩個 NoSQL 表格:demodemoKeyVal
{
 ...
      "message": "Apply complete! Resources: 2 added, 0 changed, 0 destroyed.",
 ...
}

本教學課程提供許多詳細資訊。我們建立了在 OCI 雲端部署 NoSQL 資料庫表格所需的 terraform 組態檔,然後設定這些檔案的來源位置。接著,我們使用 OCI Resource Manager CLI 建立堆疊、產生執行計畫,以及在執行計畫上執行套用工作。