What are we running - Tagging infrastructure in Azure

Asset Tagging is one of the best ways for providing run-time compliance and versioning information in cloud-based resources.  All of the major cloud providers provide the ability to annotate cloud resources with custom tags.  We reliably version/tag cloud resources if we follow the Infrastructure as Code philosophy.  All our infrastructure is created using scripts that call cloud APIs, or resource templates that can be loaded by cloud APIs.


Infrastructure versioning has both design time and run time components. The design-time pieces come into play at build and packaging time and when deploying.  The run time component is what lets us interrogate the component or the cloud metadata around that component. 

Video

Design-time infrastructure versions with IaC

IaC uses code or configuration files to drive provisioning. Those files can be checked into source code management systems where they can be branched and versioned like any other code. We can always query the VCS for the various branches and versions of our IaC code.

Run-time version interrogation: Tagging

Cloud resource tags are the simplest most flexible way of attaching metadata to cloud provisioned resources. Everyone should have a minimum set of standard tags that are attached to all cloud infrastructure components.  My personal set are:
  • UpdatedAt: the last time this infrastructure was updated via template or script.
  • Version:  The semantic version of the configuration scripts or templates that updated this resource.
  • Project: The project or team project code

Viewing Tags in the Portal

Almost every Azure resource can have tags. You can see the tags in the Azure Portal or via management API call.


Tagging with az tag create

We have our scripts tag resources at the time of creation or after updates.  I update the version and publishedAt tag every time I run an infrastructure script even if nothing changes on a particular resource.  I do that because individual components may not change during an infrastructure set update but I still want to know the last time they were "correct".
# re-tag every time we deploy
rg_metadata=$(az group list --query "[?name=='$AZURE_RESOURCE_GROUP']")
rg_id=$(jq -r ".[0].id" <<< "$rg_metadata")
tagging_metadata=$(az tag create --resource-id $rg_id --tags PublishedAt="$NOW_PUBLISHED_AT" Purpose="$PROJECT" Version="$VERSION")
# don't want to re-fetch but will to get the latest tags
rg_metadata=$(az group list --query "[?name=='$AZURE_RESOURCE_GROUP']")
echo "using resource group: $rg_metadata"

Tagging with an ARM template

We can tag resources as part of a template-based provisioning process.  Template engines will update individual tags even if no other changes were made to the tagged component.  We want this behavior because we often version at the infrastructure set level where an individual component may not actually be changed on every update.  We want to know the latest release this component was configured for.  So every template update brings in a later version and date that is applied to new and updated pieces.

       {
            "type": "Microsoft.EventHub/namespaces",
            "apiVersion": "2021-06-01-preview",
            "name": "[parameters('namespaces_namespace_name')]",
            "location": "[parameters('location')]",
            "tags": {
                "PublishedAt": "[parameters('lastPublishedAt')]",
                "Project": "[parameters('project')]",
                "Version": "[parameters('version')]"
            },
  }


Created 2022 01

Comments

Popular posts from this blog

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Almost PaaS Document Parsing with Tika and AWS Elastic Beanstalk