Professional Documents
Culture Documents
Terraform - 5 Ways To Create Infrastructure in Multiple Environments
Terraform - 5 Ways To Create Infrastructure in Multiple Environments
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one.
Member-only story
Listen Share
In this post, we will see the different ways of provisioning infrastructure in multiple
environments. Each of them has pros and cons.
Introduction
Prerequisite
Workspaces
Modules
Terragrunt
Summary
Conclusion
Introduction
Every Application goes through different environments before it is deployed into
production. It’s always best practice to have similar environments for consistency
purposes. It’s so easy to replicate the bugs and fix them easily. It’s not easy to
replicate the same set of infrastructure in each environment if we do that manually.
Terraform makes it easy to create infrastructure in a multi-cloud environment.
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 2/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Since the Terraform is an Infrastructure as a code tool you write your infrastructure
in code and it’s very easy to write for different environments in a modular way.
Prerequisite
If you are new to the Terraform and just getting started with it I would recommend
you go through the below article first.
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 3/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Each folder represents a separate environment and you can have a backend in each
folder as well and there is nothing common between these folders. You can have
outputs.tf, providers.tf, variables.tf files, etc in each folder. When you are running
terraform commands you have to navigate to the respective folder and run the three
commands init, plan and apply.
Using Folders
Pros
You can easily add or remove resources in each environment
Cons
Duplication of code
If you want to change the resource you have to change it in all environments.
Since we are maintaining the same main.tf, variables.tf files, when you are running
terraform commands you need to pass different variables based on the
environment. For example, if we have three environments these are the commands
you need to run for creating infrastructure.
// Dev Environment
terraform plan --var-file="tfvars/environment/dev.tfvars"
// QA Environment
terraform plan --var-file="tfvars/environment/qa.tfvars"
// Prod Environment
terraform plan --var-file="tfvars/environment/prod.tfvars"
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 5/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Using Folders
Pros
There is no duplication of code
If you want to change the resource you don’t have to change in all the
environments.
Cons
You can’t easily add or remove resources in each environment
Workspaces
Terraform starts with a single workspace named default. The workspace is special
because it is the default and also because it can’t ever be deleted. If you have never
explicitly used workspaces, then you have only ever worked on the default
workspace.
Workspaces are managed with the command terraform workspace. There are a set
of commands for the workspaces. For Example, You can use this command
terraform workspace new to create a new workspace.
Modules
A module is a container for multiple resources that are used together. Every
Terraform configuration has at least one module, known as the root module. This
root module usually consists of the resources defined in the files which have the
extension .tf in the main working directory.
A module can call other modules and these called modules have become child
modules to the calling modules. You can put a lot of resources in a particular
module you can use it in your main module in a concise way. You can use these
modules in a configurable way so that the same module can be used in different
environments without changing any code.
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 6/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
This is a combination of using folders and modules. You import the module into
your main file and you can pass respective input variables based on the
environment as depicted in the following figure.
Multiple Environments
Terragrunt
Terragrunt is a thin wrapper that provides extra tools for keeping your
configurations DRY, working with multiple Terraform modules, and managing
remote state.
You can get to know more about this tool on their official site below. I will write a
detailed post on this in future posts.
https://terragrunt.gruntwork.io/
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 7/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Summary
Terraform is an open-source infrastructure as a code software tool that provides
a consistent CLI workflow to manage hundreds of cloud services.
Using Folders is the most used one of all. There are two ways to do this.
A module is a container for multiple resources that are used together. You can
use these modules in a configurable way so that the same module can be used
in different environments without changing any code.
Terragrunt is a thin wrapper that provides extra tools for keeping your
configurations DRY, working with multiple Terraform modules, and managing
remote state.
Conclusion
These are different ways to create infrastructure using Terraform in different
environments. We have to mix up these ways to create different environments and
has their own pros and cons. In future posts, I will write detailed posts on all these
ways in multi-cloud environments.
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 8/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Follow
1.5K 37
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 9/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
705 9
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 10/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
365 2
2K 12
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 11/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
101 6
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 12/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Flavius Dinu
334
Lists
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 13/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Flavius Dinu
312 2
Flavius Dinu
Once upon a time, if you worked in the IT industry, there was a big chance you faced different
challenges when provisioning or managing…
344 1
Deploy WordPress and MySQL with Terraform on AWS EC2 Like a Pro
Prerequisites:
25 2
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 15/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium
Flavius Dinu
344
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 16/16