Multiple environment with terraform workspace

Hello Terraform users,

I’m new with terraform and I would like to use it to deploy and control infrastructure resources (AWS) of my customer for each of its environments. So I saw Terraform workspace to be able to switch between environment and to have a separate tfstate file for each of them. But, now I would like to know if we can exclude the deployment of a resource for a specific environment. Let’s take an example, Imagine, I have 3 environments : dev, staging and prod and I would like to deploy a S3 bucket for each environment and a documentDB instance for prod and one shared documentDB instance for dev and staging. How can I do this by using workspace with terraform? How can I exclude some resources for a specific workspace?

I saw that we can use conditionals with terraform but If I don’t make a mistake its working if we have a count property. But in my example DocumentDB have no count property so I can’t have a map for each environment and specify 1 if we want this resource and 0 if we don’t want.

If someone can help me to structure my project it’s will be really cool because I have never use terraform for a real project.

If you need more context about the environment of my customer I can give you more details or we can also discuss on slack on somewhere else.

Thanks you for our help,

Best regards,

Rayane.

Hi @rayanebel,

As described in When to use Multiple Workspaces, we (the Terraform team at HashiCorp) don’t recommend using multiple workspaces in your situation, and recommend instead using a separate configuration for each environment so that you can represent the differences between your environments in a straightforward and readable way, rather than introducing various conditional expressions throughout the configuration.

For the elements that are common between your environments, you can use shared modules to represent those and then call those modules in different ways (and, if necessary, in different combinations) for each environment. This pattern is an application of module composition.

A couple months ago I wrote a longer worked example in response to a Reddit question. That specific example might help to put the general advice from the documentation into context, even though your situation is using some different specific infrastructure types.