Search code examples
amazon-web-servicesterraformtagsterragrunt

Terragrunt - dynamically add TF and TG versions to AWS default tags


I have a default tags block and would like to add new tags showing the TG and TF versions used in deployment.

I assumed this would work, but I was wrong..

locals {
  terraform_version      = "${run_cmd("terraform --version")}"
  terragrunt_version     = "${run_cmd("terragrunt --version")}"
}

provider "aws" {
  default_tags {
    tags = {
      terraform_version  = local.terraform_version
      terragrunt_version = local.terragrunt_version

      }
    }
  }

I'm sure there's a simple way to do this, but it alludes me.

Here's the error message:

my-mac$ terragrunt apply
ERRO[0000] Error: Error in function call
ERRO[0000] on /Users/me/git/terraform/environments/terragrunt.hcl line 8, in locals: 
ERRO[0000] 8:   terraform_version      = "${run_cmd("terraform --version")}" 
ERRO[0000]                                              
ERRO[0000] Call to function "run_cmd" failed: exec: "terraform --version": executable file not found in $PATH.
ERRO[0000] Encountered error while evaluating locals in file /Users/me/git/terraform/environments/terragrunt.hcl 
ERRO[0000] /Users/me/git/terraform/environments/terragrunt.hcl:8,31-39: Error in function call; Call to function "run_cmd" failed: exec: "terraform --version": executable file not found in $PATH. 
ERRO[0000] Unable to determine underlying exit code, so Terragrunt will exit with error code 1

Solution

  • Building on jordanm's good work, I found the TG version was good but I needed to remove some verbosity in the TF output for it to be usable as an aws tag.

    locals {
      terraform_version      = "${run_cmd("/bin/bash", "-c", terraform --version | sed 1q")}"
      terragrunt_version     = "${run_cmd("terragrunt", "--version")}"
    }
    

    Good work everybody!