Remote AzureRM backend shared with multiple modules


Firstly I apologise as I am new with Terraform.

I am struggling to understand how to best configure a remote backend state file for multiple modules.

  1. I created an Azure storage account and container
  2. I configured the first module to use the remote backend
  3. I configured a second module to use the same remote backend

But when I try run terraform plan it is wanting to delete the resources created by the first module because it says they are not in the configuration of the second module.

What am I missing? How do you create resources that use a remote backend without having a state file for each module and set of resources you create?

Hi @Terranoob,

Each separate configuration you create must have a separate backend configuration, and therefore a separate Terraform state. Otherwise, as you have found, Terraform will think that you intend to modify the objects managed by your first configuration to match the second configuration, because Terraform is a “desired state” system and so it does its work by comparing the existing state with the desired state described in your configuration.

How can a module be shared across teams? If I have a Module that creates a web app and database. For me to run this I have to create a backend configuration. How can this module be shared with another team so they can create a web and database?

Since it has the backend configured when they run it , it will have a conflict of state to what I created, to what they want to create. Wouldn’t they have to duplicate the module then change the backend configuration? This isn’t really repeatable/re-usuable

Or should re-usable modules not contain any backend configuration? Once a backend if configured it becomes a project , not a module???

When we use the term “shared module” in Terraform that typically means a module intended to be called using a module block from another module. A shared module in that sense should not include a backend configuration, because it’s the root module’s responsibility to configure that.

The root module (the one in the same directory where you run terraform apply) should not typically be shared in the sense I think you mean. Instead, each of your teams can have its own root module which includes backend configuration, provider configurations, and then a module block calling a shared module you maintain separately.

1 Like

Thanks this makes more sense.