Deploy Export Image to OBS

Application Scenario

Image Management Service (IMS) is an image management service provided by Huawei Cloud, supporting image creation, sharing, copying, exporting, and other functions. By exporting images to OBS, you can export private images as image files and store them in OBS buckets, achieving image backup, migration, and offline storage. This best practice will introduce how to use Terraform to automatically deploy export images to OBS, including querying private images, creating OBS buckets, and exporting images to OBS buckets.

This best practice involves the following main resources and data sources:

Data Sources

Resources

Resource/Data Source Dependencies

data.huaweicloud_images_images
    └── huaweicloud_obs_bucket
        └── huaweicloud_ims_image_export

Note: Image export requires creating an OBS bucket first. The exported image files will be stored in the specified OBS bucket, supporting multiple file formats including vhd, zvhd, vmdk, raw, qcow2, zvhd2, vdi, etc.

Operation Steps

1. Script Preparation

Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.

2. Query Private Images

Add the following script to the TF file (e.g., main.tf) to query private images:

Parameter Description:

  • visibility: Image visibility, set to "private" to query private images

  • image_type: Image type, assigned by referencing input variable image_type, optional parameter, used to filter images of specific types (such as ECS, BMS, etc.)

  • os: Operating system type, assigned by referencing input variable image_os, optional parameter, used to filter images of specific operating systems (such as Ubuntu, CentOS, etc.)

  • name_regex: Image name regular expression, assigned by referencing input variable image_name_regex, optional parameter, used to filter images by name

  • active_images: Local variable, used to filter images with status "active", only images with status "active" can be exported

Note: Only images with status "active" can be exported. You can filter images to be exported by setting parameters such as image_type, image_os, name_regex, etc.

3. Create OBS Bucket

Add the following script to the TF file (e.g., main.tf) to create OBS bucket:

Parameter Description:

  • bucket: OBS bucket name, assigned by referencing input variable obs_bucket_name, globally unique

  • storage_class: Storage class, set to "STANDARD" for standard storage

  • region: Region, assigned by referencing input variable region_name

  • tags: Tags, assigned by referencing input variable obs_bucket_tags, optional parameter

Note: OBS bucket name needs to be globally unique. It is recommended to use meaningful naming rules. Storage class can be selected according to actual needs. Standard storage is suitable for frequently accessed scenarios.

4. Export Images to OBS

Add the following script to the TF file (e.g., main.tf) to export images to OBS:

Parameter Description:

  • count: Number of resource creations, assigned by referencing the length of local variable active_images, creating one export task for each active image

  • region: Region, assigned by referencing input variable region_name

  • image_id: Image ID, assigned by referencing local variable active_images

  • bucket_url: OBS bucket URL, format is "bucket:object_key", generated by combining OBS bucket name, image name, image ID, and file format

  • file_format: File format, assigned by referencing input variable file_format, supporting formats such as vhd, zvhd, vmdk, raw, qcow2, zvhd2, vdi, default value is "zvhd2"

  • depends_on: Explicit dependency relationship, ensuring to export images after OBS bucket is created

Note: Image export may take a long time, please be patient. The exported image files will be stored in the specified OBS bucket, with file name format as "image-name-image-id.file-format". Supported file formats include vhd, zvhd, vmdk, raw, qcow2, zvhd2, vdi, etc. It is recommended to select an appropriate format according to actual needs.

5. Preset Input Parameters Required for Resource Deployment (Optional)

In this practice, some resources and data sources use input variables to assign configuration content. These input parameters need to be manually entered during subsequent deployment. At the same time, Terraform provides a method to preset these configurations through tfvars files, which can avoid repeated input during each execution.

Create a terraform.tfvars file in the working directory with the following example content:

Usage:

  1. Save the above content as a terraform.tfvars file in the working directory (this filename allows users to automatically import the content of this tfvars file when executing terraform commands. For other naming, you need to add .auto before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values according to actual needs, especially:

    • region_name needs to be set to the region where resources are located

    • obs_bucket_name needs to be set to OBS bucket name, which needs to be globally unique

    • file_format can be set to exported file format, supporting formats such as vhd, zvhd, vmdk, raw, qcow2, zvhd2, vdi, default value is "zvhd2"

    • image_type can be set to image type filter condition, optional parameter

    • image_os can be set to operating system type filter condition, optional parameter

    • image_name_regex can be set to image name regular expression filter condition, optional parameter

    • obs_bucket_tags can be set to OBS bucket tags, optional parameter

  3. When executing terraform plan or terraform apply, Terraform will automatically read the variable values in this file

In addition to using the terraform.tfvars file, you can also set variable values in the following ways:

  1. Command line parameters: terraform apply -var="obs_bucket_name=my-bucket" -var="file_format=zvhd2"

  2. Environment variables: export TF_VAR_obs_bucket_name=my-bucket and export TF_VAR_file_format=zvhd2

  3. Custom named variable file: terraform apply -var-file="custom.tfvars"

Note: If the same variable is set through multiple methods, Terraform will use variable values according to the following priority: command line parameters > variable file > environment variables > default values. Image export may take a long time, please be patient. The exported image files will be stored in the specified OBS bucket and can be downloaded and used through OBS console or API.

6. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create export images to OBS:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming that the resource plan is correct, run terraform apply to start creating OBS bucket and exporting images

  4. Run terraform show to view the details of the created export images to OBS

Note: Image export may take a long time, please be patient. The exported image files will be stored in the specified OBS bucket and can be downloaded and used through OBS console or API. Only images with status "active" can be exported. Please ensure the image status is correct.

Reference Information

Last updated