gruntwork-io / terratest

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.
https://terratest.gruntwork.io/
Apache License 2.0
7.4k stars 1.32k forks source link

How to pass variables to a module in terratest #564

Open skadem07 opened 4 years ago

skadem07 commented 4 years ago

How do we pass values to a module , I remember having terratest work without having to define a variables.tf file inside root-module. But now it fails if i don't define a variables.tf file in root-directory. Am I missing something, or its mandatory that we should pass the variables.tf in root-directory?

My directory structure looks liks tf_acm_module github repo

package test

import(
    "testing"
    "os"
    "github.com/gruntwork-io/terratest/modules/aws"
    "github.com/gruntwork-io/terratest/modules/logger"
    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestTerraformAwsNetworkExample(t *testing.T) {
    t.Parallel()

    logger.Log(t, "This will show up in stdout immediately")
    os.Setenv("TERRATEST_IAM_ROLE", "arn:aws:iam::xxxxx:role/role")
    awsRegion := "us-east-1"
    PrimaryDomain := "module.foo.com"
    SubAltDomNames := []string{"*.foo.com","bar.dev"}
    ValidMethod := "DNS"
    Tag := map[string]string{"owner":"xxx"}
    terraformOptions := &terraform.Options{
        TerraformDir: "terraform/",
        Vars:  map[string]interface{} {
            "primary_domain": PrimaryDomain,
            "subjective_alternative_names": SubAltDomNames,
            "validation_method": ValidMethod,
            "tags": Tag,
        },
        EnvVars: map[string]string{
            "AWS_DEFAULT_REGION": awsRegion,
        },
    }
    defer terraform.Destroy(t, terraformOptions)
    terraform.InitAndApply(t, terraformOptions)
    acmARN := terraform.Output(t, terraformOptions, "acm_arn")
    ttACMARN := aws.GetAcmCertificateArn(t,awsRegion,PrimaryDomain)
    assert.Equal(t,acmARN,ttACMARN)
}

Error:

TestTerraformAwsNetworkExample 2020-06-24T23:31:19-04:00 logger.go:66: Error: Value for undeclared variable
TestTerraformAwsNetworkExample 2020-06-24T23:31:19-04:00 logger.go:66:
TestTerraformAwsNetworkExample 2020-06-24T23:31:19-04:00 logger.go:66: A variable named "primary_domain" was assigned on the command line, but the
TestTerraformAwsNetworkExample 2020-06-24T23:31:19-04:00 logger.go:66: root module does not declare a variable of that name. To use this value, add a
TestTerraformAwsNetworkExample 2020-06-24T23:31:19-04:00 logger.go:66: "variable" block to the configuration.
TestTerraformAwsNetworkExample 2020-06-24T23:31:19-04:00 logger.go:66:

TestTerraformAwsNetworkExample 2020-06-24T23:31:19-04:00 logger.go:66:

brikis98 commented 4 years ago

Any variables you put in Vars within terraform.Options get passed to your Terraform module via -var arguments. As of Terraform 0.12, if you pass -var foo=bar to a module, then it must have a variable "foo" declared within it.