All Products
Search
Document Center

Alibaba Cloud CLI:Migrasi instance ECS lintas wilayah menggunakan Alibaba Cloud CLI

Last Updated:Apr 05, 2026

Topik ini menjelaskan cara memigrasi instance Elastic Compute Service (ECS) dari satu wilayah ke wilayah lain menggunakan Alibaba Cloud CLI. Prosesnya mencakup pembuatan custom image dari instans sumber, penyalinan gambar tersebut ke wilayah tujuan, serta peluncuran instans baru berdasarkan gambar yang telah disalin.

Cara kerja

Proses migrasi lintas wilayah menggunakan custom image untuk membuat replika disk instance ECS Anda di wilayah baru.

  1. Create a custom image dari instans sumber. Langkah ini menangkap sistem operasi, aplikasi, dan data pada semua disk.

  2. Salin gambar kustom ke Wilayah tujuan.

  3. Create a new instance di wilayah tujuan dari gambar yang telah disalin.

  4. Verify the new instance untuk memastikan semua aplikasi dan layanan berfungsi sebagaimana mestinya.

  5. Release the source resources untuk menghindari biaya yang tidak perlu.

Diagram showing the migration workflow: Create Image - / /> Copy Image -> Create Instance -> Verify -> Release.

Sebelum memulai

Batasan

  • Metode ini tidak mendukung instans yang menggunakan local disk karena snapshot tidak dapat dibuat untuk jenis disk tersebut.

  • Metode ini tidak mendukung pembuatan gambar yang berisi snapshot dari sistem disk dan data disk pada instans dengan SSD lokal. Untuk informasi lebih lanjut, lihat Instance families with local SSDs (i series).

  • Instans sumber dapat berada di jaringan klasik atau VPC, tetapi instans tujuan hanya dapat dibuat di VPC.

  • Tipe instans untuk instans baru harus tersedia di zona tujuan.

Penagihan

  • Anda akan dikenai biaya untuk snapshot yang dibuat sebagai bagian dari custom image. Untuk informasi lebih lanjut, lihat Snapshot billing.

Pertimbangan perencanaan

  • Sebelum memulai, pastikan Anda telah membuat VPC, vSwitch, dan security group di wilayah tujuan.

  • Instans baru akan memiliki metadata yang berbeda (seperti ID instans baru, alamat IP pribadi, dan alamat MAC). Perbarui aplikasi atau skrip apa pun yang bergantung pada metadata ini. Misalnya, Anda mungkin perlu mengikat ulang lisensi perangkat lunak yang terikat pada alamat MAC atau memperbarui alamat IP dalam konfigurasi kluster. Untuk informasi lebih lanjut, lihat Instance metadata.

Prasyarat

  • Instal dan konfigurasikan Alibaba Cloud CLI. Untuk informasi lebih lanjut, lihat Installation guide dan Configure credentials.

  • Pastikan Anda memiliki izin untuk operasi API ECS yang diperlukan (misalnya, CreateImage, CopyImage, RunInstances, dan DeleteInstance).

Langkah 1: Buat custom image dari instans sumber

Panggil operasi CreateImage untuk membuat custom image dari instans sumber Anda. Untuk informasi lebih lanjut, lihat Create a custom image from an instance.

Catatan

Jika instans sumber sedang berjalan, operasi ini akan menghentikannya selama beberapa menit untuk memastikan konsistensi data pada snapshot.

aliyun ecs CreateImage \
  --RegionId 'cn-hangzhou' \
  --ImageName 'source-image-for-migration' \
  --InstanceId 'i-bp1g6zv0ce8oghu7****'

Tanggapan sukses akan mengembalikan ID gambar baru.

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

Simpan ImageId. Anda akan membutuhkannya pada langkah berikutnya.

Langkah 2: Salin gambar ke wilayah tujuan

Sebelum menyalin gambar, Anda harus menunggu hingga statusnya menjadi Available. Anda dapat memeriksa statusnya dengan memanggil operasi DescribeImages.

Panggil operasi CopyImage untuk menyalin gambar ke wilayah tujuan. Contoh berikut menyalin gambar dari Tiongkok (Hangzhou) ke Tiongkok (Beijing).

aliyun ecs CopyImage \
  --RegionId 'cn-hangzhou' \
  --ImageId 'm-bp146shijn7hujku****' \
  --DestinationRegionId 'cn-beijing' \
  --DestinationImageName 'migrated-image-beijing'

