ノート:
- このチュートリアルでは、Oracle Cloudへのアクセスが必要です。無料アカウントにサインアップするには、Oracle Cloud Infrastructure Free Tierの開始を参照してください。
- Oracle Cloud Infrastructureの資格証明、テナンシおよびコンパートメントに例の値を使用します。演習を完了するときは、これらの値をクラウド環境に固有の値に置き換えます。
Terraformを使用したOracle Cloud Infrastructureの複数のリージョンへのカスタム・イメージのコピー
イントロダクション
Oracle Cloud Infrastructure (OCI)は、インスタンスの起動にイメージを使用します。インスタンスを起動するときに、使用するイメージを指定できます。インスタンスのブート・ディスクのカスタム・イメージを作成し、他のインスタンスの起動に使用できます。カスタム・イメージから起動されたインスタンスには、元のイメージからカスタマイズ、構成およびインストールされたソフトウェアが含まれます。カスタム・イメージは、イメージのインポートおよびエクスポートを使用して、テナンシおよびリージョン間で共有できます。
目的
- Terraformを使用して、カスタム・イメージを複数のリージョンにコピーするプロセスを自動化します。
前提条件
-
Terraformをインストールおよび設定します。詳細は、Terraformのインストールを参照してください。
-
Oracle Cloud Infrastructureコマンドライン・インタフェース(OCI CLI)をインストールおよび設定します。詳細は、OCI CLIを参照してください。
-
コピーするカスタム・イメージOracle Cloud Identifier (OCID)。
-
同じ命名規則を使用して、各リージョンにOCI Object Storageバケットを作成します。たとえば、
custom-image-bucket-<region name>
です。 -
OCI Terraformプロバイダを構成します。詳細については、Configuring the Providerを参照してください。
タスク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スクリプトは、カスタム・イメージのインポートが各リージョンで同時に行われるため、カスタム・イメージのコピーにかかる時間と労力を削減します。
関連リンク
承認
- 著者 - 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
F97117-01
May 2024