skip to Main Content

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
}

3

Answers


  1. Chosen as BEST ANSWER

    Managed to resolve this - I basically used ignore changes on all the properties Massive thanks to @MarkoE and AC81


  2. 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

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search