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.
Related Resources/Data Sources
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_exportNote: 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:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows users to automatically import the content of thistfvarsfile when executing terraform commands. For other naming, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs, especially:
region_nameneeds to be set to the region where resources are locatedobs_bucket_nameneeds to be set to OBS bucket name, which needs to be globally uniquefile_formatcan be set to exported file format, supporting formats such as vhd, zvhd, vmdk, raw, qcow2, zvhd2, vdi, default value is "zvhd2"image_typecan be set to image type filter condition, optional parameterimage_oscan be set to operating system type filter condition, optional parameterimage_name_regexcan be set to image name regular expression filter condition, optional parameterobs_bucket_tagscan be set to OBS bucket tags, optional parameter
When executing
terraform planorterraform 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:
Command line parameters:
terraform apply -var="obs_bucket_name=my-bucket" -var="file_format=zvhd2"Environment variables:
export TF_VAR_obs_bucket_name=my-bucketandexport TF_VAR_file_format=zvhd2Custom 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:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming that the resource plan is correct, run
terraform applyto start creating OBS bucket and exporting imagesRun
terraform showto 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