Home > front end >  Is there a way to append tags, instead of replace them, when deploying resources with BICEP?
Is there a way to append tags, instead of replace them, when deploying resources with BICEP?

Time:12-20

I am trying to add tags to a Bicep deployment so i can see who or what deployed a resource. I notice however that existing tags get replaced when i use the tags.

param lastDeployedBy string = 'deliverypl'

param deployementDateTime string = utcNow('dd-MM-yyyy HH:mm')

var resourceTags = {
  LastDeployedBy: lastDeployedBy
  LastDeployedDateUTC: deployementDateTime
}

resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: resourceGroupName
  location: location
  tags: resourceTags
}

Is there a way to append the tags from bicep, or should i create a script to do this?

  • Created a test resourcegroup with the bicep code.
  • Added some manual tags to the resourcegroup
  • Ran the bicep deployment again. = Manual tags are removed.

I would like it to append the tags added from the bicep deployment.

CodePudding user response:

Firstly, I've deployed a bicep template for adding below tags as shown:

enter image description here

To append/concat, Use Union operator for object as well as string params.

I've taken your script and modified as follows to append the new tags to the existed one's.

vi main.bicep:

@description('Specifies the location for resources.')
param  location  string = 'East US'
targetScope = 'subscription'
param  lastDeployedBy  string = 'deliverypl'
param  deployementDateTime  string = utcNow('19-12-2022 17:05')
var  resourceTags = {
LastDeployedBy: lastDeployedBy
LastDeployedDateUTC: deployementDateTime
}
resource  resourceGroup  'Microsoft.Resources/resourceGroups@2021-04-01' = {
name: 'new'
location: location
tags: union(resourceTags,{
value : 'Appended'
})
}

Build & Deployment succeeded:

enter image description here

Added value:Appended tag to the existed tags within the scope/resourcegroup:

enter image description here

  • Related