Tanggapan akan mengembalikan ImageId baru untuk gambar yang disalin di wilayah tujuan.

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

Simpan ImageId baru ini untuk langkah berikutnya.

Langkah 3: Buat instans tujuan

Panggil operasi RunInstances untuk membuat instans baru dari gambar yang disalin di wilayah tujuan. Untuk informasi lebih lanjut, lihat Custom launch ECS instances.

aliyun ecs RunInstances \
  --RegionId 'cn-beijing' \
  --ImageId 'm-bp1h46wfpjsjastd****' \
  --InstanceType 'ecs.g6.large' \
  --SecurityGroupId 'sg-2zea9dbddva****' \
  --VSwitchId 'vsw-2zep7vc25mjc1****' \
  --InstanceName 'migrated-instance-beijing' \
  --PasswordInherit true \
  --InternetChargeType 'PayByTraffic' \
  --SystemDisk.Size '40' \
  --SystemDisk.Category 'cloud_essd' \
  --InstanceChargeType 'PostPaid' \
  --InternetMaxBandwidthOut '10'

Dalam contoh ini, PasswordInherit diatur ke true, sehingga Anda dapat login ke instans baru menggunakan kata sandi yang sama dengan instans sumber. Untuk daftar lengkap parameter, lihat referensi API.

Tanggapan sukses akan mengembalikan ID instans baru.

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

Langkah 4: Verifikasi instans tujuan

Setelah instans baru berjalan, hubungkan ke instans tersebut dan lakukan pemeriksaan berikut:

  • Konfirmasi bahwa sistem file dan data pada sistem disk serta data disk identik dengan instans sumber.

  • Jalankan aplikasi dan layanan Anda untuk memastikan semuanya berfungsi sepenuhnya.

  • Perbarui asosiasi resource apa pun yang bergantung pada metadata instans, seperti alamat IP atau alamat MAC.

Langkah 5: Lepaskan sumber daya sumber

Setelah Anda memverifikasi bahwa instans baru beroperasi dengan benar, lepaskan sumber daya asli untuk menghindari biaya yang tidak perlu.

Peringatan

Melepas instans serta menghapus gambar dan snapshot merupakan tindakan yang tidak dapat dikembalikan. Pastikan data Anda telah berhasil dimigrasikan dan diverifikasi sebelum melanjutkan.

  1. Melepaskan Instans sumber: Panggil operasi DeleteInstance. Untuk informasi selengkapnya, lihat Melepaskan Instans.

    aliyun ecs DeleteInstance --InstanceId 'i-bp1g6zv0ce8oghu7****' --Force true
  2. Delete the custom images: Panggil operasi DeleteImage untuk kedua gambar, baik di wilayah sumber maupun tujuan. Untuk informasi lebih lanjut, lihat Delete a custom image.

    # Delete the source region image
    aliyun ecs DeleteImage --RegionId 'cn-hangzhou' --ImageId 'm-bp146shijn7hujku****'
    
    # Delete the destination region image
    aliyun ecs DeleteImage --RegionId 'cn-beijing' --ImageId 'm-bp1h46wfpjsjastd****'
  3. Delete the related snapshots: Panggil operasi DeleteSnapshot untuk setiap snapshot yang dibuat selama proses pembuatan gambar.

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

Otomatisasi migrasi dengan skrip

Anda dapat menggunakan skrip command-line untuk mengotomatisasi proses migrasi. Contoh berikut menyediakan kerangka dasar yang dapat Anda sesuaikan sesuai kebutuhan.

Contoh skrip:

Bash

Untuk menjalankan skrip ini, Anda harus menginstal tool jq.

#!/usr/bin/env bash
 
# Source instance ID
SRC_INSTANCE_ID="i-bp1g6zv0ce8oghu7****"
# Source instance region ID
SRC_REGION_ID="cn-hangzhou"
# Destination region ID
DST_REGION_ID="cn-beijing"
# Destination instance zone ID
DST_ZONE_ID="cn-beijing-h"
# Source instance type
SRC_INSTANCE_TYPE="ecs.e-c1m1.large"
# Source instance system disk category
SRC_SYSTEM_DISK_CATEGORY="cloud_auto"
# Source instance system disk size
SRC_SYSTEM_DISK_SIZE=40
 
# Output log
function log {
  local level="$1"
  local message="$2"
  echo "$(date +'%Y-%m-%d %H:%M:%S') [$level] $message"
}
 
