Search code examples
goterraformterratest

Concatenate declared variable with random string


It is possible, using Terratest, to declare a tfvars file with the following variable:

bar = {
  name   = "test"
  domain = "test.com"
  regions = [
    { location = "France Central", alias = "france" }
  ]
}

But include a random prefix to the bar.domain string inside the go code?

I'm using terraformOptions as follows:

terraformOptions := &terraform.Options{
        TerraformDir: sourcePath,
        VarFiles:     []string{variablesPath + "/integration.tfvars"},
}

Solution

  • It is not ideal for one to make use of the tfvars file directly to take the input in case of tests. More on this here

    To answer your question :

    You can use something similar to this :

    options := terraform.Options{
            TerraformDir: "sourcePath",
            Vars: map[string]interface{}{
                    "name":  "test",
                    "domain": addRandomprefix()+"test.com",
                    "region ":    map[string]interface{}{
                        "location" : "France Central",
                        "alias" : "france",
                    },
            },
        }
    

    Just create your own custom addRandomprefix() method. I hope this helps :)