高阶15 - 如何导入其他云商/本地的虚拟机镜像

天下大势,分久必合,合久必分。同样云计算市场分分合合,来来去去也正常,有不少朋友以前用的是非AWS的云, 但是因为踩坑或割韭菜等原因,想要迁移到AWS上,在这过程中遇到最多的问题,就是服务器镜像的跨云迁移。 本文就抛砖引玉,利用VM Import服务来导入镜像,生成能在AWS上使用的AMI镜像。 -- D.C

VM Import/Export 支持的映像类型很多,比如:OVA,VHD/VHDX,VMDK,RAW。其实不只是云端镜像,本地机房虚拟机镜像也可。

映像格式

VM Import/Export 支持以下映像格式来导入磁盘和 VMs:

更多条件查询 点我

准备工作

s3://tempimage/abcd

镜像导到S3可以有2种方式:

A - 利用同步工具,直接从某云的对象存储拉到AWS的S3。

B - 以文件的形式下载到本地,再上传到AWS S3。

权限设置

总而言之,就是让用户拥有操作S3,EC2相关动作的权限。

{

    "Version": "2012-10-17",

    "Statement": [

        {

            "Effect": "Allow",

            "Action": [

                "s3:GetBucketLocation",

                "s3:GetObject",

                "s3:ListBucket"

            ],

            "Resource": [

                "arn:aws-cn:s3:::tempimage",

                "arn:aws-cn:s3:::tempimage/*"

            ]

        },

        {

            "Effect": "Allow",

            "Action": [

                "ec2:ModifySnapshotAttribute",

                "ec2:CopySnapshot",

                "ec2:RegisterImage",

                "ec2:Describe*"

            ],

            "Resource": "*"

        }

    ]

}

policy

choosepolicy

{

  "Version": "2012-10-17",

  "Statement": [

    {

      "Effect": "Allow",

      "Principal": {

        "Service": "vmie.amazonaws.com"

      },

      "Action": "sts:AssumeRole",

      "Condition": {

        "StringEquals": {

          "sts:Externalid": "vmimport"

        }

      }

    }

  ]

}

addtrust

运行命令转化镜像

[
  {
    "Description": "My Server1",
    "Format": "VHD",
    "UserBucket": {
        "S3Bucket": "tempimage",
        "S3Key": "abcd1"
    }
}]
$ aws ec2 import-image --description "My server VM" --disk-containers "file://D:\containers.json"
aws ec2 describe-import-image-tasks --import-task-ids import-ami-1234567890abcdef0

Status的状态值有如下几种:

active — The import task is in progress.

deleting — The import task is being canceled.

deleted — The import task is canceled.

updating — Import status is updating.

validating — The imported image is being validated.

validated — The imported image was validated.

converting — The imported image is being converted into an AMI.

completed — The import task is completed and the AMI is ready to use.

如果转化磁盘镜像(snapshot)

$ aws ec2 import-snapshot --description "My server VM" --disk-container "file://C:\containers.json"

containers.json 的格式如下:

{
    "Description": "My server disk",
    "UserBucket": {
        "S3Bucket": "tempimage",
        "S3Key": "abcd_disk"
    }
}
$ aws ec2 describe-import-snapshot-tasks --import-task-ids import-snap-1234567890abcdef0

snapshotstatus

补充材料:

VM Import/Export

还有疑问或不清楚的,可以在群里提问哟~

我只是一个句号。