Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an AMI by importing virtual machine images from any virtualization environment #5998

Open
mchouque opened this issue Sep 26, 2018 · 9 comments
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service.

Comments

@mchouque
Copy link

Hello,

With EC2 on AWS, you can use AMIs provided by Amazon or you can "bring your own OS".

Namely you can (it's documented here Importing a VM as an Image Using VM Import/Export:)

  • upload a copy of an existing OS disk image you have to a S3 bucket with "aws s3 cp myimage" (can be a vhd, vmdk or ova file)
  • trigger a conversion to an AMI using "aws ec2 import-image"

When it's done, you have an AMI that was created with the disk image you provided: your own OS tailored to your needs.

In a nutshell, with google, the code to do that is as follow (don't mind the crappy variable names and code, I'm just starting using terraform):

resource "google_storage_bucket_object" "linux-image" {
  name   = "${var.file_name}"
  source = "${var.image_path}/${var.file_name}"
  bucket = "${google_storage_bucket.linux-bucket.name}"
}

resource "google_compute_image" "linux-image" {
  name        = "${var.image_name}"
  family      = "${var.image_family}"
  description = "${var.image_description}"

  raw_disk {
    source = "https://storage.googleapis.com/${var.bucket_name}/${google_storage_bucket_object.linux-image.name}"
  }
}

Namely two resources: one to upload a file to a bucket, the other to "transform" it.

With Azure, it's very similar: 2 resources. One to upload the image, and one to convert it.

resource "azurerm_storage_blob" "linux-image" {
  name                   = "${var.image_name}"
  resource_group_name    = "${var.resource_group}"
  storage_account_name   = "${azurerm_storage_account.linux-bucket.name}"
  storage_container_name = "${azurerm_storage_container.linux-container.name}"
  type                   = "page"
  source                 = "${var.image_path}/${var.image_name}"
}

resource "azurerm_image" "linux-image" {
  name                = "${var.image_name_short}"
  resource_group_name = "${var.resource_group}"
  location            = "${var.bucket_location}"

  os_disk {
    os_type  = "Linux"
    blob_uri = "${azurerm_storage_blob.linux-image.url}"
  }
}

With Amazon, we have the first resource, it's "aws_s3_bucket_object".

The only thing missing in terraform for AWS is the equivalent of the second resource, azurerm_image for Azure or google_compute_image for Google.

On EC2, the conversion is triggered by "aws ec2 import-image" and I believe the API call for that is ImportImage.

Let me know if you have any questions.

Regards,
Mathieu

@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service. labels Sep 26, 2018
@ghost
Copy link

ghost commented Jul 1, 2020

Is there any way of doing this yet with Terraform in AWS?

@zzzuzik
Copy link

zzzuzik commented Jul 24, 2020

same question here, is it possible now?

@km274
Copy link

km274 commented Nov 17, 2021

I would love to see a Terraform resource for this as well!

@alexeiser
Copy link

This is possible now through a combination of aws_ebs_snapshot_import and aws_ami.

@artificial-aidan
Copy link
Contributor

This is possible now through a combination of aws_ebs_snapshot_import and aws_ami.

This is not true for all cases. You need image import, not snapshot import for a number of use cases.

I started #25621 to try and support this.

@alexeiser
Copy link

This is possible now through a combination of aws_ebs_snapshot_import and aws_ami.

This is not true for all cases. You need image import, not snapshot import for a number of use cases.

I started #25621 to try and support this.

Hopefully they will accept your MR - out of curiosity what use case does the import handle that the snapshot import and custom ami creation step not handle?

@artificial-aidan
Copy link
Contributor

When I imported a Windows VM disk, then created an AMI, a Linux AMI was created. aws_ami doesn't allow platform specification.

@trevor-viljoen
Copy link

Is this enhancement still actively being considered? Is there an ETA on when it could be implemented if so? I need to create an EC2 from an OVA appliance that was sent to us by the vendor and the preferred way of doing that would be with Terraform.

@liamraeAL
Copy link

We have this exact use case - importing a number of images from S3 into our image builder pipeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service.
Projects
None yet
Development

No branches or pull requests

8 participants