So, we have a repo with several terraform.tfvars files. Each one has all the values for the environment it is deploying into. These environments are identical and it is a business requirement, rather than JUST a technical one.
The pipeline runs the deployments in parallel, each path using 1 of the terraform.tfvars files.
To date, good or bad, this has worked well for us. Admittedly, it was a supplied “solution” that we never changed.
As part of upgrading to Terraform v1.5, we would like to use the import {} feature. What we have found that our current setup is not ideal for this. In fact, terraform import would require a developer to do the work on their host, and so not be peer-reviewed in the way we want it all to be.
By using import {}, combined with variables, we can do this sort of thing:
variable "id_of_the_thing_being_imported" {
description = "The ID of the thing being imported"
type = string
}
import {
to = aws_resource.imported
id = var.id_of_the_thing_being_imported
}
And then each terraform.tfvars file has the appropriate entry:
live_1_terraform.tfvars:id_of_the_thing_being_imported="id-one"live_2_terraform.tfvars:id_of_the_thing_being_imported="id-two"
And this is sort of where we’re stuck.
Firstly, the resources are really managed by modules, so module.some_name.resource_type.another_name is the more usual pattern.
Secondly, when we run terraform plan, we add the -generate-config-out= option. We can add a unique filename for each environment, but the pipelines run a clean checkout and so handling that file cleanly for each environment back to the repository would result in the resources from all the environments being hard-coded into the repo.
Thirdly, where does moved {} come in with regards to import {}. Can we combine them in some way to help our situation?
What is the “expected” solution here?
We use IaC via VCS rather than have developers do the terraform applies from their hosts. Peer-review of code changes is important.
How should we setup parallel setups. The idea of creating multiple repos for each environment when the only real differences would be terraform.tfvars files, is just increasing the maintenance time for no real gain (at the moment).
Currently, the approach we’ve used for terraform import is to suspend the pipeline and have a trusted administrator do the work on their host, and when all the imports have been done locally, the local plan should result in no additional changes, and then that’s pushed to master (without the pipeline running), a plan-only PR is made and verified as being a no-op and then merged. It is a lot of work.
From what we can tell, the import {} block is really for doing some of the work we currently do manually, not a full “one button” solution.
Is there anything we’re missing that can simplify the work?