使用 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 配置文件。
  • 决定将 terraform 配置文件存储在何处。您可以将这些文件存储在不同的源中,例如本地文件夹或 zip、对象存储存储桶以及源控制系统(例如 GitHub 或 GitLab)。
  • 运行资源管理器 CLI 命令以执行以下任务:
    • 创建堆栈。
    • 生成和复核执行计划。
    • 运行“应用”作业以预配 NoSQL 资源。
    • 根据需要复核日志文件。

注意:

我们将使用 Oracle Cloud Infrastructure (OCI) 资源管理器命令行界面 (Command Line Interface,CLI) 并使用控制台在 Cloud Shell 中执行这些命令。这意味着您需要一些有关云租户和其他项目的信息,例如公钥或私钥对。如果您要在本地计算机上配置 OCI CLI,请参阅此文档

本文章包含以下主题:

Prerequisites

  • 基本了解 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" 的提供程序,但输入了两次 oci 提供程序定义,一次针对 us-phoenix-1 区域(使用别名 "phx"),一次针对区域 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。

注意:

此令牌将在一个小时后过期。预配资源所用时间超过一小时时,请避免使用此验证方法。有关详细信息,请参阅刷新令牌
在下面的示例中,region 需要为 OCI Terraform 提供程序提供一个参数。安全令牌验证需要 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 配置,则需要创建源配置提供程序。

有关如何为远程 Terraform 配置创建配置源提供程序的详细信息,请参阅管理配置源提供程序

步骤 3:从文件创建堆栈


从文件创建堆栈

使用与文件位置相关的命令。有关资源管理器支持的 Terraform 配置源,请参阅在何处存储 Terraform 配置

对于本教程,我们将使用本地 terraform 配置 zip 文件 terraform.zip 中的实例主用户验证方法创建堆栈。terraform.zip 文件包含以下文件:
  • provider.tf
  • nosql.tf
  • terraform.tfvars
  • variables.tf

注意:

在本教程中,我们使用 OCI 资源管理器 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 是指定资源的输入变量的文件的路径。

    在本地运行 Terraform 时,Oracle Cloud Infrastructure Terraform 提供程序需要其他参数(除非您使用的是实例主体)。有关在 Terraform 中使用变量的详细信息,请参阅输入变量。另请参阅输入变量配置

  • (可选)--display-name 是新堆栈的友好名称。
  • (可选)--description 是新堆栈的说明。
  • (可选)--working-directory 是目录中的根配置文件。如果未指定,或者在本示例中为 null,则服务假定目录中的顶层文件是根配置文件。
例如:
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 资源管理器 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"
}
您可以检查应用作业的状态,以验证作业是“成功”还是“失败”。
{
...
      "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 资源管理器 CLI 创建堆栈,生成执行计划,并在执行计划上运行应用作业。