Custom Azure Policy :: Require a tag and its specific values on Resource Groups

Require a tag and its specific values on Resource GroupsAlright, let’s face it, Azure Policies are not easy to setup.

Why you say that?

Because Policies and Tags in general they look like the half baked products that Microsoft didn’t want to release.
But do you want to know what is my favorite Azure Policy?

No, tell me, please.

It’s Require a tag on resource groups. Without it you cannot really do FinOps.


Because when you want to track the consumption of a customer or a specific product I bet you go for the biggest offenders: SQL Databases, App Services, VMs, etc…

Yes, and what’s wrong with that?

Most of the time people forget that there is a galaxy of other small resources that fluctuate around biggest offenders: Microsoft Defender, Log Analytics, Virtual Networks, Network Watcher, etc…

Yeah, but they don’t cost much, right?

Well, it’s true, it’s just $16 per Resource Group.

You see!

But when you have 400 Resource Groups it can account for $6’400 at the end of the month.
…and then the CFO comes to your desk with a stick in his hands.

So how to avoid this?

By using a good combination of tagging and policy. Or tagging through policies.

And why Require a tag on resource groups is not enough?

Because sometimes people just put “Hello World” in the tag and the tag becomes unusable.
You cannot track money any more.
What you need is my Custom Azure Policy…

Require a tag and its specific values on Resource Groups

But Azure already has a Policy which name sounds very similar: Require a tag and its value on resource groups

Yes, but that is limited to 1 value.
We want to have a list of values!
For example let’s say that we want to allow our user to only put on the tag myTag this list of values:

  • Apple
  • Banana
  • Kiwi
  • Mango
  • Peach

Everything else will not be allowed.

How do you do that?

You have to create a new Policy definition with this code:

And voilà, you are good to go.

Any problems with Terraform?

Yes, you need to coordinate Terraform accordingly otherwise they will compete and overwrite one each other.



Leave a Reply

Your email address will not be published. Required fields are marked *