gitterraformterraform-provider-azureterraform-template-file

Terraform :: how to use WHOAMI as a tag


We are using Terraform to manage our infrastructure.

One of the tags that we use is called updatedBy and it's supposed to show the e-mail of the person that updated that resource:

  tags = {
    "updatedBY" = "name.surname@contoso.com"
  }

As you can imagine so many times people input the wrong e-mail.

Is there a way I can put that e-mail in a variable using:

In my opinion git config user.email should be the way to go because it's OS agnostic and all our Terraform engineers use Git.

But how to put that into a variable so they cannot be wrong again?


Solution

  • Use the AzureRM Client Config provider and the AADUser provider to update with the Deploying Users UPN

    data "azurerm_client_config" "current" {}
    
    data "azuread_user" "current_user" {
      object_id = data.azurerm_client_config.current.object_id
    }
    
    resource "azurerm_resource_group" "rg" {
      name = var.resourceGroupName
      location = var.location
      tags = {
        "updatedBY" = data.azuread_user.current_user.user_principal_name
      }
    }