Note:

Copy Custom Images to Multiple Regions in Oracle Cloud Infrastructure using Terraform

Introduction

Oracle Cloud Infrastructure (OCI) utilizes images for launching instances. When launching an instance, you can specify the image to use. You can create a custom image of an instance boot disk and use it for launching other instances. Instances launched from your custom image include the customizations, configurations, and installed software from the original image. Custom images can be shared across tenancies and regions using image import and export.

Objectives

Prerequisites

Task 1: Create a Variable File

Create a variable file named var.tf and add the following variables in the file.

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>"}

Task 2: Export Custom Image to Bucket Storage using Terraform

Exported images are stored in the OCI Object Storage service. To perform an image export, you need write access to the OCI Object Storage bucket for the image. OCI Terraform provider does not have inbuilt resource for exporting custom images. We will use local-exec provisioner to run OCI CLI command for exporting custom images.

Create a file named 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"
}

Task 3: Copy the Exported Custom Image object to all Regions using Terraform

Make the list of regions in which you want to copy the custom images and store it in a local value. We will use local-exec provisioner to copy the object in every region mentioned in the list. OCI CLI command will be run for each region separately.

Create a file named 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"
}

Task 4: Import Custom Images in each Region from OCI Object Storage

OCI Terraform provider does not provide resources for importing custom images from OCI Object Storage. We will again use local-exec provisioner for importing custom images in each region. Importing of images in each region will happen simultaneously.

Create a file named 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
  }
}

Next steps

Copying custom images manually through the OCI Console takes a long time. The Terraform scripts described in this tutorial will reduce the time and effort in copying the custom images as importing of custom images will be happen simultaneously in each region.

Acknowledgments

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.