How do I implement soft delete for acr using terraform

94 views Asked by At

I have a pre-existing ACR and I need to enable soft-deletion policy using terraform. I don't want to use null-resource, is there any other way I can achieve this?

resource "null_resource" "soft_delete_policy" {
  provisioner "local-exec" {
    command = "az acr config soft-delete update -r MyRegistry --days 7 --status Enabled"
  }
1

There are 1 answers

0
cdub On

You could use the AzAPI provider:

    softDeletePolicy = {
      retentionDays = int
      status = "string"
    }

First, run az acr show -g <your-resource-group> -n <your-acr-name> -o json to get the container registry's resource configuration in JSON format.

Then, construct an azapi_resource in your Terraform configuration using the relevant bits of the JSON:

resource "azapi_resource" "your_acr" {
  type = "Microsoft.ContainerRegistry/registries@2023-11-01-preview"
  name = "youracr"
  location = "westus"
  parent_id = azurerm_resource_group.your_resource_group.id
  tags = { }
  body = jsonencode({
    properties = {
      adminUserEnabled = false
...

Next, import the AzAPI resource into your Terraform configuration:

terraform import azapi_resource.your_acr /subscriptions/<your-subscription-id>/resourcegroups/<your-resource-group-name>/providers/Microsoft.ContainerRegistry/registries/youracr

Update the AzAPI resource in the Terraform config to set soft delete policy:

softDeletePolicy = {
  retentionDays = 7
  status = "enabled"
}

Finally, apply the change via terraform apply