ノート:

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 Object Storageサービスに格納されます。イメージのエクスポートを実行するには、イメージに対するOCI Object Storageバケットへの書込みアクセス権が必要です。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を参照してください。