How to Migrate EC2 Instance From AWS to GCP

Bharath Sampath
Ankercloud Engineering
3 min readJan 23, 2023

Introduction:

Through this article, you will know how to migrate the EC2 instance of AWS to GCP.

  • Prerequisites
  • Export the AMI to S3 bucket
  • Import the Image in GCP
  • Launch Virtual machine from GCP

Requirement

AWS cli:

Install the AWS CLI in your local machine and configure the programmatic access key and secret key in AWS config.

S3 bucket:

Create one s3 bucket to store the exported image.

Create file trust-policy.json using the below content.

{

“Version”: “2012–10–17”,

“Statement”: [

{

“Effect”: “Allow”,

“Principal”: { “Service”: “vmie.amazonaws.com” },

“Action”: “sts:AssumeRole”,

“Condition”: {

“StringEquals”:{

“sts:Externalid”: “vmimport”

}

}

}

]

}

Create Service role:

Create a role named vmimport and grant VM Import/Export access to it, with the below CLI command.

aws iam create-role — role-name vmimport — assume-role-policy-document “file://C:\trust-policy.json”

Create policy:

Create a file named role-policy.json with the following policy, where disk-image-file-bucket is the bucket for disk images and export-bucket is the bucket that’s created in the above step for exported images:

{

“Version”:”2012–10–17",

“Statement”:[

{

“Effect”: “Allow”,

“Action”: [

“s3:GetBucketLocation”,

“s3:GetObject”,

“s3:ListBucket”

],

“Resource”: [

“arn:aws:s3:::disk-image-file-bucket”,

“arn:aws:s3:::disk-image-file-bucket/*”

]

},

{

“Effect”: “Allow”,

“Action”: [

“s3:GetBucketLocation”,

“s3:GetObject”,

“s3:ListBucket”,

“s3:PutObject”,

“s3:GetBucketAcl”

],

“Resource”: [

“arn:aws:s3:::export-bucket”,

“arn:aws:s3:::export-bucket/*”

]

},

{

“Effect”: “Allow”,

“Action”: [

“ec2:ModifySnapshotAttribute”,

“ec2:CopySnapshot”,

“ec2:RegisterImage”,

“ec2:Describe*”

],

“Resource”: “*”

}

]

}

Attach the policy:

The following put-role-policy command can be used to attach the policy to the role created above.

aws iam put-role-policy — role-name vmimport — policy-name vmimport — policy-document “file://C:\import\role-policy.json”

AMI:

Take an AMI of ec2 instances that you want to migrate.

GCloud cli:

Install the gcloud CLI in your local machine and login with your mail id.

Export The Image to S3

VM Import/Export helps you import virtual machine (VM) images from your present virtualization environment to Amazon EC2, and then export them back.

Export:

To export your image, use the export-image command.

aws ec2 export-image — image-id ami-id — disk-image-format VMDK — s3-export-location S3Bucket=my-export-bucket

In the above command give the taken ami-id and bucket name in my-export-bucket.

Monitor the export:

To monitor the export of your image, use the following describe-export-image-tasks command.

aws ec2 describe-export-image-tasks — export-image-task-ids export-ami-1234567890abcdef0

If the status shows active that means the export task is in progress. The image will be ready to use when the status is completed. It’ll take time to complete depending on the machine image size.

Import the image to GCP

STS token:

Generate an sts token from AWS using the AWS CLI command.

aws sts get-session-token — duration-seconds 6600

Provide the value in seconds based on your requirement. Once generated save the access key, secret key, and session token values.

Import the image:

Now, let's import the image from AWS s3 bucket to the GCP image using gcloud command.

gcloud beta compute images import <imagename> — aws-region=<region> — aws-access-key-id=<access key> — aws-secret-access-key=<secreat key> — aws-session-token=<token> — aws-ami-export-location=s3://<bucket name/image vdmk file path> — os=<os version eg: ubuntu-1804>

In the above command fill the appropriate values, in the –os parameter give the value of the image’s os name and version.

Finally, let's launch the instance from GCP console in the image service

  • Go to the GCP console -> Compute Engine -> Images. Here we can see the imported images from AWS.
  • Click the 3 dots action button from the right side of the bootable image and click create the instance.

--

--