Skip to content

Ability to manage values for variables #55

@baolsen

Description

@baolsen

Can be assigned to me, I would just like some feedback on the issue / approach first.

It would be good to specify and override values for Terraform variables when testing.

Currently we don't see a good way to do this with pytest-terraform.

Apparent options:

  • Setting os.environ[TF_VAR_x] = "foo" before the test seems to work in some cases and not in others. It's a bit flaky.
  • Writing terraform.tfvars to the tests/terraform/my_test folder and exclude it from source control. This works but is a bit clunky as we need to ensure variable values are written correctly to the file, eg if they are of complex type.

The specific use case we have involves providing at least a value for the AWS profile that the Terraform AWS provider needs to use at run time, so that we can authenticate to the correct environment.

pytest --profile my_ci_account_profile
=> We add a fixture called "profile" to read in this value
=> We write terraform.tfvars with profile = "my_ci_account_profile"

Our terraform test code:

# variables.tf:
variable "profile" {
   description = "AWS profile"
   type = string
}

# providers.tf:
provider "aws" {
  region = "us-east-1"
  profile     = var.profile # Dynamically use an AWS profile at runtime
}

My particular use case is complicated because the profile is only known at runtime.

Perhaps something like this could work, for our use case:

@terraform("main", scope="session", variables={profile=os.environ.get["profile"]})

Perhaps this feature could even use pytest parameterize to iteratively test values of variables under different scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions