avatarBhargav Bachina

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3060

Abstract

se 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 <code>init, plan and apply.</code></p><figure id="1554"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*crMDKBgq0tA8CnzhYStPdA.png"><figcaption><b>Using Folders</b></figcaption></figure><h2 id="a71b">Pros</h2><ul><li>You can easily add or remove resources in each environment</li><li>Changes in one environment don’t affect other environments</li></ul><h2 id="eee7">Cons</h2><ul><li>Duplication of code</li><li>If you want to change the resource you have to change it in all environments.</li></ul><h1 id="cbd2">Using Folders — Method 2</h1><p id="fd3f">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 different infrastructure in all environments.</p><p id="7ce1">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.</p><div id="c7a9"><pre><span class="hljs-comment">// Dev Environment</span> terraform plan --<span class="hljs-keyword">var</span>-<span class="hljs-keyword">file</span>=<span class="hljs-string">"tfvars/environment/dev.tfvars"</span></pre></div><div id="1655"><pre><span class="hljs-comment">// QA Environment</span> terraform plan --<span class="hljs-keyword">var</span>-<span class="hljs-keyword">file</span>=<span class="hljs-string">"tfvars/environment/qa.tfvars"</span></pre></div><div id="4784"><pre><span class="hljs-comment">// Prod Environment</span> terraform plan --<span class="hljs-keyword">var</span>-<span class="hljs-keyword">file</span>=<span class="hljs-string">"tfvars/environment/prod.tfvars"</span></pre></div><figure id="7173"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FiwmqIQLwl7FVG5aVbJt0A.png"><figcaption><b>Using Folders</b></figcaption></figure><h2 id="4d0c">Pros</h2><ul><li>There is no duplication of code</li><li>If you want to change the resource you don’t have to change in all the environments.</li></ul><h2 id="f140">Cons</h2><ul><li>You can’t easily add or remove resources in each environment</li><li>Changes in one environment do affect other environments since we are using the same files with different var files.</li></ul><h1 id="e337">Workspaces</h1><p id="1e0f">Terraform starts with a single workspace named <b><i>default</i></b>. 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 <b><i>default</i></b> workspace.</p><p id="1f5f">Workspaces are managed with the command <code>terraform workspace.</code> There are a set of commands for the workspaces. For Example, You can use this comma

Options

nd <code>terraform workspace new</code> to create a new workspace.</p><h1 id="60bd">Modules</h1><p id="e0e7">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 <code>.tf</code> in the main working directory.</p><p id="25dc">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.</p><p id="9569">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.</p><figure id="1b71"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*CFv2Am_ovSLjcoK49QosJw.png"><figcaption><b>Multiple Environments</b></figcaption></figure><h1 id="4149">Terragrunt</h1><p id="e9a5">Terragrunt is a thin wrapper that provides extra tools for keeping your configurations DRY, working with multiple Terraform modules, and managing remote state.</p><p id="0615">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.</p><div id="ef9d"><pre><span class="hljs-symbol">https:</span><span class="hljs-comment">//terragrunt.gruntwork.io/</span></pre></div><h1 id="3814">Summary</h1><ul><li>Terraform is an open-source infrastructure as a code software tool that provides a consistent CLI workflow to manage hundreds of cloud services.</li><li>Most of the time we deploy the infrastructure in multiple environments. We need these environments for the development, stage, testing, and production.</li><li>There are 5 ways to write reusable code for different environments in Terraform.</li><li>Using Folders is the most used one of all. There are two ways to do this.</li><li>Terraform starts with a single workspace named <b><i>default</i></b>. The workspace is special because it is the default and also because it can’t ever be deleted.</li><li>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.</li><li>Terragrunt is a thin wrapper that provides extra tools for keeping your configurations DRY, working with multiple Terraform modules, and managing remote state.</li></ul><h1 id="6338">Conclusion</h1><p id="29ed">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.</p></article></body>

Terraform — 5 Ways To Create Infrastructure in Multiple Environments

Exploring Different ways of creating infrastructure in different environments with Terraform

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.

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.

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.

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 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"
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.

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/

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
Recommended from ReadMedium