HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaterraformMinor

What is the difference between modules and workspaces in Terraform

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
modulesthewhatdifferenceworkspacesbetweenandterraform

Problem

I currently use Terraform workspaces to configure different environments like dev, test, prod, and my code works fine. And I believe I can use workspaces to configure different regions as well.

However, I think Terraform modules can do the same thing, right?

If that's so, when should I use workspaces and when should I use modules? Or they are doing a similar thing in different ways?

Solution

I think a key difference between Terraform Modules and Workspaces is that modules can be inherited and used by other modules and configurations. Terraform Workspaces are intended to be a collection of configurations that represent a single environment, whereas Modules are components that can be utilized by one or more modules/configurations. I think you could use Modules in a similar way to how you are using Workspaces, but you would be contradicting the intent of the Modules.

For example, say that you want to create a base configuration file for all your company's EC2 instances that allows traffic over SSH. You would create a module that spins up a VM, modifies your firewall to allow SSH, and any other configuration settings that you would want all of your instances to have. You would then have every VM Terraform config inherit this file. This would minimize the amount of code that you would have to write and allow for any changes that you would want across all of your TF configs to be made in one central module.

Context

StackExchange DevOps Q#3181, answer score: 4

Revisions (0)

No revisions yet.