Deploy Cross Account Migration with Data Image
Application Scenario
Image Management Service (IMS) is an image management service provided by Huawei Cloud, supporting image creation, sharing, copying, and other functions. By migrating data images across accounts, you can share data disk images from one account to another, achieving cross-account data migration and image sharing. This best practice will introduce how to use Terraform to automatically deploy cross-account migration with data images, including creating ECS instances, data disks, and data images in the sharer account, sharing images to the accepter account, accepting shared images in the accepter account, and creating data disks using shared images.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Note: This best practice involves two accounts: sharer account and accepter account. You need to configure two providers in Terraform configuration, corresponding to the authentication information of the two accounts respectively. After image sharing, the accepter account needs to accept the sharing before it can use the shared image.
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.
Note: This best practice requires configuring two providers, corresponding to the sharer account and accepter account respectively. In the provider configuration, you need to specify the access_key and secret_key of the two accounts separately.
2. Query Sharer Account Data Sources
Add the following script to the TF file (e.g., main.tf) to query availability zones, ECS flavors, and image information of the sharer account:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
Other parameter descriptions are the same as regular ECS instance creation
3. Create Sharer Account Network Resources
Add the following script to the TF file (e.g., main.tf) to create VPC, subnet, and security group of the sharer account:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
Other parameter descriptions are the same as regular VPC, subnet, and security group creation
4. Create Sharer Account ECS Instance and Data Disk
Add the following script to the TF file (e.g., main.tf) to create ECS instance and data disk of the sharer account:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
server_id: ECS instance ID to which the data disk is attached, assigned by referencing the ECS instance resource
Other parameter descriptions are the same as regular ECS instance and data disk creation
5. Create Data Disk Image
Add the following script to the TF file (e.g., main.tf) to create a data image from the data disk:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
name: Data image name, assigned by referencing input variable data_image_name
volume_id: Data disk ID, assigned by referencing the data disk resource
description: Data image description, assigned by referencing input variable data_image_description, optional parameter
enterprise_project_id: Enterprise project ID, assigned by referencing input variable enterprise_project_id, optional parameter
Note: Data disk image creation requires creating from an existing data disk. Ensure that the data disk has been created and contains the data that needs to be migrated.
6. Share Image to Accepter Account
Add the following script to the TF file (e.g., main.tf) to share the image to the accepter account:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
source_image_id: Source image ID, assigned by referencing the data disk image resource
target_project_ids: Target project ID list, assigned by querying project information of the accepter account
Note: Image sharing requires specifying the project ID of the accepter account. By querying project information of the accepter account, you can obtain the project ID corresponding to the region.
7. Accepter Account Accepts Shared Image
Add the following script to the TF file (e.g., main.tf) to accept the shared image in the accepter account:
Parameter Description:
provider: Specify to use the accepter account provider (huaweicloud.accepter)
image_id: Shared image ID, assigned by referencing the data disk image resource
depends_on: Explicit dependency relationship, ensuring to accept sharing after image sharing is created
Note: The accepter account needs to accept the shared image before it can use it. After accepting the shared image, you can create data disks using the shared image in the accepter account.
8. Create Accepter Account Resources (Optional)
Add the following script to the TF file (e.g., main.tf) to create ECS instance and data disk using shared image in the accepter account:
Parameter Description:
provider: Specify to use the accepter account provider (huaweicloud.accepter)
image_id: Data disk image ID, assigned by referencing the image share accepter resource, used to create data disk using shared image
depends_on: Explicit dependency relationship, ensuring to create ECS instance and data disk after accepting shared image
Other parameter descriptions are the same as regular ECS instance and data disk creation
Note: The accepter account can create data disks using shared images. When creating data disks, specify the image_id parameter to use the shared image. After the data disk is created, it can be attached to ECS instances for use.
9. 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 locatedaccess_keyandsecret_keyneed to be set to the authentication information of the sharer accountaccepter_access_keyandaccepter_secret_keyneed to be set to the authentication information of the accepter accountResource names, network configuration, and other parameters of the sharer account and accepter account need to be set according to actual requirements
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="region_name=cn-north-4" -var="vpc_name=my-vpc"Environment variables:
export TF_VAR_region_name=cn-north-4andexport TF_VAR_vpc_name=my-vpcCustom 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. This best practice requires configuring authentication information for two accounts. Please ensure that the access_key and secret_key of both accounts are correctly configured.
10. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create cross-account migration with data image:
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 resources of the sharer account and accepter accountRun
terraform showto view the details of the created cross-account migration with data image
Note: Cross-account migration with data image requires authentication information for two accounts. Please ensure that the provider configuration of both accounts is correct. After image sharing, the accepter account needs to accept the sharing before it can use the shared image. Data disk image creation requires creating from an existing data disk. Ensure that the data disk has been created and contains the data that needs to be migrated.
Reference Information
Last updated