All Products
Search
Document Center

Alibaba Cloud CLI:Use Alibaba Cloud CLI to migrate ECS instances across regions

Last Updated:Oct 21, 2024

Elastic Compute Service (ECS) instances are continuously iterated. During the iteration, issues may occur on earlier ECS instances, such as failures in provisioning new resources. This may affect the O&M of your business in the cloud. In this case, you can use the custom image feature to migrate ECS instances. This helps ensure the O&M efficiency of your business in the cloud. This topic describes how to use Alibaba Cloud CLI to create and copy a custom image and use the custom image to migrate an ECS instance across regions.

Overview

You can use Alibaba Cloud CLI to migrate an ECS instance across regions by using a custom image. To do so, perform the following steps:

  1. Create an image: Create a custom image from the source ECS instance. The new instance created by using the custom image contains custom parameters that you have configured. This way, you do not need to customize the destination instance again.

  2. Copy the image: After you copy the image to a region, a new image with a different ID is generated in the destination region. The new image uses settings such as the tag, resource group, and encryption attribute that you specify when you copy the source image.

  3. Create an instance: Use the custom image to create a destination ECS instance in the destination region.

  4. Check the instance: Check the data on the destination instance to ensure that your business can run as expected on the destination instance after migration.

  5. Release resources: After the migration is complete, you can release or delete the resources related to the source ECS instance based on your business requirements. This prevents unnecessary costs.

    image

Usage notes

Before you migrate instance data, take note of the following points:

  • When you create a custom image, the system automatically creates a snapshot for each disk of the ECS instance. You are charged for the snapshots. For information about the billing of snapshots, see Snapshots.

  • Snapshots cannot be created for local disks. Therefore, the migration method described in this topic does not apply to instances that use local disks.

  • The source ECS instance can reside in the classic network or a virtual private cloud (VPC).

  • The destination ECS instance can be created only in a VPC.

  • When you create the destination ECS instance, you can select only an instance type that is available in the selected zone.

    Notes

    If you want to migrate instance data across zones or regions, we recommend that you plan resources in the destination zone or region in advance.

  • After you use the custom image feature to migrate instance data, disk data on the destination ECS instance is the same as that on the source ECS instance. However, new metadata is generated for the destination ECS instance, which is different from that of the source ECS instance. For more information about instance metadata, see Overview of ECS instance metadata.

    Before you migrate instance data, we recommend that you manually sort out resource associations so that you can update resource associations accordingly at the earliest opportunity after migration. Examples:

    • If instances use private IP addresses to communicate within a cluster, you must replace the private IP address of the destination instance with the new private IP address after the migration.

    • The licenses of some applications are bound to the MAC addresses of instances. After you migrate data from one instance to another, the licenses bound to the MAC address of the source instance become invalid. You must rebind the licenses to the MAC address of the destination instance.

Step 1: Create a custom image from the source ECS instance

Before you create a custom image from the source ECS instance, make sure that you understand the usage notes of creating a custom image. For more information, see Create a custom image from an instance.

  1. Run the following command to create a custom image from the source ECS instance by calling the CreateImage operation:

    aliyun ecs CreateImage \
      --RegionId 'cn-hangzhou' \
      --ImageName Created_from_hangzhouECS \
      --InstanceId 'i-bp1g6zv0ce8oghu7****' \
      --ResourceGroupId 'rg-bp67acfmxazb4p****'
  2. Check the response. Sample response:

    {
      "ImageId": "m-bp146shijn7hujku****",
      "RequestId": "C8B26B44-0189-443E-9816-*******"
    }

Step 2: Copy the custom image to a different region

If you want to migrate data from the source ECS instance to a destination ECS instance in a different region, you must first use the image copy feature to copy the custom image to the destination region.

  1. Run the following command to copy the custom image from the China (Hangzhou) region to the China (Beijing) region by calling the CopyImage operation:

    aliyun ecs CopyImage \
      --RegionId 'cn-hangzhou' \
      --DestinationImageName Copy_from_hangzhouImage \
      --ImageId 'm-bp1h46wfpjsjastc****' \
      --DestinationRegionId 'cn-beijing' \
      --ResourceGroupId 'rg-bp67acfmxazb4p****'
  2. Check the response. Sample response:

    {
      "ImageId": "m-bp1h46wfpjsjastd****",
      "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E"
    }