# Wrap Alibaba Cloud CLI
function invoke_aliyun_command() {
  local service="$1"
  local action="$2"
  shift 2
  local -a params=("$@")
  response=$(aliyun "$service" "$action" "${params[@]}")
  exit_code=$?
  if [ $exit_code -eq 0 ]; then return 0; fi
  log "ERROR" "Failed to invoke aliyun command: aliyun $service $action ${params[*]}"
  exit 1
}
 
# Wait for resource to be available
function wait_resource_available() {
  local service="$1"
  local action="$2"
  local region_id="$3"
  local resource_type="$4"
  local resource_id="$5"
 
  local -a params=(
    "--region" "$region_id"
    "--RegionId" "$region_id"
    "--${resource_type}Id" "$resource_id"
  )
 
  if [ "$resource_type" == "Image" ]; then
    params+=("--Status" "Creating,Waiting,Available")
  fi
 
  local current_status
  local current_progress
 
  local timeout=1200
  local interval=20
  local end_time=$(( $(date +%s) + timeout ))
 
  while (( $(date +%s) < end_time )); do
    invoke_aliyun_command "$service" "$action" "${params[@]}"
    current_status=$(echo "$response" | jq -r '.. | .Status? // empty' | head -n1)
    current_progress=$(echo "$response" | jq -r '.. | .Progress? // empty' | head -n1)
 
    log "INFO" "${resource_type} status: $current_status"
    if [[ -n "$current_progress" ]]; then
        log "INFO" "Creation progress: $current_progress"
    fi
    if [[ "$current_status" == "Available" ]]; then return 0; fi
    sleep "$interval"
  done
}
 
# Create image
log "INFO" "Creating source image from instance '$SRC_INSTANCE_ID'"
src_img_params=(
  "--region" "$SRC_REGION_ID"
  "--RegionId" "$SRC_REGION_ID"
  "--InstanceId" "$SRC_INSTANCE_ID"
  "--ImageName" "cli-src-img"
)
invoke_aliyun_command ecs CreateImage "${src_img_params[@]}"
src_img_id=$(echo "$response" | jq -r .ImageId)
wait_resource_available ecs DescribeImages "$SRC_REGION_ID" "Image" "$src_img_id"
log "INFO" "Source image created: '$src_img_id'"
 
# Copy image to destination region
log "INFO" "Copying image to region: '$DST_REGION_ID'"
dst_img_params=(
  "--region" "$SRC_REGION_ID"
  "--RegionId" "$SRC_REGION_ID"
  "--DestinationRegionId" "$DST_REGION_ID"
  "--ImageId" "$src_img_id"
  "--DestinationImageName" "cli-dst-img"
)
invoke_aliyun_command ecs CopyImage "${dst_img_params[@]}"
dst_img_id=$(echo "$response" | jq -r .ImageId)
wait_resource_available ecs DescribeImages "$DST_REGION_ID" "Image" "$dst_img_id"
log "INFO" "Destination image copied: '$dst_img_id'"
 
# Create VPC
log "INFO" "Creating VPC"
dst_vpc_params=(
  "--region" "$DST_REGION_ID"
  "--RegionId" "$DST_REGION_ID"
  "--CidrBlock" "10.0.0.0/8"
  "--VpcName" "cli-dst-vpc"
)
invoke_aliyun_command ecs CreateVpc "${dst_vpc_params[@]}"
dst_vpc_id=$(echo "$response" | jq -r .VpcId)
wait_resource_available "Vpc" "DescribeVpcAttribute" "$DST_REGION_ID" "Vpc" "$dst_vpc_id"
log "INFO" "VPC created: '$dst_vpc_id'"
 
# Create a vSwitch
log "INFO" "Creating VSwitch"
dst_vsw_params=(
  "--region" "$DST_REGION_ID"
  "--RegionId" "$DST_REGION_ID"
  "--ZoneId" "$DST_ZONE_ID"
  "--VpcId" "$dst_vpc_id"
  "--CidrBlock" "10.1.1.0/24"
)
invoke_aliyun_command vpc CreateVSwitch "${dst_vsw_params[@]}"
dst_vsw_id=$(echo "$response" | jq -r .VSwitchId)
wait_resource_available "vpc" "DescribeVSwitchAttributes" "$DST_REGION_ID" "VSwitch" "$dst_vsw_id"
log "INFO" "VSwitch created: '$dst_vsw_id'"
 
