Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Last active March 11, 2021 20:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tuxfight3r/35f61371ab74f63cbf34667d0add0cbc to your computer and use it in GitHub Desktop.
Save tuxfight3r/35f61371ab74f63cbf34667d0add0cbc to your computer and use it in GitHub Desktop.
Importing a VM into AWS EC2 from S3 Bucket

NOTE: Assuming .vhd image is already uploaded to s3 bucket, the following is the process to import the image when the vmimport role is missing

#create a role policy json file

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Sid":"",
         "Effect":"Allow",
         "Principal":{
            "Service":"vmie.amazonaws.com"
         },
         "Action":"sts:AssumeRole",
         "Condition":{
            "StringEquals":{
               "sts:ExternalId":"vmimport"
            }
         }
      }
   ]
}

#create the role from cli using the above policy file

aws iam create-role --role-name vmimport --assume-role-policy-document file://trust-policy.json

OUTPUT:

{
    "Role": {
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17", 
            "Statement": [
                {
                    "Action": "sts:AssumeRole", 
                    "Principal": {
                        "Service": "vmie.amazonaws.com"
                    }, 
                    "Effect": "Allow", 
                    "Condition": {
                        "StringEquals": {
                            "sts:ExternalId": "vmimport"
                        }
                    }, 
                    "Sid": ""
                }
            ]
        }, 
        "RoleId": "AROAJKAOLYHOM4KSIHTFO", 
        "CreateDate": "2018-01-26T16:51:58.194Z", 
        "RoleName": "vmimport", 
        "Path": "/", 
        "Arn": "arn:aws:iam::585959602392:role/vmimport"
    }
}

#update the bucket name to the bucket name you have created

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "s3:ListBucket",
            "s3:GetBucketLocation"
         ],
         "Resource":[
            "arn:aws:s3:::<disk-image-file-bucket>"
         ]
      },
      {
         "Effect":"Allow",
         "Action":[
            "s3:GetObject"
         ],
         "Resource":[
            "arn:aws:s3:::<disk-image-file-bucket>/*"
         ]
      },
      {
         "Effect":"Allow",
         "Action":[
            "ec2:ModifySnapshotAttribute",
            "ec2:CopySnapshot",
            "ec2:RegisterImage",
            "ec2:Describe*"
         ],
         "Resource":"*"
      }
   ]
}

#Assign the policy to the role created in the beginning

 aws iam put-role-policy --role-name vmimport --policy-name vmimport --policy-document file://role-policy.json
 

#now import the image into ec2 from our bucket mgmt-thirdparty-images

aws ec2 import-image --disk-containers file://containers3.json --region=eu-west-1

OUTPUT:

[-255-(mohan@xpsbox) ~/aws ]$ aws ec2 import-image --disk-containers file://containers3.json --region=eu-west-1
{
    "Status": "active", 
    "Progress": "2", 
    "SnapshotDetails": [
        {
            "UserBucket": {
                "S3Bucket": "mgmt-thirdparty-images", 
                "S3Key": "cfme-ec2-5.8.2.3-3.x86_64.vhd"
            }, 
            "DiskImageSize": 0.0, 
            "Format": "VHD"
        }
    ], 
    "StatusMessage": "pending", 
    "ImportTaskId": "import-ami-fgbhgzvt"
}
[-127-(mohan@xpsbox) ~/aws ]$ aws ec2 describe-import-image-tasks --region eu-west-1  --import-task-ids import-ami-fgbhgzvt
{
    "ImportImageTasks": [
        {
            "Status": "active", 
            "SnapshotDetails": [
                {
                    "UserBucket": {
                        "S3Bucket": "mgmt-thirdparty-images", 
                        "S3Key": "cfme-ec2-5.8.2.3-3.x86_64.vhd"
                    }, 
                    "DiskImageSize": 0.0, 
                    "Format": "VHD"
                }
            ], 
            "Progress": "4", 
            "StatusMessage": "validating", 
            "ImportTaskId": "import-ami-fgbhgzvt"
        }
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment