Edit

Share via


"Policy definition not found" during policy assignments in Terraform

This article provides a solution to the "Policy definition not found" error that you encounter during policy assignments when you use the Terraform tool.

Symptoms

When you try to run the following policy assignments that are configured in the Main.tf file of the Terraform tool, you receive the "Policy definition not found" error message:

resource "azurerm_policy_assignment" "kubernetes" {
  name                 = "kubernetes"
  scope                = <scope>
  policy_definition_id = <policy_definition_id>
  description          = "Assignment of the Kubernetes to subscription."
  display_name         = "Kubernetes-custom-initiative-test01"
}

Cause

The error occurs if the policy assignment references the policy initiative definition that's provided for the policy_definition_id argument. However, the azurerm_policy_set_definition module might be delayed or not found before the policy assignment is created.

Solution

In your Terraform configuration, include a depends_on parameter within the policy assignment resource, as shown the following example. This value makes sure that the policy assignment is generated only after the creation of the policy set definition.

resource "azurerm_policy_assignment" "kubernetes" {
  name                 = "kubernetes"
  scope                = <scope>
  policy_definition_id = <policy_definition_id>
  description          = "Assignment of the Kubernetes to subscription."
  display_name         = "Kubernetes-custom-initiative-test01"
  depends_on = [azurerm_policy_definition.policies]
}

For more information, see the depends_on Meta-Argument.

Third-party information disclaimer

The third-party products that this article discusses are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, about the performance or reliability of these products.

Contact us for help

If you have questions or need help, create a support request, or ask Azure community support. You can also submit product feedback to Azure feedback community.