附註:

使用 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 將匯出的自訂映像檔物件複製到所有區域

建立要複製自訂映像檔並將其儲存在本機值的區域清單。我們將使用 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 Help Center