[AWS] VMファイルへのエクスポート
1. AMIの作成
エクスポートするEC2のインスタンスから「Amazon Elastic Block Store (Amazon EBS) backed AMI」を作成します。
# aws ec2 create-image --instance-id <インスタンスID> --name "<インスタンス名>"
【例】
# aws ec2 create-image --instance-id i-0123456789abcdefg --name "ec2-example.jp"
2. S3バケットの作成
(1) S3バケットの作成
エクスポートする AMI と同じ AWS リージョンに新しい S3 バケットを作成します。
【例】
# aws s3 mb s3://ec2-example-jp-export-bucket
正常に作成された場合には以下のように表示されます。
make_bucket: ec2-example-jp-export-bucket
確認のため、S3バケットの一覧表示をします。
(2) VM Import/Export用サービスロールの作成
VM Import/Exportでは、アカウント内で特定の操作を実行するにはロールが必要です。
ここでは「vmimport」という名前のロールを作成し、そのロールにVM Import/Exportのアクセス権限を付与します。
「trust-policy.json」という名前のファイルを作成し、以下の内容を記述します。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "vmie.amazonaws.com" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals":{
"sts:ExternalId": "vmimport"
}
}
}
]
}
以下のコマンドを実行します。
# aws iam create-role --role-name vmimport --assume-role-policy-document file://trust-policy.json
実行結果が表示されます。
{
"Role": {
"Path": "/",
"RoleName": "vmimport",
"RoleId": "AROAU5WMBNOYBK7WNFY4I",
"Arn": "arn:aws:iam::338656586672:role/vmimport",
"CreateDate": "2021-04-13T06:27:13+00:00",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "vmie.amazonaws.com"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:Externalid": "vmimport"
}
}
}
]
}
}
}
(3) ポリシーのアタッチ
「role-policy.json」という名前のファイルを作成します。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
"s3:GetBucketAcl"
],
"Resource": [
"arn:aws:s3:::<S3バケット名>",
"arn:aws:s3:::<S3バケット名>/*"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:ModifySnapshotAttribute",
"ec2:CopySnapshot",
"ec2:RegisterImage",
"ec2:Describe*"
],
"Resource": "*"
}
]
}
※<S3バケット名>に作成したS3バケット名を指定して下さい。
「vmimport」ロールに「role-policy.json」ポリシーをアタッチします。
# aws iam put-role-policy --role-name vmimport --policy-name vmimport --policy-document "file://role-policy.json"
3. エクスポートタスク
(1) AMIイメージのエクスポート
AMIイメージをS3バケットにエクスポートします。
aws ec2 export-image --image-id <AMIイメージID> --disk-image-format VMDK --s3-export-location S3Bucket=<S3バケット名>,S3Prefix=exports/
【例】
# aws ec2 export-image --image-id ami-0123456789abcdefg --disk-image-format VMDK --s3-export-location S3Bucket=ec2-example-jp-export-bucket,S3Prefix=exports/
実行結果を表示します。
{
"DiskImageFormat": "VMDK",
"ExportImageTaskId": "export-ami-xxxxxxxxxxxxxx",
"ImageId": "ami-0123456789abcdefg",
"Progress": "0",
"S3ExportLocation": {
"S3Bucket": "ec2-example-jp-export-bucket",
"S3Prefix": "exports/"
},
"Status": "active",
"StatusMessage": "validating"
}
(2) エクスポートタスクの進行状況確認
エクスポートタスクの進行状況を確認します。
# aws ec2 describe-export-image-tasks --export-image-task-id <ExportImageTaskId>
【例】
# aws ec2 describe-export-image-tasks --export-image-task-id export-ami-xxxxxxxxxxxxxx
※「--export-image-task-id」に前述の「ExportImageTaskId」の値を代入します。
■実行中
{
"ExportImageTasks": [
{
"ExportImageTaskId": "export-ami-xxxxxxxxxxxxxx",
"Progress": "85",
"S3ExportLocation": {
"S3Bucket": "ec2-example-jp-export-bucket",
"S3Prefix": "exports/"
},
"Status": "active",
"StatusMessage": "converting",
"Tags": []
}
]
}
■処理完了
「Status」が「completed」になったら処理完了です。
{
"ExportImageTasks": [
{
"ExportImageTaskId": "export-ami-xxxxxxxxxxxxxx",
"S3ExportLocation": {
"S3Bucket": "ec2-example-jp-export-bucket",
"S3Prefix": "exports/"
},
"Status": "completed",
"Tags": []
}
]
}
4. VMファイルのダウンロード
エクスポートしたVMファイルをダウンロードします。
# aws s3 cp s3://<S3バケット名>/exports/<ExportImageTaskId>.vmdk ./
【例】
# aws s3 cp s3://ec2-example-jp-export-bucket/exports/export-ami-xxxxxxxxxxxxxx.vmdk ./
5. エクスポート用バケットの削除
不要となったエクスポート用バケットを削除します。
ここで指定している名称はサンプルなので、実際の名称に当てはめて下さい。
(1) AMIイメージファイルを削除
エクスポートディレクトリを確認します。
# aws s3 ls s3://<S3バケット名>/exports/
【例】
# aws s3 ls s3://ec2-example-jp-export-bucket/exports/
AMIイメージファイルを削除します。
# aws s3 rm s3://<S3バケット名>/exports/<ExportImageTaskId>.vmdk
【例】
# aws s3 rm s3://ec2-example-jp-export-bucket/exports/export-ami-xxxxxxxxxxxxxx.vmdk
(2) エクスポート用ディレクトリの削除
# aws s3 rm s3://<S3バケット名>/exports/
# aws s3 ls s3://<S3バケット名>/
【例】
# aws s3 rm s3://ec2-example-jp-export-bucket/exports/
# aws s3 ls s3://ec2-example-jp-export-bucket/
「vmimportexport_write_verification」ファイルも削除します。
# aws s3 rm s3://<S3バケット名>/vmimportexport_write_verification
【例】
# aws s3 rm s3://ec2-example-jp-export-bucket/vmimportexport_write_verification
(3) エクスポート用バケットの削除
# aws s3 rb s3://<S3バケット名>/
# aws s3 ls
【例】
# aws s3 rb s3://ec2-example-jp-export-bucket/
# aws s3 ls
6. その他後処理
(1) AMIの登録の解除
VMエクスポートに使用したAMIの登録を解除します。
# aws ec2 deregister-image --image-id <AMI ID>
【例】
# aws ec2 deregister-image --image-id ami-0123456789abcdefg
最終更新:2021年04月26日 08:51