Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brucedkyle
Last active June 3, 2020 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucedkyle/38401d8640eae1d76469d2ab6f4efa53 to your computer and use it in GitHub Desktop.
Save brucedkyle/38401d8640eae1d76469d2ab6f4efa53 to your computer and use it in GitHub Desktop.
Naming Convention for Azure Resources
REGION_ABBR="wu2"
ENVIRONMENT="dev"
PROJECT="azdays"
ITERATION="01"
LOCATION="West US 2"
RESOURCE_GROUP_NAME=rg-${REGION_ABBR}-${ENVIRONMENT}-${PROJECT}-${ITERATION}
TAGS=("Cost Center=AzDays" "Location=West US 2")
az group create --name $RESOURCE_GROUP_NAME --location "$LOCATION" --tags "${TAGS[@]}"
# get resource groups with a tag which does not have spaces
az group list --tag Environment=Dev -o json
# get a list of resources with a tag without spaces
az resource list --tag Environment=Dev -o json
(Get-AzResourceGroup -Tag @{ "Cost Center"="AzDays" }).ResourceGroupName
TAG="Cost Center=AzDays"
# az group list --tag supports a single tag only
az group list --tag "$TAG"
$REGION_ABBR = "wu2"
$ENVIRONMENT = "dev"
$PROJECT = "azdays"
$ITERATION = "01"
$LOCATION = "West US 2"
$RESOURCE_GROUP_NAME = "rg-" + $REGION_ABBR + "-" + $ENVIRONMENT + "-" + $PROJECT + "-" + $ITERATION
$TAGS = @{ "Cost Center" = "AzDays"; "Location"=$LOCATION }
# new resource group
New-AzResourceGroup -Name $RESOURCE_GROUP_NAME -Location $LOCATION -Tags $TAGS
# sample resource group name
rg-wu2-prod-azdays-01
# sample virtual network name
vnet-wu2-prod-azdays-01
# sample strorage account name
stwu2prazdays89304
# adapted from https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources#handling-spaces
RESOURCE_GROUP_NAME='rg-wu2-prod-azdays-01'
jsontags=$(az group show --name $RESOURCE_GROUP_NAME --query tags -o json)
tags=$(echo $jsontags | tr -d '{}"' | sed 's/: /=/g' | sed "s/\"/'/g" | sed 's/, /,/g' | sed 's/ *$//g' | sed 's/^ *//g')
origIFS=$IFS
IFS=','
read -a tagarr <<< "$tags"
resourceids=$(az resource list -g $RESOURCE_GROUP_NAME --query [].id --output tsv)
for id in $resourceids
do
az resource tag --tags "${tagarr[@]}" --id $id
done
IFS=$origIFS
$RESOURCE_GROUP_NAME='rg-wu2-prod-azdays-01'
$RESOURCE_NAME='vnet-wu2-prod-azdays-01'
$resource = Get-AzResource -ResourceName $RESOURCE_NAME -ResourceGroupName $RESOURCE_GROUP_NAME
$resource | ForEach-Object { Update-AzTag -Tag @{ "Cost Center"="AzDays"; "Environment"="Dev" } -ResourceId $_.ResourceId -Operation Merge }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment