Home > Software engineering >  How can I prevent terraform from destroying and recreating vm azure vm extensions. Life cycle block
How can I prevent terraform from destroying and recreating vm azure vm extensions. Life cycle block

Time:06-24

How can I prevent terraform from destroying and recreating azure vm extensions? Life cycle code block isn't working. Terraform persists on destroying the resources and fails when I have the locks enabled. Please can someone tell me where I am going wrong with this

This is my code

resource "azurerm_virtual_machine_extension" "dsc" {
  for_each = var.dsc_agent_name

  name                       = each.key
  virtual_machine_id         = each.value
  publisher                  = "Microsoft.Powershell"
  type                       = "DSC"
  type_handler_version       = "2.0"
  auto_upgrade_minor_version = "true"
  tags                       = local.tags
  lifecycle {
    prevent_destroy = true
  }

  settings = <<SETTINGS
        {
            "ModulesUrl":"",
            "SasToken":"",
            "WmfVersion": "latest",
            "Privacy": {
                "DataCollection": ""
            },
            "ConfigurationFunction":""
        }
    SETTINGS
}

CodePudding user response:

Try removing the lifecycle block and then run 'terraform plan' - it should then show you which configuration item is causing it to be destroyed / re-created

CodePudding user response:

You can try to put an ignore section in:

lifecycle {
    prevent_destroy = true
    ignore_changes = [ VMDiagnosticsSettings ]
}

That way it will ignore what has been set on the resource in Azure with what is being declared (if anything for this section) in TF

  • Related