DEV Community

Rafaf Tahsin
Rafaf Tahsin

Posted on

Migrate an EC2 instance from one AWS account to another

Create an AMI of the instance

aws ec2 create-image \
    --instance-id <i-0123456789abcdef> \
    --name "AMI-Image-Name" \
    --description "AMI Image of EC2 created from Source AWS Account to Destination AWS Account" \
    --region <region-id> \
    --profile <optional-profile>
Enter fullscreen mode Exit fullscreen mode

Above command will output an Image ID. We will share this image with the destination AWS Account. Image creation might take some time. When the image is created run following command to give permission to destination AWS account.

Give AMI Permission to destination AWS Account

aws ec2 modify-image-attribute \
    --image-id <AMI-ID-retrieved-from-above-command> \
    --region <region-id> \
    --launch-permission "Add=[{UserId=<destination-aws-account-id>}]" \
    --profile infolytxdev
Enter fullscreen mode Exit fullscreen mode

Reference:

  1. https://docs.aws.amazon.com/cli/latest/reference/ec2/create-image.html
  2. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/sharingamis-explicit.html

Top comments (0)