# Create security group
log "INFO" "Creating security group"
dst_sg_params=(
  "--region" "$DST_REGION_ID"
  "--RegionId" "$DST_REGION_ID"
  "--SecurityGroupName" "cli-dst-sg"
  "--VpcId" "$dst_vpc_id"
  "--SecurityGroupType" "normal"
)
invoke_aliyun_command ecs CreateSecurityGroup "${dst_sg_params[@]}"
dst_sg_id=$(echo "$response" | jq -r .SecurityGroupId)
log "INFO" "Security group created: '$dst_sg_id'"
 
# Create a pay-as-you-go ECS instance in the destination region
log "INFO" "Creating ECS instance in zone: '$DST_ZONE_ID'"
dst_instance_params=(
  "--region" "$DST_REGION_ID"
  "--RegionId" "$DST_REGION_ID"
  "--ImageId" "$dst_img_id"
  "--SecurityGroupId" "$dst_sg_id"
  "--VSwitchId" "$dst_vsw_id"
  "--InstanceType" "$SRC_INSTANCE_TYPE"
  "--InstanceName" "cli-dst-ecs"
  "--PasswordInherit" "true"
  "--SystemDisk.Category" "$SRC_SYSTEM_DISK_CATEGORY"
  "--SystemDisk.Size" "$SRC_SYSTEM_DISK_SIZE"
  "--InstanceChargeType" "PostPaid"
)
invoke_aliyun_command ecs RunInstances "${dst_instance_params[@]}"
dst_instance_id=$(echo "$response" | jq -r '.InstanceIdSets.InstanceIdSet[0]')
log "INFO" "ECS instance created: '$dst_instance_id'"

Powershell

# Set error exit policy
$ErrorActionPreference = "Stop"
 
