Deploy Cross Account Migration with Whole 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 whole images across accounts, you can share ECS whole images (including system disks and data disks) from one account to another, achieving cross-account whole machine migration and image sharing. This best practice will introduce how to use Terraform to automatically deploy cross-account migration with whole images, including creating ECS instances, CBR vaults, and whole images in the sharer account, sharing images to the accepter account, accepting shared images in the accepter account, and creating new ECS instances 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. Whole images need to be stored in CBR vaults, so both the sharer account and accepter account need to create CBR vaults. After image sharing, the accepter account needs to accept the sharing before it can use the shared image to create new ECS instances.
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
Add the following script to the TF file (e.g., main.tf) to create ECS instance of the sharer account:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
data_disks: Data disk configuration, assigned by referencing input variable instance_data_disks, supporting dynamic creation of multiple data disks
Other parameter descriptions are the same as regular ECS instance creation
5. Create CBR Vault
Add the following script to the TF file (e.g., main.tf) to create CBR vault:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
name: Vault name, assigned by referencing input variable vault_name
type: Vault type, assigned by referencing input variable vault_type, default value is "server"
consistent_level: Consistency level, assigned by referencing input variable vault_consistent_level, default value is "crash_consistent"
protection_type: Protection type, assigned by referencing input variable vault_protection_type, default value is "backup"
size: Vault capacity (GB), assigned by referencing input variable vault_size, default value is 200
Note: Whole images need to be stored in CBR vaults, so you need to create a CBR vault first. The vault capacity needs to be set reasonably according to the disk size of the ECS instance. It is recommended to reserve sufficient space.
6. Create ECS Whole Image
Add the following script to the TF file (e.g., main.tf) to create a whole image from ECS instance:
Parameter Description:
provider: Specify to use the sharer account provider (huaweicloud.sharer)
name: Whole image name, assigned by referencing input variable whole_image_name
instance_id: ECS instance ID, assigned by referencing the ECS instance resource
vault_id: CBR vault ID, assigned by referencing the CBR vault resource
description: Whole image description, assigned by referencing input variable whole_image_description, optional parameter
Note: Whole image creation requires creating from an existing ECS instance, including system disk and data disks. The whole image will be stored in the specified CBR vault. The creation process may take a long time, please be patient.
7. 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 whole image resource
target_project_ids: Target project ID list, assigned by referencing input variable accepter_project_ids
Note: Image sharing requires specifying the project ID of the accepter account. You can obtain the project ID corresponding to the region by querying project information of the accepter account.
8. 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 whole image resource
vault_id: CBR vault ID, assigned by referencing the accepter account CBR vault resource
depends_on: Explicit dependency relationship, ensuring to accept sharing after image sharing is created
Note: The accepter account needs to create a CBR vault to store the accepted shared image. After accepting the shared image, you can create new ECS instances using the shared image in the accepter account.
9. Create Accepter Account ECS Instance (Optional)
Add the following script to the TF file (e.g., main.tf) to create a new ECS instance using shared image in the accepter account:
Parameter Description:
provider: Specify to use the accepter account provider (huaweicloud.accepter)
image_id: Image ID, assigned by referencing the image share accepter resource, used to create ECS instance using shared image
depends_on: Explicit dependency relationship, ensuring to create ECS instance after accepting shared image
Other parameter descriptions are the same as regular ECS instance creation
Note: The accepter account can create new ECS instances using shared images. When creating ECS instances, specify the image_id parameter to use the shared image. ECS instances created using shared images will include the system disk and data disk contents of the original ECS instance.
10. 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 accountaccepter_project_idsneeds to be set to the project ID list 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
instance_data_diskscan configure data disks for ECS instances, supporting multiple data disks
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. Whole image creation may take a long time, please be patient.
11. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create cross-account migration with whole 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 whole image
Note: Cross-account migration with whole image requires authentication information for two accounts. Please ensure that the provider configuration of both accounts is correct. Whole image creation may take a long time, please be patient. After image sharing, the accepter account needs to accept the sharing before it can use the shared image. ECS instances created using shared images will include the system disk and data disk contents of the original ECS instance.
Reference Information
Last updated