Search code examples
terraformazure-policy

How do you use the output of a terraform apply as input into another terraform variable?


Policy as code - Azure - Terraform https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/policy_definition

The output is an id. This id needs to be used as a variable for a policy assignment. https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group_policy_assignment

  1. How is this written in terraform?
  2. Does the terraform apply need to be separate for each definition and assignment?

Solution

  • You can simply pass the azurerm_policy_definition output to resource azurerm_resource_group_policy_assignment like in the example you find in the DOC

    resource "azurerm_resource_group_policy_assignment" "example" {
      name                 = "example"
      resource_group_id    = azurerm_resource_group.example.id
      policy_definition_id = azurerm_policy_definition.example.id # This is the output of azurerm_policy_definition resource
    
      ...
      ...
    }