How to re-use a module several times in the same `main.tf`?

Viewed 2211

I created a module for a re-usable piece of infrastructure. The module is a project, so each time we want to create a new project and the related infrastructure items, we could use this module:

module "project1" {
source = ".modules/project_module"
    project_id = "project1"
    ...
}

module "project2" {
source = ".modules/project_module"
    project_id = "project2"
    ...
}

The module uses the google provider to create resources on GCP.

Unfortunately, this didn't work as hoped. First, each new project requires to invoke terraform init and second, it is impossible to remove a project, because when removing a module from the main.tf file, Terraform complains that without the Google provider it cannot destroy resources. For example:

module.project1.google_storage_bucket_iam_member.some-bucket: 
configuration for module.project1.provider.google is not present; a provider configuration block is required for all operations

Is there a way to use several times the same module in the same main.tf? I realize that ideally I should write a provider, but I would like to avoid that for now.

1 Answers

It turned out there was something inconsistent in the state. Burning the end, after re-creating the project from scratch while keeping the Google provider outside of the module, it worked.

Related