0
votes

I'm trying to use terraform to deploy budget alert for Azure, unfortunately there's no native resource in terraform for that, so I'm using azurerm_subscription_template_deployment along with ARM template.

I noticed a strange thing is after the deployment, everything seem fine, I can see the resource (in ARM template) by doing terraform state show.

However, when I do destroy, it doesn't seem to delete the budget alert, it's still there, and if I do terraform state list, it's showing everything is deleted without a trace, but actual resource is still there.

I noticed terraform has the following for azurerm_resource_group_template_deployment, I assume this is also for subscrption deployment

"This resource will automatically attempt to delete resources deployed by the ARM Template when it is deleted. You can opt-out of this by setting the delete_nested_items_during_deletion field within the template_deployment block of the features block to false."

so If I understand this correctly, If my features block is by default, it should delete the actual resource, when doing destroy.

why it's not behaving correctly?

code as following

resource "azurerm_subscription_template_deployment" "budgetalert" {
  name     = "mcs_budget_alert"
  location = var.location

  template_content = <<-TEMPLATE
 {
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "client_initial": {
          "type": "string",
          "metadata": {
            "description": "Name of the Budget. It should be unique within a resource group."
          }
        },
        "amount": {
          "type": "string",
          "defaultValue": "1000",
          "metadata": {
            "description": "The total amount of cost or usage to track with the budget"
          }
        },
        "timeGrain": {
          "type": "string",
          "defaultValue": "Monthly",
          "allowedValues": [
            "Monthly",
            "Quarterly",
            "Annually"
          ],
          "metadata": {
            "description": "The time covered by a budget. Tracking of the amount will be reset based on the time grain."
          }
        },
        "startDate": {
          "type": "string",
          "metadata": {
            "description": "The start date must be first of the month in YYYY-MM-DD format. Future start date should not be more than three months. Past start date should be selected within the timegrain preiod."
          }
        },
        "endDate": {
          "type": "string",
          "metadata": {
            "description": "The end date for the budget in YYYY-MM-DD format. If not provided, we default this to 10 years from the start date."
          }
        },
        "operator": {
          "type": "string",
          "defaultValue": "GreaterThan",
          "allowedValues": [
            "EqualTo",
            "GreaterThan",
            "GreaterThanOrEqualTo"
          ],
          "metadata": {
            "description": "The comparison operator."
          }
        },
        "contactGroups": {
          "type": "string",
          "metadata": {
            "description": "The list of action groups to send the budget notification to when the threshold is exceeded. It accepts array of strings."
        }
        },
        "threshold": {
          "type": "string",
          "defaultValue": "90",
          "metadata": {
            "description": "Threshold value associated with a notification. Notification is sent when the cost exceeded the threshold. It is always percent and has to be between 0 and 1000."
          }
        }
    },
      "variables":{
        "resourcegroupID": "[concat(subscription().id, '/resourceGroups/', 'ccl_mcs_maintain')]",
        "actionGroupName": "budget Action Group"
    },
      "resources": [
        {
          "type": "Microsoft.Consumption/budgets",
          "apiVersion": "2019-10-01",
          "name": "[concat(parameters('client_initial'), '-MCS-BudgetAlert')]",
          "properties": {
            "category": "Cost",
            "amount": "[parameters('amount')]",
            "timeGrain": "[parameters('timeGrain')]",
            "timePeriod": {
              "startDate": "[parameters('startDate')]",
              "endDate": "[parameters('endDate')]"
            },
            "notifications": {
              "First-Notification": {
                "enabled": true,
                "operator": "[parameters('operator')]",
                "threshold": "[parameters('threshold')]",
                "contactGroups": "[array(parameters('contactGroups'))]"
              
               }
            }
          },
          "dependsOn": []
        }
      ]
 }
  TEMPLATE

  // NOTE: whilst we show an inline template here, we recommend
  // sourcing this from a file for readability/editor support
  #parameters_content = file("./modules/budget_alert/parameters.json")
  parameters_content = <<-PARAMETER
  {
    "client_initial": {
      "value": ${jsonencode(var.client_initial)}
    },
    "amount": {
      "value": ${jsonencode(var.amount)}
    },
    "timeGrain": {
      "value": ${jsonencode(var.timeGrain)}
    },
    "startDate": {
      "value": ${jsonencode(var.startDate)}
    },
    "endDate": {
      "value": ${jsonencode(var.endDate)}
    },
    "operator": {
      "value": ${jsonencode(var.operator)}
    },
    "threshold": {
      "value": ${jsonencode(var.threshold)}
    },
    "contactGroups": {
     "value": ${jsonencode(var.contactGroups)}
    }
  }
  PARAMETER


}
1

1 Answers

0
votes

Your understanding is correct but unfortunately, the delete_nested_items_during_deletion field does not take effect on the resources azurerm_subscription_template_deployment. It removes the ARM template partition in the .tfstate file when you run terraform destroy. It also does not be mentioned in the document.

The field is only available on azurerm_resource_group_template_deployment after my validation. You may have a feature request like this.