Allow deployments only from Terraform using Azure Policy

Logan 40 Reputation points
2025-03-26T14:00:40.9233333+00:00

Hi Team,

Is it possible to block all deployments and allow it only from Terraform? I tried this but it didn't work.

{
          "not": {
            "anyOf": [
              {
                "field": "Microsoft.Authorization/roleAssignments/principalId",
                "equals": "[parameters('allowedServicePrincipal')]"
              }
            ]
          }
        }
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,014 questions
{count} votes

Accepted answer
  1. SadiqhAhmed-MSFT 49,326 Reputation points Microsoft Employee Moderator
    2025-05-08T19:47:15.7633333+00:00

    @Logan Thank you for reaching out to us and appreciate your patience during the support tenure through Microsoft Q&A platform.Sharing the summarized answer to help other community members/users looking for similar solution.

    Question: Allow deployments only from Terraform using Azure Policy

    Solution: You could leverage identity-based exemptions, which is currently in private preview, to achieve this scenario. With this, you’d be able to apply a policy to prevent resource creation or modification, then create an exemption for that assignment which would allow the Terraform pipeline’s Service Principal ID to bypass the policy.

    So, you’ll first need to create the policy definition which blocks storage account creation, then assign it to the desired scope. Once that assignment is in place, then you’ll create an exemption for that assignment which uses selectors to specify allowed/disallowed user/object ID(s).

    Also, in your selectors, it is not necessary to use both the “in” and “notIn” restrictions – this one is more restrictive and can be used by itself: "kind": "userPrincipalId", "in": ["1234-1234"]

     [create block all users E.g group1 and create exemption for specific users] is the correct understanding. In its current private preview state, identity-based exemptions through resource selectors are not supported in portal. Try creating the exemption through an API call.

    You should be able to create the exemption using an API request URI & request body like this (be sure to first assign the policy, then insert real values for the placeholders shown): 

    PUT https://management.azure.com/{scope}/providers/Microsoft.Authorization/policyExemptions/{policyExemptionName}?api-version=2022-07-01-preview

    User's image

    Note: The feature is fully supported at the API level but remains in private preview while we collect feedback on the experience and complete the necessary steps to release it in public preview or GA, including portal support. We don’t currently have an ETA for these releases, but we will be reaching out to customers using the private preview to keep you informed, and you can keep up with product announcements through blog posts.


    If the response helped, do "Accept Answer" and up-vote it

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.