Can somene help me how to give the specific service principal name for azure custom policy in terraform code

54 views Asked by At

The Scenario is:We need to create resources in azure portal through terraform cloud only by using service principals and azure portal shouldn't allow creation of resources by other means. (No manual creation in azure portal,and also by SVM's tools like vs code) The following code which i tried:

resource "azurerm_policy_definition" "policych" {
  name         = "PolicyTestSP"
  policy_type  = "Custom"
  mode         = "Indexed"
  display_name = "Allow thru SP"

  metadata = <<METADATA
    {
    "category": "General"
    }

METADATA


  policy_rule = <<POLICY_RULE
    {
    "if": {
        "not": {
          "field": "type",
          "equals": "https://app.terraform.io/"
          
        }
        
      },
      "then": {
        "effect": "deny"
      }
    }
POLICY_RULE
}

resource "azurerm_policy_assignment" "policych" {
  name                 = "Required servive-principles"
  display_name         = "Allow thru SP"
  description          = " Require SP Authentication'"
  policy_definition_id = "${azurerm_policy_definition.policych.id}"
  scope                = "/subscriptions/xxxx-xxxx-xxxx-xxxx"
}
0

There are 0 answers