Add a Tag when creating an Azure Resource Group

I have an Azure Automation Runbook, see post Clean up Azure Resource Groups with a Tag, that deletes Azure Resource Groups every night based on a tag.

Instead of typing the tag name every time, when creating new Resource Groups, I use a script that adds the tag and the location of the Resource Group.

To use the script, use Azure Cli in Azure or on a Linux host by running:

.\create_resourcegroup_with_tag.sh -g myrg

Download the script from GitHub or copy it from below.

# Creates a resource group with a tag
# Example: .\create_resourcegroup_with_tag.sh -g myrg

# Variables to be set when creating the resource group
location="westeurope"
tagname="RemoveResourceGroup"
tagvalue="Yes"

# input section
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -g|--resourcegroup)
    rg="$2"
    shift
    shift
    ;;
    -h|--help)
    echo "Please add -g for Resource Group Name" >&2; exit 1
    ;;
    -*) echo "unknown option: $1" >&2; exit 1;;
    *) handle_argument "$1"; shift 1;;
esac
done

if [ "$rg" == "" ]
    then
        echo "Missing argument Resource Group Name"
        exit 1
fi

az group create -n "$rg" -l "$location" --tag "$tagname"="$tagvalue"

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.