附註:
- 此教學課程需要存取 Oracle Cloud。若要註冊免費帳戶,請參閱開始使用 Oracle Cloud Infrastructure Free Tier 。
- 它使用 Oracle Cloud Infrastructure 憑證、租用戶及區間的範例值。完成實驗室時,請以雲端環境特有的值取代這些值。
使用 Terraform 將自訂映像檔複製到 Oracle Cloud Infrastructure 中的多個區域
簡介
Oracle Cloud Infrastructure (OCI) 運用映像檔啟動執行處理。啟動執行處理時,您可以指定要使用的映像檔。您可以建立執行處理開機磁碟的自訂映像檔,並用於啟動其他執行處理。從自訂映像檔啟動的執行個體包括來自原始映像檔的自訂、配置和已安裝的軟體。您可以使用映像檔匯入和匯出,跨租用戶和區域共用自訂映像檔。
目標
- 使用 Terraform 自動化將自訂映像複製到多個區域的程序。
必要條件
-
安裝並設定 Terraform。如需詳細資訊,請參閱 Install Terraform 。
-
安裝並設定 Oracle Cloud Infrastructure 命令行介面 (OCI CLI)。如需詳細資訊,請參閱 OCI CLI 。
-
要複製的自訂映像檔 Oracle Cloud ID (OCID)。
-
在每個具有相同命名慣例的區域中建立 OCI 物件儲存的儲存桶。例如,
custom-image-bucket-<region name>
。 -
設定 OCI Terraform 提供者。有關詳細資訊,請參閱設定提供者。
工作 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 指令碼可減少在匯入自訂映像檔時複製自訂映像檔的時間和精力,將在每個區域同時執行。
相關連結
認可
- 作者 - Nikhil Khandelwal (企業雲端架構師)
其他學習資源
瀏覽 docs.oracle.com/learn 的其他實驗室,或前往 Oracle Learning YouTube 頻道存取更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。
如需產品文件,請造訪 Oracle Help Center 。
Copy Custom Images to Multiple Regions in Oracle Cloud Infrastructure using Terraform
F97121-01
May 2024