r/Terraform•
TallSequoia

terraform apply fails reapply VM after extensions installed via policy

Help Wanted

I have a Terraform scripts that deploys a bare-bones Ubuntu Linux VM to Azure. No extensions are deployed via Terraform. This is successful. The subscription is enrolled in into Microsoft Defender for Cloud and a MDE.Linux extension is deployed to the VM automatically. Once the extension is provisioned, re-running terraform apply fails with a message

CreateOrUpdate: unexpected status 400 (400 Bad Request) with error: MismatchingNestedResourceSegments: The resource with name 'MDE.Linux' and type 'Microsoft.Compute/virtualMachines/extensions' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name. Please see https://aka.ms/arm-template/#resources for usage details.

If the extension is removed, the command completes successfully. But this is not desired and the extension is reinstalled automatically.

I tried adding lifecycle { ignore_changes = [extensions]} to the azurerm_linux_virtual_machine resource, but it did not help.

Is there a way to either ignore extensions or to import configuration of applied extensions to the TFSTATE file?

Posted Apr 8, 2025, 9:14 PM UTC

51
Loading comments…