Hi Vishal P
Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.
First, identify the resource provider and associated resource types for Savings Plans. If Azure offers a specific resource type for Savings Plans, you can target them directly.
Create a policy that blocks the creation of the identified resource types, unless the subscription is the one you wish to permit.
Assign this policy to the root management group or to all management groups, excluding the one that contains the Prod subscription.
resource "azurerm_policy_definition" "restrict_savings_plan" {
name = "restrict-savings-plan-creation"
policy_type = "Custom"
mode = "All"
display_name = "Restrict Savings Plan Creation to Prod Subscription"
description = "This policy restricts the creation of Savings Plans to the designated Prod subscription."
policy_rule = <<POLICY_RULE
{
"if": {
"allOf": [
{
"field": "type",
"equals": "<SavingsPlanResourceType>"
},
{
"not": {
"field": "subscriptionId",
"equals": "<ProdSubscriptionId>"
}
}
]
},
"then": {
"effect": "deny"
}
}
POLICY_RULE
}
resource "azurerm_policy_assignment" "restrict_savings_plan_assignment" {
name = "restrict-savings-plan-assignment"
policy_definition_id = azurerm_policy_definition.restrict_savings_plan.id
scope = "/providers/Microsoft.Management/managementGroups/<ManagementGroupId>"
}
Replace <SavingsPlanResourceType> with the correct resource type for Savings Plans. You can check Azure's documentation or use Azure Resource Graph to find it.
Replace <ProdSubscriptionId> with the subscription ID for your production subscription, where Savings Plans are allowed.
Replace <ManagementGroupId> with the ID of the management group where you want to apply the policy, or use the tenant root for a broader scope.
Note:
If Azure doesn't provide a specific resource type for Savings Plans that can be controlled by policies, you can use alternatives like role-based access control (RBAC) or custom scripts to enforce the policy.
Make sure to test the policy in a non-production environment first to confirm it works as expected before applying it to the whole system.
let us know if any help, we will always help as you needed.!
Please do not forget to "Accept the answer” and upvote it wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.