注意:
- 此教程需要访问 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 标识符 (OCID)。
-
在每个区域中按照相同的命名约定创建 OCI 对象存储桶。例如,
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 对象存储服务中。要执行映像导出,您需要对映像的 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 将“Exported Custom Image(导出的定制映像)”对象复制到所有区域
列出要在其中复制定制映像并将其存储在本地值中的区域。我们将使用 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 帮助中心。
Copy Custom Images to Multiple Regions in Oracle Cloud Infrastructure using Terraform
F97120-01
May 2024