Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium

You have 2 free member-only stories left this month. Sign up for Medium and get an extra one.

Member-only story

Terraform — 5 Ways To Create Infrastructure in


Multiple Environments
Exploring Different ways of creating infrastructure in different environments with
Terraform

Bhargav Bachina · Follow


Published in Bachina Labs
5 min read · Jan 5, 2021

Listen Share

Terraform is an open-source infrastructure as a code software tool that provides a


consistent CLI workflow to manage hundreds of cloud services. Terraform codifies
cloud APIs into declarative configuration files.
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 1/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium

Most of the time we deploy the infrastructure in multiple environments. We need


these environments for the development, stage, testing, and production. It’s very
important to write a maintainable and scalable Terraform configuration that should
allow us to provision infrastructure on these different environments.

In this post, we will see the different ways of provisioning infrastructure in multiple
environments. Each of them has pros and cons.

Introduction

Prerequisite

Using Folders — Method 1

Using Folders — Method 2

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.

We will see the possible ways of creating infrastructure in multiple environments in


the following sections.

Prerequisite
If you are new to the Terraform and just getting started with it I would recommend
you go through the below article first.

How To Get Started With Terraform

Using Folders — Method 1


In this method, we duplicate the same infrastructure in each folder with different
values in the terraform.tfvars file. This is not ideal when you have the same
infrastructure in all environments.

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

Changes in one environment don’t affect other environments

Cons
Duplication of code

If you want to change the resource you have to change it in all environments.

Using Folders — Method 2


In this method, we maintain the same infrastructure in common files but have
different terraform.tfvars in each environment. This is not ideal when you have the
https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 4/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium

different infrastructure 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

Changes in one environment do affect other environments since we are using


the same files with different var files.

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.

Most of the time we deploy the infrastructure in multiple environments. We


need these environments for the development, stage, testing, and production.

There are 5 ways to write reusable code for different environments in


Terraform.

Using Folders is the most used one of all. There are two ways to do this.

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.

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.

Terraform DevOps Programming Web Development Software Development

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

Written by Bhargav Bachina


11.2K Followers · Editor for Bachina Labs

Entrepreneur | 600+ Tech Articles | Subscribe to upcoming Videos


https://www.youtube.com/channel/UCWLSuUulkLIQvbMHRUfKM-g | https://www.linkedin.com/in/bachina

Open in app Sign up Sign In


More from Bhargav Bachina and Bachina Labs

Bhargav Bachina in Bachina Labs

250 Practice Questions For Terraform Associate Certification


Read and Practice these questions before your exam

· 47 min read · Jul 13, 2020

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

Bhargav Bachina in Bachina Labs

React — How To Proxy To Backend Server


Explaining how to configure a proxy for backend API calls with an example.

· 6 min read · Jun 13, 2020

705 9

Bhargav Bachina in Bachina Labs

Kubernetes — Learn Sidecar Container Pattern

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

Understanding Sidecar Container Pattern With an Example Project

· 6 min read · Sep 7, 2020

365 2

Bhargav Bachina in Bachina Labs

How To Implement Micro-Frontend Architecture With Angular


Everything you need to know about microservice oriented architecture for the frontend from
beginner to advanced

· 8 min read · Jan 10, 2020

2K 12

See all from Bhargav Bachina

See all from Bachina Labs

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

Recommended from Medium

Wenqi Glantz in Better Programming

DevOps Self-Service Centric Terraform Project Structure


How to structure Terraform code and its reusable modules

· 8 min read · Jan 11

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

Terraform from 0 to hero — 4. Data Sources and Outputs


Terraform resources are great and you can do a bunch of stuff with them. But did I tell you can
use Data Sources and Outputs in…

· 3 min read · Jan 22

334

Lists

General Coding Knowledge


20 stories · 25 saves

It's never too late or early to start something


10 stories · 7 saves

Coding & Development


11 stories · 11 saves

Stories to Help You Grow as a Software Developer


19 stories · 149 saves

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

Terraform from 0 to Hero — 14. Modules


Terraform modules are one of the most important features that Terraform has to offer. They
make your code reusable, can be easily…

· 5 min read · Mar 21

312 2

Flavius Dinu

Terraform from 0 to hero — 1. What the heck is Terraform?


https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 14/16
6/27/23, 9:59 AM Terraform — 5 Ways To Create Infrastructure in Multiple Environments | by Bhargav Bachina | Bachina Labs | Medium

Once upon a time, if you worked in the IT industry, there was a big chance you faced different
challenges when provisioning or managing…

· 3 min read · Jan 15

344 1

headintheclouds in AWS Tip

Deploy WordPress and MySQL with Terraform on AWS EC2 Like a Pro
Prerequisites:

· 14 min read · Jan 3

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

Terraform from 0 to hero — 5. Variables and Locals


Terraform variables and locals are used to better organize your code, easily change it, and
make your configuration reusable.

· 5 min read · Jan 24

344

See more recommendations

https://medium.com/bb-tutorials-and-thoughts/terraform-5-ways-to-create-infrastructure-in-multiple-environments-8a6e3f42a694 16/16

You might also like