Step 3: Create a destination ECS instance from the custom image

  1. Run the following command to create a destination ECS instance from the custom image by calling the RunInstances operation:

    Note
    • In this example, the PasswordInherit parameter is set to true. The password preset in the image is used to create the destination instance. Then, you can use the same password that you use on the source ECS instance to log on to the destination ECS instance.

    • You can specify the instance parameters based on your business requirements. For more information about the instance parameters, see Create an instance on the Custom Launch tab.

    aliyun ecs RunInstances \
      --region cn-beijing \
      --RegionId 'cn-beijing' \
      --SecurityGroupId 'sg-2zea9dbddva****' \
      --VSwitchId 'vsw-2zep7vc25mjc1****' \
      --ImageId 'm-bp1h46wfpjsjastd****' \
      --InstanceType 'ecs.e-c1m1.large' \
      --InstanceName Copy_from_hangzhouECS \
      --PasswordInherit true \
      --InternetChargeType PayByTraffic \
      --SystemDisk.Size 40 \
      --SystemDisk.Category cloud_essd \
      --InstanceChargeType PostPaid \
      --InternetMaxBandwidthOut 10
  2. Check the response. Sample response:

    {
      "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3****",
      "InstanceIdSets": {
        "InstanceIdSet": [
          "i-bp67acfmxazb4pd2****"
        ]
      }
    }

Step 4: Check data on the destination ECS instance

You must check the data on the destination instance to ensure that your business can run as expected on the destination instance after migration. Examples:

  • Check disk data: Remotely connect to the destination ECS instance and check whether the system disk data is the same as that of the source ECS instance. For example, check whether the files and the directory structures remain unchanged. If the source ECS instance has a data disk and a new data disk is attached to the destination ECS instance, check whether the data on the attached disk is the same as that on the source data disk.

  • Run applications or services: If a specific application or service runs on your source ECS instance, run the application or service on the destination ECS instance. Then, check whether the application or service can provide features and support data operations in the same manner as those on the source ECS instance.

  • Compare resource changes:

    • Run the following command to query the information about the destination ECS instance by calling the DescribeInstances operation. Then, check the resource changes, such as the image information and network configurations, between the source ECS instance and the destination ECS instance.

      aliyun ecs DescribeInstances --RegionId 'cn-beijing' --InstanceIds '["i-bp67acfmxazb4pd2****"]'
  • Update resource associations: New metadata is generated for the destination ECS instance, which is different from that of the source ECS instance. You must update resource associations at the earliest opportunity after data migration. For more information, see Obtain instance metadata.

Step 5: Release or delete the source ECS instance and its resources

After you verify that the data on the destination ECS instance is the same as that on the source ECS instance, the business associations are updated, and your business runs as expected on the destination ECS instance, you can release or delete the source ECS instance and its resources to prevent unnecessary costs. Related operations:

Warning

The operations of releasing an instance, deleting an image, and deleting snapshots are irreversible. Once the operations are complete, the data in the resources cannot be recovered. Make sure that all business data is migrated before you release or delete resources.

  • Run the following command to release the source instance by calling the DeleteInstance operation. For more information, see Release an instance.

    aliyun ecs DeleteInstance --InstanceId i-bp67acfmxazb4pd2****
  • Run the following command to delete the custom image by calling the DeleteImage operation. For more information, see Delete a custom image.

    Important

    After you delete the custom image, you cannot initialize the system disk of the ECS instance that is created from the custom image. If your custom image is free of charge and is required in subsequent operations, we recommend that you do not delete it. For more information about the billing rules of images, see Images.

    aliyun ecs DeleteImage --RegionId 'cn-hangzhou' --ImageId 'm-bp146shijn7hujku****'
  • Run the following command to delete related snapshots by calling the DeleteSnapshot operation. For more information, see Delete a snapshot.

    aliyun ecs DeleteSnapshot --SnapshotId 's-bp1c0doj0taqyzzl****'

References

You can also migrate data between ECS instances in the same region or across regions in the ECS console. For more information, see Use a custom image to replicate ECS instances across regions.