注意:

使用 Terraform 将自定义映像复制到 Oracle Cloud Infrastructure 中的多个区域

简介

Oracle Cloud Infrastructure (OCI) 利用映像来启动实例。启动实例时,您可以指定要使用的映像。您可以创建实例引导磁盘的定制映像并使用它来启动其他实例。从定制映像启动的实例包括来自原始映像的定制、配置和安装的软件。可以使用映像导入和导出在租户和区域之间共享定制映像。

目标

先决条件

任务 1:创建变量文件

创建名为 var.tf 的变量文件并在文件中添加以下变量。

variable compartment_id { default = "<compartment ocid>" }
variable region { default = "<Region name where base custom image will be exported>" }
variable custom_image_ocid {default = "<custom image ocid>"}
variable bucket_name { default = "prefix of bucket names of each region"}
variable os_namespace { default = "<namespace of object storage where base custom image will be exported>"}
variable imported_image_name { default = "<custom image name which will be imported in all regions>"}
variable base_image_name { default = "<custom image name which needs to be copied>"}

任务 2:使用 Terraform 将定制映像导出到存储桶存储

导出的映像存储在 OCI 对象存储服务中。要执行映像导出,您需要对映像的 OCI 对象存储存储桶具有写入访问权限。OCI Terraform 提供程序没有用于导出自定义映像的内置资源。我们将使用 local-exec 预配程序运行 OCI CLI 命令导出定制映像。

创建名为 export_image.tf 的文件。

# Export the custom image to Object Storage
resource "null_resource" "export_image" {
  provisioner "local-exec" {
    command = "oci compute image export to-object --image-id ${var.custom_image_ocid} --bucket-name ${var.bucket_name}_${var.region} --namespace ${var.os_namespace} --name ${var.base_image_name}"
  }
}

resource "time_sleep" "wait_15_min" {
  depends_on = [null_resource.export_image]

  create_duration = "900s"
}

任务 3:使用 Terraform 将“Exported Custom Image(导出的定制映像)”对象复制到所有区域

列出要在其中复制定制映像并将其存储在本地值中的区域。我们将使用 local-exec 预配程序复制列表中提到的每个区域中的对象。将为每个区域单独运行 OCI CLI 命令。

创建名为 copy_image_object.tf 的文件。

locals {
  regions = [
    "us-ashburn-1",
    "us-phoenix-1",
    // Add more regions as needed
  ]
}

resource "null_resource" "copy_objects" {
  # depends_on = [time_sleep.wait_15_min]
  count = length(local.regions)
  triggers = {
    region_index = count.index
  }
  provisioner "local-exec" {
    command = <<EOF
      oci os object copy -bn ${var.bucket_name}_${var.region} --source-object-name ${var.base_image_name} --destination-bucket ${var.bucket_name}_${local.regions[count.index]}  --destination-region ${local.regions[count.index]} -ns ${var.os_namespace}
    EOF
  }
}

resource "time_sleep" "wait_60_seconds" {
  depends_on = [null_resource.copy_objects]

  create_duration = "60s"
}

任务 4:从 OCI 对象存储中导入每个区域中的定制映像

OCI Terraform 提供程序不提供从 OCI 对象存储导入定制映像的资源。我们将再次使用 local-exec 预配程序导入每个区域中的定制映像。将同时导入每个区域的图像。

创建名为 import_image.tf 的文件。

resource "null_resource" "import-image" {
  depends_on = [time_sleep.wait_60_seconds]
  count = length(local.regions)

  triggers = {
    region_index = count.index
  }

  provisioner "local-exec" {
    command = <<EOF
      oci compute image import from-object --compartment-id ${var.compartment_id} -ns ${var.os_namespace} --bucket-name ${var.bucket_name}_${local.regions[count.index]} --name ${var.base_image_name} --region ${local.regions[count.index]} --display-name ${var.imported_image_name}
    EOF
  }
}

后续步骤

通过 OCI 控制台手动复制定制映像需要很长时间。本教程中介绍的 Terraform 脚本将减少复制定制映像所需的时间和精力,因为每个区域中将同时导入定制映像。

确认

更多学习资源

浏览 docs.oracle.com/learn 上的其他实验室,或者通过 Oracle Learning YouTube 频道访问更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 以成为 Oracle Learning Explorer。

有关产品文档,请访问 Oracle 帮助中心