From aa2524f2b82883b3dde3306f775806eb69e97bcd Mon Sep 17 00:00:00 2001 From: Ferry Blankendaal Date: Thu, 19 Dec 2024 11:13:32 +0100 Subject: [PATCH] enhancement: disk public access to false, as default (#4) * disk public access to false, as default it now seems that disks are by default public network access enabled, this should be change to false and you should be able to set it to true --------- Co-authored-by: github-actions[bot] --- README.md | 8 +++++++- data.tf | 3 +++ main.linux.tf | 23 +++++++++++++++++++++++ main.windows.tf | 22 ++++++++++++++++++++++ terraform.tf | 4 ++++ variables.tf | 24 +++++++++++++++++++++++- 6 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 data.tf diff --git a/README.md b/README.md index 0836939..fa66682 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ az vm image list --location germanywestcentral --publisher Canonical --output ta | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.8 | +| [azapi](#requirement\_azapi) | >= 2.0, < 3.0 | | [azurerm](#requirement\_azurerm) | >= 4.5, < 5.0 | | [random](#requirement\_random) | >= 3.0 | | [time](#requirement\_time) | >= 0.12 | @@ -63,6 +64,7 @@ az vm image list --location germanywestcentral --publisher Canonical --output ta | Name | Version | |------|---------| +| [azapi](#provider\_azapi) | >= 2.0, < 3.0 | | [azurerm](#provider\_azurerm) | >= 4.5, < 5.0 | | [random](#provider\_random) | >= 3.0 | | [terraform](#provider\_terraform) | n/a | @@ -76,6 +78,8 @@ No modules. | Name | Type | |------|------| +| [azapi_update_resource.linux_os_disk](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/update_resource) | resource | +| [azapi_update_resource.windows_os_disk](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/update_resource) | resource | | [azurerm_dev_test_global_vm_shutdown_schedule.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dev_test_global_vm_shutdown_schedule) | resource | | [azurerm_key_vault_secret.admin_password](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_secret) | resource | | [azurerm_linux_virtual_machine.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) | resource | @@ -96,6 +100,7 @@ No modules. | [random_password.admin_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource | | [terraform_data.password_timestamp](https://registry.terraform.io/providers/hashicorp/terraform/latest/docs/resources/data) | resource | | [time_static.password_timestamp](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/static) | resource | +| [azurerm_resource_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source | ## Inputs @@ -118,7 +123,7 @@ No modules. | [capacity\_reservation\_group\_resource\_id](#input\_capacity\_reservation\_group\_resource\_id) | (Optional) Specifies the Azure Resource ID of the Capacity Reservation Group with the Virtual Machine should be allocated to. Cannot be used with availability\_set\_id or proximity\_placement\_group\_id | `string` | `null` | no | | [computer\_name](#input\_computer\_name) | (Optional) Specifies the Hostname which should be used for this Virtual Machine. If unspecified this defaults to the value for the `vm_name` field. If the value of the `vm_name` field is not a valid `computer_name`, then you must specify `computer_name`. Changing this forces a new resource to be created. | `string` | `null` | no | | [custom\_data](#input\_custom\_data) | (Optional) The Base64 encoded Custom Data for building this virtual machine. Changing this forces a new resource to be created | `string` | `null` | no | -| [data\_disk\_managed\_disks](#input\_data\_disk\_managed\_disks) | This variable is a map of objects used to define one or more data disks for creation and attachment to the virtual machine.

- `` - Use a custom map key to define each data disk
- `caching` (Required) - Specifies the caching requirements for this Data Disk. Possible values include None, ReadOnly and ReadWrite
- `lun` (Required) - The Logical Unit Number of the Data Disk, which needs to be unique within the Virtual Machine. Changing this forces a new resource to be created.
- `name` (Required) - Specifies the name of the Managed Disk. Changing this forces a new resource to be created.
- `storage_account_type` (Required) - The type of storage to use for the managed disk. Possible values are Standard\_LRS, StandardSSD\_ZRS, Premium\_LRS, PremiumV2\_LRS, Premium\_ZRS, StandardSSD\_LRS or UltraSSD\_LRS
- `create_option` (Optional) - The method to use when creating the managed disk. Changing this forces a new resource to be created. Possible values include: 1. Import - Import a VHD file in to the managed disk (VHD specified with source\_uri). 2.ImportSecure - Securely import a VHD file in to the managed disk (VHD specified with source\_uri). 3. Empty - Create an empty managed disk. 4. Copy - Copy an existing managed disk or snapshot (specified with source\_resource\_id). 5. FromImage - Copy a Platform Image (specified with image\_reference\_id) 6. Restore - Set by Azure Backup or Site Recovery on a restored disk (specified with source\_resource\_id). 7. Upload - Upload a VHD disk with the help of SAS URL (to be used with upload\_size\_bytes).
- `disk_access_resource_id` (Optional) - The ID of the disk access resource for using private endpoints on disks. disk\_access\_resource\_id is only supported when network\_access\_policy is set to AllowPrivate.
- `disk_attachment_create_option` (Optional) - The disk attachment create Option of the Data Disk, such as Empty or Attach. Defaults to Attach. Changing this forces a new resource to be created.
- `disk_encryption_set_resource_id` (Optional) - The resource ID of the Disk Encryption Set which should be used to Encrypt this OS Disk.
- `disk_iops_read_only` (Optional) - The number of IOPS allowed across all VMs mounting the shared disk as read-only; only settable for UltraSSD disks and PremiumV2 disks with shared disk enabled. One operation can transfer between 4k and 256k bytes.
- `disk_iops_read_write` (Optional) - The number of IOPS allowed for this disk; only settable for UltraSSD disks and PremiumV2 disks. One operation can transfer between 4k and 256k bytes.
- `disk_mbps_read_only` (Optional) - The bandwidth allowed across all VMs mounting the shared disk as read-only; only settable for UltraSSD disks and PremiumV2 disks with shared disk enabled. MBps means millions of bytes per second.
- `disk_mbps_read_write` (Optional) - The bandwidth allowed for this disk; only settable for UltraSSD disks and PremiumV2 disks. MBps means millions of bytes per second.
- `disk_size_gb` (Optional) - (Required for a new managed disk) - Specifies the size of the managed disk to create in gigabytes. If create\_option is Copy or FromImage, then the value must be equal to or greater than the source's size. The size can only be increased.If No Downtime Resizing is not available, be aware that changing this value is disruptive if the disk is attached to a Virtual Machine. The VM will be shut down and de-allocated as required by Azure to action the change. Terraform will attempt to start the machine again after the update if it was in a running state when the apply was started. When upgrading disk\_size\_gb from value less than 4095 to a value greater than 4095, the disk will be detached from its associated Virtual Machine as required by Azure to action the change. Terraform will attempt to reattach the disk again after the update.
- `gallery_image_reference_resource_id` (Optional) - ID of a Gallery Image Version to copy when create\_option is FromImage. This field cannot be specified if image\_reference\_id is specified. Changing this forces a new resource to be created.
- `hyper_v_generation` (Optional) - The HyperV Generation of the Disk when the source of an Import or Copy operation targets a source that contains an operating system. Possible values are V1 and V2. For ImportSecure it must be set to V2. Changing this forces a new resource to be created.
- `image_reference_resource_id` (Optional) - ID of an existing platform/marketplace disk image to copy when create\_option is FromImage. This field cannot be specified if gallery\_image\_reference\_resource\_id is specified. Changing this forces a new resource to be created.
- `inherit_tags` (Optional) - Defaults to true. Set this to false if only the tags defined on this resource should be applied.
- `lock_level` (Optional) - Set this value to override the resource level lock value. Possible values are `CanNotDelete`, and `ReadOnly`.
- `lock_name` (Optional) - The name for the lock on this disk
- `logical_sector_size` (Optional) - Logical Sector Size. Possible values are: 512 and 4096. Defaults to 4096. Changing this forces a new resource to be created. Setting logical sector size is supported only with UltraSSD\_LRS disks and PremiumV2\_LRS disks.
- `max_shares` (Optional) - The maximum number of VMs that can attach to the disk at the same time. Value greater than one indicates a disk that can be mounted on multiple VMs at the same time. Premium SSD maxShares limit: P15 and P20 disks: 2. P30,P40,P50 disks: 5. P60,P70,P80 disks: 10. For ultra disks the max\_shares minimum value is 1 and the maximum is 5.
- `network_access_policy` (Optional) - Policy for accessing the disk via network. Allowed values are AllowAll, AllowPrivate, and DenyAll.
- `on_demand_bursting_enabled` (Optional) - Specifies if On-Demand Bursting is enabled for the Managed Disk.
- `optimized_frequent_attach_enabled` (Optional) - Specifies whether this Managed Disk should be optimized for frequent disk attachments (where a disk is attached/detached more than 5 times in a day). Defaults to false. Setting optimized\_frequent\_attach\_enabled to true causes the disks to not align with the fault domain of the Virtual Machine, which can have operational implications.
- `os_type` (Optional) - Specify a value when the source of an Import, ImportSecure or Copy operation targets a source that contains an operating system. Valid values are Linux or Windows.
- `performance_plus_enabled` (Optional) - Specifies whether Performance Plus is enabled for this Managed Disk. Defaults to false. Changing this forces a new resource to be created. performance\_plus\_enabled can only be set to true when using a Managed Disk with an Ultra SSD.
- `public_network_access_enabled` (Optional) - Whether it is allowed to access the disk via public network. Defaults to true.
- `resource_group_name` (Optional) - Specify a resource group name if the data disk should be created in a separate resource group from the virtual machine
- `secure_vm_disk_encryption_set_resource_id` (Optional) - The ID of the Disk Encryption Set which should be used to Encrypt this OS Disk when the Virtual Machine is a Confidential VM. Conflicts with disk\_encryption\_set\_id. Changing this forces a new resource to be created. secure\_vm\_disk\_encryption\_set\_resource\_id can only be specified when security\_type is set to ConfidentialVM\_DiskEncryptedWithCustomerKey.
- `security_type` (Optional) - Security Type of the Managed Disk when it is used for a Confidential VM. Possible values are ConfidentialVM\_VMGuestStateOnlyEncryptedWithPlatformKey, ConfidentialVM\_DiskEncryptedWithPlatformKey and ConfidentialVM\_DiskEncryptedWithCustomerKey. Changing this forces a new resource to be created. When security\_type is set to ConfidentialVM\_DiskEncryptedWithCustomerKey the value of create\_option must be one of FromImage or ImportSecure. security\_type cannot be specified when trusted\_launch\_enabled is set to true. secure\_vm\_disk\_encryption\_set\_id must be specified when security\_type is set to ConfidentialVM\_DiskEncryptedWithCustomerKey.
- `source_resource_id` (Optional) - The ID of an existing Managed Disk or Snapshot to copy when create\_option is Copy or the recovery point to restore when create\_option is Restore. Changing this forces a new resource to be created.
- `source_uri` (Optional) - URI to a valid VHD file to be used when create\_option is Import or ImportSecure. Changing this forces a new resource to be created.
- `storage_account_resource_id` (Optional) - The ID of the Storage Account where the source\_uri is located. Required when create\_option is set to Import or ImportSecure. Changing this forces a new resource to be created.
- `tags` (Optional) - A mapping of tags to assign to the resource.
- `tier` (Optional) - The disk performance tier to use. Possible values are documented at https://docs.microsoft.com/azure/virtual-machines/disks-change-performance. This feature is currently supported only for premium SSDs.Changing this value is disruptive if the disk is attached to a Virtual Machine. The VM will be shut down and de-allocated as required by Azure to action the change. Terraform will attempt to start the machine again after the update if it was in a running state when the apply was started.
- `trusted_launch_enabled` (Optional) - Specifies if Trusted Launch is enabled for the Managed Disk. Changing this forces a new resource to be created. Trusted Launch can only be enabled when create\_option is FromImage or Import
- `upload_size_bytes` (Optional) - Specifies the size of the managed disk to create in bytes. Required when create\_option is Upload. The value must be equal to the source disk to be copied in bytes. Source disk size could be calculated with ls -l or wc -c. More information can be found at Copy a managed disk. Changing this forces a new resource to be created.
- `write_accelerator_enabled` (Optional) - Specifies if Write Accelerator is enabled on the disk. This can only be enabled on Premium\_LRS managed disks with no caching and M-Series VMs. Defaults to false.
- `encryption_settings` = (Optional) List of encryption objects with the following attributes:
- `disk_encryption_key_vault_secret_url` (Required) - The URL to the Key Vault Secret used as the Disk Encryption Key. This can be found as the id on the azurerm\_key\_vault\_secret resource.
- `disk_encryption_key_vault_resource_id` (Required) - The ID of the source Key Vault. This can be found as the id on the azurerm\_key\_vault resource.
- `key_encryption_key_vault_secret_url` (Required) - The URL to the Key Vault Key used as the Key Encryption Key. This can be found as the id on the azurerm\_key\_vault\_key resource.
- `key_encryption_key_vault_resource_id` (Required) - The ID of the source Key Vault. This can be found as the id on the azurerm\_key\_vault resource.
- `role_assignments` = (Optional) - Map of role assignments to assign to this disk
- `` - Use a custom map key to define each role assignment configuration assigned to the system managed identity of this virtual machine
- `role_definition_id_or_name` = (Required) - The Scoped-ID of the Role Definition or the built-in role name. Changing this forces a new resource to be created. Conflicts with role\_definition\_name
- `scope_resource_id` = (Required) - The scope at which the System Managed Identity Role Assignment applies to, such as /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333, /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup, or /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM, or /providers/Microsoft.Management/managementGroups/myMG. Changing this forces a new resource to be created.
- `condition` = (Optional) - The condition that limits the resources that the role can be assigned to. Changing this forces a new resource to be created.
- `condition_version` = (Optional) - The version of the condition. Possible values are 1.0 or 2.0. Changing this forces a new resource to be created.
- `description` = (Optional) - The description for this Role Assignment. Changing this forces a new resource to be created.
- `skip_service_principal_aad_check` = (Optional) - If the principal\_id is a newly provisioned Service Principal set this value to true to skip the Azure Active Directory check which may fail due to replication lag. This argument is only valid if the principal\_id is a Service Principal identity. Defaults to true.
- `delegated_managed_identity_resource_id` = (Optional) - The delegated Azure Resource Id which contains a Managed Identity. Changing this forces a new resource to be created.
- `principal_type` = (Optional) - The type of the `principal_id`. Possible values are `User`, `Group` and `ServicePrincipal`. It is necessary to explicitly set this attribute when creating role assignments if the principal creating the assignment is constrained by ABAC rules that filters on the PrincipalType attribute.

Example Inputs:
hcl
#Create a new empty disk and attach it as lun 0
data_disk_managed_disks = {
disk1 = {
name = "testdisk1-win-lun0"
storage_account_type = "Premium_LRS"
lun = 0
caching = "ReadWrite"
disk_size_gb = 32
}
}
|
map(object({
caching = string
lun = number
name = string
storage_account_type = string
create_option = optional(string, "Empty")
disk_access_resource_id = optional(string)
disk_attachment_create_option = optional(string)
disk_encryption_set_resource_id = optional(string) #this is currently a preview feature in the provider
disk_iops_read_only = optional(number, null)
disk_iops_read_write = optional(number, null)
disk_mbps_read_only = optional(number, null)
disk_mbps_read_write = optional(number, null)
disk_size_gb = optional(number, 128)
edge_zone = optional(string, null)
gallery_image_reference_resource_id = optional(string)
hyper_v_generation = optional(string)
image_reference_resource_id = optional(string)
inherit_tags = optional(bool, true)
lock_level = optional(string, null)
lock_name = optional(string, null)
logical_sector_size = optional(number, null)
max_shares = optional(number)
network_access_policy = optional(string)
on_demand_bursting_enabled = optional(bool)
optimized_frequent_attach_enabled = optional(bool, false)
os_type = optional(string)
performance_plus_enabled = optional(bool, false)
public_network_access_enabled = optional(bool)
resource_group_name = optional(string)
secure_vm_disk_encryption_set_resource_id = optional(string)
security_type = optional(string)
source_resource_id = optional(string)
source_uri = optional(string)
storage_account_resource_id = optional(string)
tags = optional(map(string), null)
tier = optional(string)
trusted_launch_enabled = optional(bool)
upload_size_bytes = optional(number, null)
write_accelerator_enabled = optional(bool)

encryption_settings = optional(list(object({
disk_encryption_key_vault_secret_url = optional(string)
disk_encryption_key_vault_resource_id = optional(string)
key_encryption_key_vault_secret_url = optional(string)
key_encryption_key_vault_resource_id = optional(string)
})), [])

role_assignments = optional(map(object({
role_definition_id_or_name = string
principal_id = string
description = optional(string, null)
skip_service_principal_aad_check = optional(bool, false)
condition = optional(string, null)
condition_version = optional(string, null)
delegated_managed_identity_resource_id = optional(string, null)
principal_type = optional(string, null)
})), {})
}))
| `{}` | no | +| [data\_disk\_managed\_disks](#input\_data\_disk\_managed\_disks) | This variable is a map of objects used to define one or more data disks for creation and attachment to the virtual machine.

- `` - Use a custom map key to define each data disk
- `caching` (Required) - Specifies the caching requirements for this Data Disk. Possible values include None, ReadOnly and ReadWrite
- `lun` (Required) - The Logical Unit Number of the Data Disk, which needs to be unique within the Virtual Machine. Changing this forces a new resource to be created.
- `name` (Required) - Specifies the name of the Managed Disk. Changing this forces a new resource to be created.
- `storage_account_type` (Required) - The type of storage to use for the managed disk. Possible values are Standard\_LRS, StandardSSD\_ZRS, Premium\_LRS, PremiumV2\_LRS, Premium\_ZRS, StandardSSD\_LRS or UltraSSD\_LRS
- `create_option` (Optional) - The method to use when creating the managed disk. Changing this forces a new resource to be created. Possible values include: 1. Import - Import a VHD file in to the managed disk (VHD specified with source\_uri). 2.ImportSecure - Securely import a VHD file in to the managed disk (VHD specified with source\_uri). 3. Empty - Create an empty managed disk. 4. Copy - Copy an existing managed disk or snapshot (specified with source\_resource\_id). 5. FromImage - Copy a Platform Image (specified with image\_reference\_id) 6. Restore - Set by Azure Backup or Site Recovery on a restored disk (specified with source\_resource\_id). 7. Upload - Upload a VHD disk with the help of SAS URL (to be used with upload\_size\_bytes).
- `disk_access_resource_id` (Optional) - The ID of the disk access resource for using private endpoints on disks. disk\_access\_resource\_id is only supported when network\_access\_policy is set to AllowPrivate.
- `disk_attachment_create_option` (Optional) - The disk attachment create Option of the Data Disk, such as Empty or Attach. Defaults to Attach. Changing this forces a new resource to be created.
- `disk_encryption_set_resource_id` (Optional) - The resource ID of the Disk Encryption Set which should be used to Encrypt this OS Disk.
- `disk_iops_read_only` (Optional) - The number of IOPS allowed across all VMs mounting the shared disk as read-only; only settable for UltraSSD disks and PremiumV2 disks with shared disk enabled. One operation can transfer between 4k and 256k bytes.
- `disk_iops_read_write` (Optional) - The number of IOPS allowed for this disk; only settable for UltraSSD disks and PremiumV2 disks. One operation can transfer between 4k and 256k bytes.
- `disk_mbps_read_only` (Optional) - The bandwidth allowed across all VMs mounting the shared disk as read-only; only settable for UltraSSD disks and PremiumV2 disks with shared disk enabled. MBps means millions of bytes per second.
- `disk_mbps_read_write` (Optional) - The bandwidth allowed for this disk; only settable for UltraSSD disks and PremiumV2 disks. MBps means millions of bytes per second.
- `disk_size_gb` (Optional) - (Required for a new managed disk) - Specifies the size of the managed disk to create in gigabytes. If create\_option is Copy or FromImage, then the value must be equal to or greater than the source's size. The size can only be increased.If No Downtime Resizing is not available, be aware that changing this value is disruptive if the disk is attached to a Virtual Machine. The VM will be shut down and de-allocated as required by Azure to action the change. Terraform will attempt to start the machine again after the update if it was in a running state when the apply was started. When upgrading disk\_size\_gb from value less than 4095 to a value greater than 4095, the disk will be detached from its associated Virtual Machine as required by Azure to action the change. Terraform will attempt to reattach the disk again after the update.
- `gallery_image_reference_resource_id` (Optional) - ID of a Gallery Image Version to copy when create\_option is FromImage. This field cannot be specified if image\_reference\_id is specified. Changing this forces a new resource to be created.
- `hyper_v_generation` (Optional) - The HyperV Generation of the Disk when the source of an Import or Copy operation targets a source that contains an operating system. Possible values are V1 and V2. For ImportSecure it must be set to V2. Changing this forces a new resource to be created.
- `image_reference_resource_id` (Optional) - ID of an existing platform/marketplace disk image to copy when create\_option is FromImage. This field cannot be specified if gallery\_image\_reference\_resource\_id is specified. Changing this forces a new resource to be created.
- `inherit_tags` (Optional) - Defaults to true. Set this to false if only the tags defined on this resource should be applied.
- `lock_level` (Optional) - Set this value to override the resource level lock value. Possible values are `CanNotDelete`, and `ReadOnly`.
- `lock_name` (Optional) - The name for the lock on this disk
- `logical_sector_size` (Optional) - Logical Sector Size. Possible values are: 512 and 4096. Defaults to 4096. Changing this forces a new resource to be created. Setting logical sector size is supported only with UltraSSD\_LRS disks and PremiumV2\_LRS disks.
- `max_shares` (Optional) - The maximum number of VMs that can attach to the disk at the same time. Value greater than one indicates a disk that can be mounted on multiple VMs at the same time. Premium SSD maxShares limit: P15 and P20 disks: 2. P30,P40,P50 disks: 5. P60,P70,P80 disks: 10. For ultra disks the max\_shares minimum value is 1 and the maximum is 5.
- `network_access_policy` (Optional) - Policy for accessing the disk via network. Allowed values are AllowAll, AllowPrivate, and DenyAll.
- `on_demand_bursting_enabled` (Optional) - Specifies if On-Demand Bursting is enabled for the Managed Disk.
- `optimized_frequent_attach_enabled` (Optional) - Specifies whether this Managed Disk should be optimized for frequent disk attachments (where a disk is attached/detached more than 5 times in a day). Defaults to false. Setting optimized\_frequent\_attach\_enabled to true causes the disks to not align with the fault domain of the Virtual Machine, which can have operational implications.
- `os_type` (Optional) - Specify a value when the source of an Import, ImportSecure or Copy operation targets a source that contains an operating system. Valid values are Linux or Windows.
- `performance_plus_enabled` (Optional) - Specifies whether Performance Plus is enabled for this Managed Disk. Defaults to false. Changing this forces a new resource to be created. performance\_plus\_enabled can only be set to true when using a Managed Disk with an Ultra SSD.
- `public_network_access_enabled` (Optional) - Whether it is allowed to access the disk via public network. Defaults to true.
- `resource_group_name` (Optional) - Specify a resource group name if the data disk should be created in a separate resource group from the virtual machine
- `secure_vm_disk_encryption_set_resource_id` (Optional) - The ID of the Disk Encryption Set which should be used to Encrypt this OS Disk when the Virtual Machine is a Confidential VM. Conflicts with disk\_encryption\_set\_id. Changing this forces a new resource to be created. secure\_vm\_disk\_encryption\_set\_resource\_id can only be specified when security\_type is set to ConfidentialVM\_DiskEncryptedWithCustomerKey.
- `security_type` (Optional) - Security Type of the Managed Disk when it is used for a Confidential VM. Possible values are ConfidentialVM\_VMGuestStateOnlyEncryptedWithPlatformKey, ConfidentialVM\_DiskEncryptedWithPlatformKey and ConfidentialVM\_DiskEncryptedWithCustomerKey. Changing this forces a new resource to be created. When security\_type is set to ConfidentialVM\_DiskEncryptedWithCustomerKey the value of create\_option must be one of FromImage or ImportSecure. security\_type cannot be specified when trusted\_launch\_enabled is set to true. secure\_vm\_disk\_encryption\_set\_id must be specified when security\_type is set to ConfidentialVM\_DiskEncryptedWithCustomerKey.
- `source_resource_id` (Optional) - The ID of an existing Managed Disk or Snapshot to copy when create\_option is Copy or the recovery point to restore when create\_option is Restore. Changing this forces a new resource to be created.
- `source_uri` (Optional) - URI to a valid VHD file to be used when create\_option is Import or ImportSecure. Changing this forces a new resource to be created.
- `storage_account_resource_id` (Optional) - The ID of the Storage Account where the source\_uri is located. Required when create\_option is set to Import or ImportSecure. Changing this forces a new resource to be created.
- `tags` (Optional) - A mapping of tags to assign to the resource.
- `tier` (Optional) - The disk performance tier to use. Possible values are documented at https://docs.microsoft.com/azure/virtual-machines/disks-change-performance. This feature is currently supported only for premium SSDs.Changing this value is disruptive if the disk is attached to a Virtual Machine. The VM will be shut down and de-allocated as required by Azure to action the change. Terraform will attempt to start the machine again after the update if it was in a running state when the apply was started.
- `trusted_launch_enabled` (Optional) - Specifies if Trusted Launch is enabled for the Managed Disk. Changing this forces a new resource to be created. Trusted Launch can only be enabled when create\_option is FromImage or Import
- `upload_size_bytes` (Optional) - Specifies the size of the managed disk to create in bytes. Required when create\_option is Upload. The value must be equal to the source disk to be copied in bytes. Source disk size could be calculated with ls -l or wc -c. More information can be found at Copy a managed disk. Changing this forces a new resource to be created.
- `write_accelerator_enabled` (Optional) - Specifies if Write Accelerator is enabled on the disk. This can only be enabled on Premium\_LRS managed disks with no caching and M-Series VMs. Defaults to false.
- `encryption_settings` = (Optional) List of encryption objects with the following attributes:
- `disk_encryption_key_vault_secret_url` (Required) - The URL to the Key Vault Secret used as the Disk Encryption Key. This can be found as the id on the azurerm\_key\_vault\_secret resource.
- `disk_encryption_key_vault_resource_id` (Required) - The ID of the source Key Vault. This can be found as the id on the azurerm\_key\_vault resource.
- `key_encryption_key_vault_secret_url` (Required) - The URL to the Key Vault Key used as the Key Encryption Key. This can be found as the id on the azurerm\_key\_vault\_key resource.
- `key_encryption_key_vault_resource_id` (Required) - The ID of the source Key Vault. This can be found as the id on the azurerm\_key\_vault resource.
- `role_assignments` = (Optional) - Map of role assignments to assign to this disk
- `` - Use a custom map key to define each role assignment configuration assigned to the system managed identity of this virtual machine
- `role_definition_id_or_name` = (Required) - The Scoped-ID of the Role Definition or the built-in role name. Changing this forces a new resource to be created. Conflicts with role\_definition\_name
- `scope_resource_id` = (Required) - The scope at which the System Managed Identity Role Assignment applies to, such as /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333, /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup, or /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM, or /providers/Microsoft.Management/managementGroups/myMG. Changing this forces a new resource to be created.
- `condition` = (Optional) - The condition that limits the resources that the role can be assigned to. Changing this forces a new resource to be created.
- `condition_version` = (Optional) - The version of the condition. Possible values are 1.0 or 2.0. Changing this forces a new resource to be created.
- `description` = (Optional) - The description for this Role Assignment. Changing this forces a new resource to be created.
- `skip_service_principal_aad_check` = (Optional) - If the principal\_id is a newly provisioned Service Principal set this value to true to skip the Azure Active Directory check which may fail due to replication lag. This argument is only valid if the principal\_id is a Service Principal identity. Defaults to true.
- `delegated_managed_identity_resource_id` = (Optional) - The delegated Azure Resource Id which contains a Managed Identity. Changing this forces a new resource to be created.
- `principal_type` = (Optional) - The type of the `principal_id`. Possible values are `User`, `Group` and `ServicePrincipal`. It is necessary to explicitly set this attribute when creating role assignments if the principal creating the assignment is constrained by ABAC rules that filters on the PrincipalType attribute.

Example Inputs:
hcl
#Create a new empty disk and attach it as lun 0
data_disk_managed_disks = {
disk1 = {
name = "testdisk1-win-lun0"
storage_account_type = "Premium_LRS"
lun = 0
caching = "ReadWrite"
disk_size_gb = 32
}
}
|
map(object({
caching = string
lun = number
name = string
storage_account_type = string
create_option = optional(string, "Empty")
disk_access_resource_id = optional(string)
disk_attachment_create_option = optional(string)
disk_encryption_set_resource_id = optional(string) #this is currently a preview feature in the provider
disk_iops_read_only = optional(number, null)
disk_iops_read_write = optional(number, null)
disk_mbps_read_only = optional(number, null)
disk_mbps_read_write = optional(number, null)
disk_size_gb = optional(number, 128)
edge_zone = optional(string, null)
gallery_image_reference_resource_id = optional(string)
hyper_v_generation = optional(string)
image_reference_resource_id = optional(string)
inherit_tags = optional(bool, true)
lock_level = optional(string, null)
lock_name = optional(string, null)
logical_sector_size = optional(number, null)
max_shares = optional(number)
network_access_policy = optional(string)
on_demand_bursting_enabled = optional(bool)
optimized_frequent_attach_enabled = optional(bool, false)
os_type = optional(string)
performance_plus_enabled = optional(bool, false)
public_network_access_enabled = optional(bool, false)
resource_group_name = optional(string)
secure_vm_disk_encryption_set_resource_id = optional(string)
security_type = optional(string)
source_resource_id = optional(string)
source_uri = optional(string)
storage_account_resource_id = optional(string)
tags = optional(map(string), null)
tier = optional(string)
trusted_launch_enabled = optional(bool)
upload_size_bytes = optional(number, null)
write_accelerator_enabled = optional(bool)

encryption_settings = optional(list(object({
disk_encryption_key_vault_secret_url = optional(string)
disk_encryption_key_vault_resource_id = optional(string)
key_encryption_key_vault_secret_url = optional(string)
key_encryption_key_vault_resource_id = optional(string)
})), [])

role_assignments = optional(map(object({
role_definition_id_or_name = string
principal_id = string
description = optional(string, null)
skip_service_principal_aad_check = optional(bool, false)
condition = optional(string, null)
condition_version = optional(string, null)
delegated_managed_identity_resource_id = optional(string, null)
principal_type = optional(string, null)
})), {})
}))
| `{}` | no | | [dedicated\_host\_group\_resource\_id](#input\_dedicated\_host\_group\_resource\_id) | (Optional) The Azure Resource ID of the dedicated host group where this virtual machine should run. Conflicts with dedicated\_host\_resource\_id (dedicated\_host\_group\_id on the azurerm provider) | `string` | `null` | no | | [dedicated\_host\_resource\_id](#input\_dedicated\_host\_resource\_id) | (Optional) The Azure Resource ID of the dedicated host where this virtual machine should run. Conflicts with dedicated\_host\_group\_resource\_id (dedicated\_host\_group\_id on the azurerm provider) | `string` | `null` | no | | [disable\_password\_authentication](#input\_disable\_password\_authentication) | If true this value will disallow password authentication on linux vm's. This will require at least one public key to be configured. If using the option to auto generate passwords and keys, setting this value to `false` will cause a password to be generated an stored instead of an SSH key. | `bool` | `true` | no | @@ -137,6 +142,7 @@ No modules. | [managed\_identities](#input\_managed\_identities) | An object that sets the managed identity configuration for the virtual machine being deployed. Be aware that capabilities such as the Azure Monitor Agent and Role Assignments require that a managed identity has been configured.

- `system_assigned` = (Optional) Specifies whether the System Assigned Managed Identity should be enabled. Defaults to false.
- `user_assigned_resource_ids` = (Optional) Specifies a set of User Assigned Managed Identity IDs to be assigned to this Virtual Machine.

Example Inputs:
hcl
#default system managed identity
managed_identities = {
system_assigned = true
}
#user assigned managed identity only
managed_identities = {
user_assigned_resource_ids = [""]
}
#user assigned and system assigned managed identities
managed_identities = {
system_assigned = true
user_assigned_resource_ids = [""]
}
|
object({
system_assigned = optional(bool, false)
user_assigned_resource_ids = optional(set(string), [])
})
| `{}` | no | | [max\_bid\_price](#input\_max\_bid\_price) | (Optional) The maximum price you're willing to pay for this Virtual Machine, in US Dollars; which must be greater than the current spot price. If this bid price falls below the current spot price the Virtual Machine will be evicted using the `eviction_policy`. Defaults to `-1`, which means that the Virtual Machine should not be evicted for price reasons. This can only be configured when `priority` is set to `Spot`. | `number` | `-1` | no | | [os\_disk](#input\_os\_disk) | Required configuration values for the OS disk on the virtual machine.

- `caching` = (Required) - The type of caching which should be used for the internal OS disk. Possible values are `None`, `ReadOnly`, and `ReadWrite`.
- `storage_account_type` = (Required) - The Type of Storage Account which should back this the Internal OS Disk. Possible values are `Standard_LRS`, `Premium_LRS`, `Premium_LRS`, `StandardSSD_ZRS` and `Premium_ZRS`. Changing this forces a new resource to be created
- `disk_encryption_set_id` = (Optional) - The Azure Resource ID of the Disk Encryption Set which should be used to Encrypt this OS Disk. Conflicts with secure\_vm\_disk\_encryption\_set\_id. The Disk Encryption Set must have the Reader Role Assignment scoped on the Key Vault - in addition to an Access Policy to the Key Vault
- `disk_size_gb` = (Optional) - The Size of the Internal OS Disk in GB, if you wish to vary from the size used in the image this Virtual Machine is sourced from.
- `name` = (Optional) - The name which should be used for the Internal OS Disk. Changing this forces a new resource to be created.
- `secure_vm_disk_encryption_set_id` = (Optional) - The Azure Resource ID of the Disk Encryption Set which should be used to Encrypt this OS Disk when the Virtual Machine is a Confidential VM. Conflicts with disk\_encryption\_set\_id. Changing this forces a new resource to be created.
- `security_encryption_type` = (Optional) - Encryption Type when the Virtual Machine is a Confidential VM. Possible values are `VMGuestStateOnly` and `DiskWithVMGuestState`. Changing this forces a new resource to be created. `vtpm_enabled` must be set to true when security\_encryption\_type is specified. encryption\_at\_host\_enabled cannot be set to `true` when security\_encryption\_type is set to `DiskWithVMGuestState`
- `write_accelerator_enabled` = (Optional) - Should Write Accelerator be Enabled for this OS Disk? Defaults to `false`. This requires that the storage\_account\_type is set to `Premium_LRS` and that caching is set to `None`
- `diff_disk_settings` - An optional object defining the diff disk settings
- `option` = (Required) - Specifies the Ephemeral Disk Settings for the OS Disk. At this time the only possible value is `Local`. Changing this forces a new resource to be created.
- `placement` = (Optional) - Specifies where to store the Ephemeral Disk. Possible values are CacheDisk and ResourceDisk. Defaults to CacheDisk. Changing this forces a new resource to be created.

Example Inputs:
hcl
#basic example:
os_disk = {
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}

#increased disk size and write acceleration example
os_disk = {
name = "sample os disk"
caching = "None"
storage_account_type = "Premium_LRS"
disk_size_gb = 128
write_accelerator_enabled = true
}
|
object({
caching = string
storage_account_type = string
disk_encryption_set_id = optional(string)
disk_size_gb = optional(number)
name = optional(string)
secure_vm_disk_encryption_set_id = optional(string)
security_encryption_type = optional(string)
write_accelerator_enabled = optional(bool, false)
diff_disk_settings = optional(object({
option = string
placement = optional(string, "CacheDisk")
}), null)
})
|
{
"caching": "ReadWrite",
"storage_account_type": "StandardSSD_ZRS"
}
| no | +| [os\_disk\_managed\_disk](#input\_os\_disk\_managed\_disk) | This variable is an object used to define the managed disk settings for the OS disk of the virtual machine.

- `network_access_policy` (Optional) - Policy for accessing the disk via network. Allowed values are AllowAll, AllowPrivate, and DenyAll.
- `public_network_access_enabled` (Optional) - Whether it is allowed to access the disk via public network. Defaults to Disabled.
hcl
os_disk_managed_disk = {
network_access_policy = "AllowPrivate"
public_network_access_enabled = "Enabled"
}
|
object({
network_access_policy = optional(string, "DenyAll")
public_network_access_enabled = optional(string, "Disabled")
})
| `{}` | no | | [os\_type](#input\_os\_type) | The base OS type of the vm to be built. Valid answers are Windows or Linux | `string` | `null` | no | | [patch\_assessment\_mode](#input\_patch\_assessment\_mode) | (Optional) Specifies the mode of VM Guest Patching for the Virtual Machine. Possible values are `AutomaticByPlatform` or `ImageDefault`. Defaults to `AutomaticByPlatform`. | `string` | `"AutomaticByPlatform"` | no | | [patch\_mode](#input\_patch\_mode) | (Optional) Specifies the mode of in-guest patching to this Linux Virtual Machine. Possible values are `AutomaticByPlatform` and `ImageDefault`. Defaults to `ImageDefault`. For more information on patch modes please see the [product documentation](https://docs.microsoft.com/azure/virtual-machines/automatic-vm-guest-patching#patch-orchestration-modes). | `string` | `null` | no | diff --git a/data.tf b/data.tf new file mode 100644 index 0000000..62e3384 --- /dev/null +++ b/data.tf @@ -0,0 +1,3 @@ +data "azurerm_resource_group" "this" { + name = var.resource_group_name +} \ No newline at end of file diff --git a/main.linux.tf b/main.linux.tf index 6c4aac7..3217282 100644 --- a/main.linux.tf +++ b/main.linux.tf @@ -165,4 +165,27 @@ resource "azurerm_linux_virtual_machine" "this" { lifecycle { ignore_changes = [vm_agent_platform_updates_enabled] } +} + + +# https://github.com/hashicorp/terraform-provider-azurerm/issues/15156 +# https://github.com/Azure/azure-rest-api-specs/issues/21325 + +resource "azapi_update_resource" "linux_os_disk" { + count = (lower(var.os_type) == "linux") ? 1 : 0 + + type = "Microsoft.Compute/disks@2023-01-02" + name = azurerm_linux_virtual_machine.this[0].os_disk[0].name + parent_id = data.azurerm_resource_group.this.id + + body = { + properties = { + networkAccessPolicy = var.os_disk_managed_disk.network_access_policy + publicNetworkAccess = var.os_disk_managed_disk.public_network_access_enabled + } + } + + depends_on = [ + azurerm_linux_virtual_machine.this + ] } \ No newline at end of file diff --git a/main.windows.tf b/main.windows.tf index 0c76c01..f0e52b2 100644 --- a/main.windows.tf +++ b/main.windows.tf @@ -181,3 +181,25 @@ resource "azurerm_windows_virtual_machine" "this" { ] } } + +# https://github.com/hashicorp/terraform-provider-azurerm/issues/15156 +# https://github.com/Azure/azure-rest-api-specs/issues/21325 + +resource "azapi_update_resource" "windows_os_disk" { + count = (lower(var.os_type) == "windows") ? 1 : 0 + + type = "Microsoft.Compute/disks@2023-01-02" + name = azurerm_windows_virtual_machine.this[0].os_disk[0].name + parent_id = data.azurerm_resource_group.this.id + + body = { + properties = { + networkAccessPolicy = var.os_disk_managed_disk.network_access_policy + publicNetworkAccess = var.os_disk_managed_disk.public_network_access_enabled + } + } + + depends_on = [ + azurerm_windows_virtual_machine.this + ] +} \ No newline at end of file diff --git a/terraform.tf b/terraform.tf index 910b9d2..33a35ed 100644 --- a/terraform.tf +++ b/terraform.tf @@ -6,6 +6,10 @@ terraform { source = "hashicorp/azurerm" version = ">= 4.5, < 5.0" } + azapi = { + source = "Azure/azapi" + version = ">= 2.0, < 3.0" + } time = { source = "hashicorp/time" version = ">= 0.12" diff --git a/variables.tf b/variables.tf index a76282f..be89ec5 100644 --- a/variables.tf +++ b/variables.tf @@ -118,6 +118,28 @@ variable "custom_data" { } } +variable "os_disk_managed_disk" { + type = object({ + network_access_policy = optional(string, "DenyAll") + public_network_access_enabled = optional(string, "Disabled") + }) + default = {} + description = <