Last active
June 3, 2020 20:57
-
-
Save brucedkyle/38401d8640eae1d76469d2ab6f4efa53 to your computer and use it in GitHub Desktop.
Naming Convention for Azure Resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[@]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Get-AzResourceGroup -Tag @{ "Cost Center"="AzDays" }).ResourceGroupName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TAG="Cost Center=AzDays" | |
# az group list --tag supports a single tag only | |
az group list --tag "$TAG" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sample resource group name | |
rg-wu2-prod-azdays-01 | |
# sample virtual network name | |
vnet-wu2-prod-azdays-01 | |
# sample strorage account name | |
stwu2prazdays89304 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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