# Set character encoding
chcp 65001 | Out-Null
[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8
 
# Source instance ID
$SRC_INSTANCE_ID = "i-bp1g6zv0ce8oghu7****"
# Source instance region ID
$SRC_REGION_ID = "cn-hangzhou"
# Destination region ID
$DST_REGION_ID = "cn-beijing"
# Destination instance zone ID
$DST_ZONE_ID = "cn-beijing-h"
# Source instance type
$SRC_INSTANCE_TYPE = "ecs.e-c1m1.large"
# Source instance system disk category
$SRC_SYSTEM_DISK_CATEGORY = "cloud_auto"
# Source instance system disk size
$SRC_SYSTEM_DISK_SIZE = 40
 
# Output log
function Log {
    param ([string]$Level, [string]$Message)
    Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [$Level] $Message"
}
 
# Wrap Alibaba Cloud CLI
function Invoke-AliyunCommand {
    param ([string]$Service, [string]$Action, [array]$Params)
    $response = aliyun $Service $Action $Params
    if ($LASTEXITCODE -eq 0) { return $response | ConvertFrom-Json }
    Log "ERROR" "Failed to call aliyun command: aliyun $Service $Action $Params"
    exit 1
}
 
# Get nested property value
function Get-NestedPropertyValue {
    param (
        [object]$Object,
        [string]$PropertyPath
    )
    $parts = $PropertyPath -split '\.'
    $value = $Object
 
    foreach ($part in $parts) {
        $value = $value.$part
    }
    return $value
}
 
# Wait for resource to be available
function Wait-ResourceAvailable {
    param (
        [string]$Service,
        [string]$Action,
        [object]$Params,
        [string]$StatusPath,
        [string]$ProgressPath
    )
    $timeout = 1200
    $interval = 20
    $endTime = (Get-Date).AddSeconds($timeout)
    while ((Get-Date) -lt $endTime) {
        $response = Invoke-AliyunCommand $Service $Action $Params
        $currentStatus = Get-NestedPropertyValue $response $StatusPath
        Log "INFO" "Resource status: $currentStatus"
        if ($ProgressPath) {
            $currentProgress = Get-NestedPropertyValue $response $ProgressPath
            Log "INFO" "Creation progress: $currentProgress"
        }
        if ("Available" -eq $currentStatus) { return }
        Start-Sleep -Seconds $interval
    }
}
 
# Create image
Log "INFO" "Creating source image from instance '$SRC_INSTANCE_ID'"
$srcImgId = (Invoke-AliyunCommand -Service "ecs" -Action "CreateImage" -Params @(
    "--region", $SRC_REGION_ID,
    "--RegionId", $SRC_REGION_ID,
    "--InstanceId", $SRC_INSTANCE_ID,
    "--ImageName", "cli-src-img"
)).ImageId
Wait-ResourceAvailable -Service "ecs" -Action "DescribeImages" -Params @(
    "--region", $SRC_REGION_ID,
    "--RegionId", $SRC_REGION_ID,
    "--ImageId", $srcImgId,
    "--Status", "Creating,Waiting,Available"
) -StatusPath "Images.Image.Status" -ProgressPath "Images.Image.Progress"
Log "INFO" "Source image created: '$srcImgId'"
 
# Copy image to destination region
Log "INFO" "Copying image to region: '$DST_REGION_ID'"
$dstImgId = (Invoke-AliyunCommand -Service "ecs" -Action "CopyImage" -Params @(
    "--region", $SRC_REGION_ID,
    "--RegionId", $SRC_REGION_ID,
    "--DestinationRegionId", $DST_REGION_ID,
    "--ImageId", $srcImgId,
    "--DestinationImageName", "cli-dst-img"
)).ImageId
Wait-ResourceAvailable -Service "ecs" -Action "DescribeImages" -Params @(
    "--region", $DST_REGION_ID,
    "--RegionId", $DST_REGION_ID,
    "--ImageId", $dstImgId,
    "--Status", "Creating,Waiting,Available"
) -StatusPath "Images.Image.Status" -ProgressPath "Images.Image.Progress"
Log "INFO" "Destination image copied: '$dstImgId'"
 
# Create VPC
Log "INFO" "Creating VPC"
$dstVpcId = (Invoke-AliyunCommand -Service "vpc" -Action "CreateVpc" -Params @(
    "--region", $DST_REGION_ID,
    "--RegionId", $DST_REGION_ID,
    "--CidrBlock", "10.0.0.0/8",
    "--VpcName", "cli-dst-vpc"
)).VpcId
Wait-ResourceAvailable -Service "Vpc" -Action "DescribeVpcAttribute" -Params @(
    "--region", $DST_REGION_ID,
    "--RegionId", $DST_REGION_ID,
    "--VpcId", $dstVpcId
) -StatusPath "Status"
Log "INFO" "VPC created: '$dstVpcId'"
 
# Create a vSwitch
Log "INFO" "Creating VSwitch"
$dstVSwitchId = (Invoke-AliyunCommand vpc CreateVSwitch @(
    "--region", $DST_REGION_ID,
    "--RegionId", $DST_REGION_ID,
    "--ZoneId", $DST_ZONE_ID,
    "--VpcId", $dstVpcId,
    "--CidrBlock", "10.1.1.0/24"
)).VSwitchId
Wait-ResourceAvailable -Service "Vpc" -Action "DescribeVSwitchAttributes" -Parameters @(
    "--region", $DST_REGION_ID,
    "--RegionId", $DST_REGION_ID,
    "--VSwitch", $dstVSwitchId
) -StatusPath "Status"
Log "INFO" "VSwitch created: '$dstVSwitchId'"
 
# Create security group
Log "INFO" "Creating security group"
$dstSecurityGroupId = (Invoke-AliyunCommand ecs CreateSecurityGroup @(
    "--region", $DST_REGION_ID,
    "--RegionId", $DST_REGION_ID,
    "--SecurityGroupName", "cli-dst-sg",
    "--VpcId", $dstVpcId,
    "--SecurityGroupType", "normal"
)).SecurityGroupId
Log "INFO" "Security group created: '$dstSecurityGroupId'"
 
# Create a pay-as-you-go ECS instance in the destination region
Log "INFO" "Creating ECS instance in zone: '$DST_ZONE_ID'"
$dstInstanceId = (Invoke-AliyunCommand ecs RunInstances @(
    "--region", $DST_REGION_ID,
    "--RegionId", $DST_REGION_ID,
    "--ImageId", $dstImgId,
    "--SecurityGroupId", $dstSecurityGroupId,
    "--VSwitchId", $dstVSwitchId,
    "--InstanceType", $SRC_INSTANCE_TYPE,
    "--InstanceName", "cli-dst-ecs",
    "--PasswordInherit", "true",
    "--SystemDisk.Category", $SRC_SYSTEM_DISK_CATEGORY,
    "--SystemDisk.Size", $SRC_SYSTEM_DISK_SIZE,
    "--InstanceChargeType", "PostPaid"
)).InstanceIdSets.InstanceIdSet[0]
Log "INFO" "ECS instance created: '$dstInstanceId'"
Log "INFO" "Script execution completed successfully."

Referensi

Cross-region instance replication using a custom image (metode berbasis Konsol)