Forum Discussion

Aaida_Aboobakkar's avatar
Mar 13, 2025

Restrict Cost Consumption by using Azure Automation, Budget and Policy

Video

See the demo video by using below link

Demonstration Video

Automation Runbook Logic

 

Logic which set tag value once threshold exceeds

# Authenticate using Managed Identity (recommended for Automation Accounts)

Connect-AzAccount -Identity

 

# Define Subscription ID and Reset Tag

$subscriptionId = (Get-AzContext).Subscription.Id

$tags = @{ "cost exceeded" = "yes" }  # Resetting the tag value

 

# Update the tag

Update-AzTag -ResourceId "/subscriptions/$subscriptionId" -Tag $tags -Operation Merge

 

Write-Output "Tag 'cost exceeded' reset to 'yes' for subscription $subscriptionId"

Logic which reset tag value every month

# Authenticate using Managed Identity (recommended for Automation Accounts)

Connect-AzAccount -Identity

 

# Define Subscription ID and Reset Tag

$subscriptionId = (Get-AzContext).Subscription.Id

$tags = @{ "cost exceeded" = "no" }  # Resetting the tag value

 

# Update the tag

Update-AzTag -ResourceId "/subscriptions/$subscriptionId" -Tag $tags -Operation Merge

 

Write-Output "Tag 'cost exceeded' reset to 'no' for subscription $subscriptionId"

Azure Policy Logic

 

{

  "properties": {

    "displayName": "budget",

    "policyType": "Custom",

    "mode": "All",

    "metadata": {

      "version": "1.0.0",

      "createdBy": "f6bb4303-e52d-4cba-9790-01f0798164b7",

      "createdOn": "2025-03-13T05:08:05.8483517Z",

      "updatedBy": "f6bb4303-e52d-4cba-9790-01f0798164b7",

      "updatedOn": "2025-03-13T06:32:35.1740944Z"

    },

    "version": "1.0.0",

    "parameters": {},

    "policyRule": {

      "if": {

        "allOf": [

          {

            "field": "type",

            "notEquals": "Microsoft.Resources/subscriptions"

          },

          {

            "value": "[subscription().tags['cost exceeded']]",

            "equals": "yes"

          }

        ]

      },

      "then": {

        "effect": "Deny"

      }

    },

    "versions": [

      "1.0.0"

    ]

  },

  }

No RepliesBe the first to reply

Resources