6

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?

2 Answers 2

4

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.

4
  • Thanks for the answer. what does "but you would be contradicting the intent of them." mean? I am configuring different environments like dev, test, prod. in general, is workspace the right way or module the right way?
    – user389955
    Commented Jan 25, 2018 at 20:01
  • @user389955 corrected for a little more clarity. If you used modules in the way you are using right now, you would not be using them as they were intended. Modules are not intended to be single environment configurations, but are meant to be shareable components utilized by other TF configurations. IMHO, it would be like using a build system to deploy to your artifacts. You certainly CAN do it, but it's not the intent of the system. I think you are currently using Workspaces as they were intended.
    – Preston Martin
    Commented Jan 25, 2018 at 20:12
  • @ PrestonM: my understanding is that you think my current way (using workspace to configure dev, test, prod env) is better than using module. is it correct? thanks.
    – user389955
    Commented Jan 26, 2018 at 1:01
  • @user389955 Yes, I believe you are using them correctly. I have added an example into my original answer of a use case for a module.
    – Preston Martin
    Commented Jan 26, 2018 at 14:53
4

Terraform modules are a way to encapsulate reusable business logic in order to be DRY. For example, you might use a module to define a jenkins setup, then invoke that module every time you want a jenkins server. In other words, you can instantiate modules as many times as you need them to achieve the same outcome.

On the other hand, Terraform workspaces are a way to isolate terraform state between environments. For example, you don't want to mingle state between a production and staging environments.

In practice, we highly recommend writing modules for everything. This is why we have over 100+ modules. Writing modules is like writing "functions" in other languages. Generally, it's considered a bad practice to stick all logic in a single function or scope; modules help us avoid that.

Workspaces are nice in principle, but in practice we recommend to share nothing between production and staging environments, including terraform state. Workspaces violate this principle. Thus we architect things so that production state remains in the production account and staging state in the staging account. We accomplish this with our terraform-aws-tfstate-backend module.

———

For the record, “we” refers to Cloud Posse, a DevOps professional services company. I am the founder. The views expressed are our own and may not represent the views of the community at large. :)

1
  • 1
    As you're using we along the answer, you may wish to drop a line about who is 'we' and you relation to it.
    – Tensibai
    Commented Jul 27, 2018 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.