-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terraform testing framework: Dynamically set object properties from variables when testing modules #34534
Comments
Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again! |
Where do you expect |
Where we define the variables is not that important for me as long as this can be possible somehow. One solution can be to create a variable file like |
While looking into this I found a workaround that I though might be worth sharing:
run "tfe_create_workspace" {
variables {
tfe = merge(var.tfe, { organization_name : var.org_name })
}
module {
source = "./"
}
#...
} It's probably worth fixing this anyways by making an extra scope / precedence level for externally configured vars, but this workaround works today on Terraform 1.6.0 and above. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Terraform Version
Use Cases
Background
My team and I are working on a Terraform module (https://github.com/blinqas/station) that facilitates the setup of projects and workspaces in Terraform Cloud, in conjunction with managing associated GitHub repositories (holds TF code for each workspace) and Azure resources associated with each workload.
Example use case:
If someone needs to deploy a new application in azure, the platform team can just call the module and it will create:
The application team can then create add their own Terraform resources in the new repository to deploy the infrastructure needed for their application. This makes it much faster to deploy new resources and the platform team has full control over the permissions given to each project.
Current issue
One of the core components of our module is the
tfe
block that creates a new workspace for each new workload. This is where we define essential attributes like organization, project name, workspace name, TF/ENV variables, etc. In the current implementation, these details are provided as variables to the module.Example
To increase flexibility and avoid hard-coding sensitive information (like the TFC org name) when writing the tests, I am exploring the possibility of dynamically setting the
organization_name
within thetfe
block, ideally through an environment variable. However, I've encountered a limitation: it seems that the Terraform testing framework does not currently allow for referencing a variable within the variable block. This limitation is a significant hurdle, as it restricts our ability to write flexible and reusable tests.A solution to this is to set the whole tfe block as an environment variable but this is kinda pointless as the test will be much less readable and we would have to wrap the testing in a bash/python script...
Current test for the TFE block
Refactoring the module would be our last resort because of how time-consuming it would be to update it for all of our clients.
Since the module also is open source and other people can contribute, it would be a hassle for others who want to perform tests against they're own TFC org. They would then have to update all the test files with their org name, test and revert the changes before making a PR.
I have already been in contact with Omar Ismail at Hasicorp regarding this issue and he suggested that we created an issue on this.
Attempted Solutions
It is not possible to reference a variable inside a variable. I have also tried to reference a local block where I pass in the value from a variable to the local block.
Proposal
It would be very helpful for us and other who have structured their module to use blocks to be able to update parts of the block using variables like so:
References
No response
The text was updated successfully, but these errors were encountered: