This shows how to import VM to AWS using AWS EC2 Import and Export Feature using PowerShell. The method is especially useful when you have complicated configurations on OS that take lots of time to prepare the same on a new EC2 instance.
Table of Contents
Select a Candidate Disk Image
I selected a VHD format disk image from Windows Hyper-V (ex. D:\WDisk1.vhd)
Upload Disk Image to AWS S3 bucket
If your PC does not have AWSPowerShell.NetCore module installed, install the module and create a profile if not done.
Install-Module -Name AWSPowerShell.NetCore
The write-S3Object command is used to upload the on-premise disk image to S3 Bucket.
Import-Module AWSPowerShell.NetCore $Profile = "AWSRunner" $Bucket = "vmimporttest.0913" $localpath = "D:\WDisk1.vhd" $S3targetpath = "WDisk1.vhd" Write-S3Object -BucketName $Bucket -File $localpath -Key $S3targetpath -ProfileName $Profile
Run:
Create a service role with a policy
VM Import requires an IAM service role “vmimport” with a trust policy.
import-module AWSPowerShell.NetCore Set-DefaultAWSRegion us-east-1 $profile ="AWSRunner" $trustpolicy = '{ "Version":"2012-10-17", "Statement":[ { "Sid":"", "Effect":"Allow", "Principal":{ "Service":"vmie.amazonaws.com" }, "Action":"sts:AssumeRole", "Condition":{ "StringEquals":{ "sts:ExternalId":"vmimport" } } } ] }' New-IAMRole -RoleName vmimport -AssumeRolePolicyDocument $trustpolicy -ProfileName $profile
Import EC2 Image
The disk image is specified as $container and cmdlet Import-EC2Image is to run the import task.
Region and profile are used as usual. Please check another article to know how to set a profile in AWS.
import-module AWSPowerShell.NetCore Set-DefaultAWSRegion us-east-1 $profile ="AWSRunner" $container = New-Object Amazon.EC2.Model.ImageDiskContainer $container.Format="vhd" $container.UserBucket = New-Object Amazon.EC2.Model.UserBucket $container.UserBucket.S3Bucket = "vmimporttest.0913" $container.UserBucket.S3Key = "wdisk2.vhd" $params = @{ "ClientToken"="idempotencyToken" "Description"="Windows 2019 Image Import" "Platform"="Windows" "LicenseType"="AWS" } Import-EC2Image -DiskContainer $container -ProfileName $profile @params
Run: