Skip to content

azurermImage

Manages a custom virtual machine image that can be used to create virtual machines.

Example Usage Creating from VHD

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as azurerm from "./.gen/providers/azurerm";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: azurerm.
For a more precise conversion please use the --provider flag in convert.*/
const azurermResourceGroupExample = new azurerm.resourceGroup.ResourceGroup(
  this,
  "example",
  {
    location: "West Europe",
    name: "example-resources",
  }
);
const azurermNetworkInterfaceExample =
  new azurerm.networkInterface.NetworkInterface(this, "example_1", {
    ip_configuration: [
      {
        name: "testconfiguration1",
        private_ip_address_allocation: "Static",
      },
    ],
    location: azurermResourceGroupExample.location,
    name: "test-nic",
    resource_group_name: azurermResourceGroupExample.name,
  });
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermNetworkInterfaceExample.overrideLogicalId("example");
const azurermVirtualMachineExample = new azurerm.virtualMachine.VirtualMachine(
  this,
  "example_2",
  {
    location: azurermResourceGroupExample.location,
    name: "acctestvm",
    network_interface_ids: [azurermNetworkInterfaceExample.id],
    resource_group_name: azurermResourceGroupExample.name,
    storage_os_disk: [
      {
        create_option: "FromImage",
        name: "myosdisk1",
      },
    ],
    vm_size: "Standard_D1_v2",
  }
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermVirtualMachineExample.overrideLogicalId("example");
const azurermImageExample = new azurerm.image.Image(this, "example_3", {
  location: azurermResourceGroupExample.location,
  name: "acctest",
  os_disk: [
    {
      blob_uri: `\${${azurermVirtualMachineExample.storageOsDisk.fqn}[0].vhd_uri}`,
      os_state: "Generalized",
      os_type: "Linux",
      size_gb: 30,
    },
  ],
  resource_group_name: azurermResourceGroupExample.name,
});
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermImageExample.overrideLogicalId("example");

Example Usage Creating from Virtual Machine (VM must be generalized beforehand)

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as azurerm from "./.gen/providers/azurerm";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: azurerm.
For a more precise conversion please use the --provider flag in convert.*/
const azurermResourceGroupExample = new azurerm.resourceGroup.ResourceGroup(
  this,
  "example",
  {
    location: "West Europe",
    name: "example-resources",
  }
);
const azurermNetworkInterfaceExample =
  new azurerm.networkInterface.NetworkInterface(this, "example_1", {
    ip_configuration: [
      {
        name: "testconfiguration1",
        private_ip_address_allocation: "Static",
      },
    ],
    location: azurermResourceGroupExample.location,
    name: "test-nic",
    resource_group_name: azurermResourceGroupExample.name,
  });
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermNetworkInterfaceExample.overrideLogicalId("example");
const azurermVirtualMachineExample = new azurerm.virtualMachine.VirtualMachine(
  this,
  "example_2",
  {
    location: azurermResourceGroupExample.location,
    name: "acctestvm",
    network_interface_ids: [azurermNetworkInterfaceExample.id],
    resource_group_name: azurermResourceGroupExample.name,
    storage_os_disk: [
      {
        create_option: "FromImage",
        name: "myosdisk1",
      },
    ],
    vm_size: "Standard_D1_v2",
  }
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermVirtualMachineExample.overrideLogicalId("example");
const azurermImageExample = new azurerm.image.Image(this, "example_3", {
  location: azurermResourceGroupExample.location,
  name: "acctest",
  resource_group_name: azurermResourceGroupExample.name,
  source_virtual_machine_id: azurermVirtualMachineExample.id,
});
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermImageExample.overrideLogicalId("example");

Argument Reference

The following arguments are supported:

  • name - (Required) Specifies the name of the image. Changing this forces a new resource to be created.
  • resourceGroupName - (Required) The name of the resource group in which to create. Changing this forces a new resource to be created. the image. Changing this forces a new resource to be created.
  • location - (Required) Specified the supported Azure location where the resource exists. Changing this forces a new resource to be created. Changing this forces a new resource to be created.
  • sourceVirtualMachineId - (Optional) The Virtual Machine ID from which to create the image.
  • osDisk - (Optional) One or more osDisk elements as defined below. Changing this forces a new resource to be created.
  • dataDisk - (Optional) One or more dataDisk elements as defined below.
  • tags - (Optional) A mapping of tags to assign to the resource.
  • zoneResilient - (Optional) Is zone resiliency enabled? Defaults to false. Changing this forces a new resource to be created.
  • hyperVGeneration - (Optional) The HyperVGenerationType of the VirtualMachine created from the image as v1, v2. The default is v1. Changing this forces a new resource to be created.

\~> Note: zoneResilient can only be set to true if the image is stored in a region that supports availability zones.


The osDisk block supports the following:

  • osType - (Optional) Specifies the type of operating system contained in the virtual machine image. Possible values are: windows or linux.
  • osState - (Optional) Specifies the state of the operating system contained in the blob. Currently, the only value is Generalized. Possible values are generalized and specialized.
  • managedDiskId - (Optional) Specifies the ID of the managed disk resource that you want to use to create the image.
  • blobUri - (Optional) Specifies the URI in Azure storage of the blob that you want to use to create the image. Changing this forces a new resource to be created.
  • caching - (Optional) Specifies the caching mode as readWrite, readOnly, or none. The default is none.
  • sizeGb - (Optional) Specifies the size of the image to be created. The target size can't be smaller than the source size.

The dataDisk block supports the following:

  • lun - (Optional) Specifies the logical unit number of the data disk.
  • managedDiskId - (Optional) Specifies the ID of the managed disk resource that you want to use to create the image. Changing this forces a new resource to be created.
  • blobUri - (Optional) Specifies the URI in Azure storage of the blob that you want to use to create the image.
  • caching - (Optional) Specifies the caching mode as readWrite, readOnly, or none. The default is none.
  • sizeGb - (Optional) Specifies the size of the image to be created. The target size can't be smaller than the source size.

Attributes Reference

The following attributes are exported:

  • id - The ID of the Image.

Timeouts

The timeouts block allows you to specify timeouts for certain actions:

  • create - (Defaults to 90 minutes) Used when creating the Image.
  • update - (Defaults to 90 minutes) Used when updating the Image.
  • read - (Defaults to 5 minutes) Used when retrieving the Image.
  • delete - (Defaults to 90 minutes) Used when deleting the Image.

Import

Images can be imported using the resourceId, e.g.

terraform import azurerm_image.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/images/image1