This article is part of the series on using Terraform in Azure and after explaining how to get started with Terraform in Azure, I will share my experience with using Terraform to provision your infrastructure for several environments.

Indeed, in a DevOps process in which your application will be deployed on several environments (example Dev - Test - QA - Production), it is the same for the infrastructure.

In Terraform there are several concepts that allow you to provision our infrastructure on multiple environments by having a maintainable code.

Among these concepts there are 2 that I will share to you, that are usage of:

  • Variables
  • Remotes backends for tfstates in Azure Storage
In this article I expose my usage of variables, the Remote backends will be exposed on the next.

Variables in Terraform have a very important role, they will allow you to optimize your code without having to duplicate it and thus improve its maintainability and visibility by having a common code for all environments and just variables for differences in names, properties,….

Using a variable is very simple: In your .tf file

1- Declaring your variables

variable "resource_group" {
  description = "The name of the resource group in which to create the virtual network."
  default     = "rg_hol_terraform"
}

variable "location" {
  description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
  default     = "West Europe"
}

2. Reference these variables

resource "azurerm_resource_group" "rg" {
  name     = "${var.resource_group}"
  location = "${var.location}"

  tags {
    environment = "Terraform in Azure"
  }
}

3. Instantiate these values

resource_group = "hol-terraform"

location="West Europe"

The complete sample is in my GitHub

That is the basis usage of variables in Terraform and you can read the official doc.

Organize the code for multiple environments

In the real project that it’s deployed in multiple environments (or stages) we need to organize the code with:

  • the same Azure resources code for all environments
  • use variables for parameter your environments properties

Below, a sample of my folder organization

project
│   main.tf                   --> code that provisioning your resources
|   variables.tf              --> your variables declaration
└───dev
│   │   env.tfvars            --> specific value of variable of the DEV env
└───production
    │   env.tfvars            --> specific value of variable of the PRODUCTION env
└───global_vars
    │   global.tfvars         --> common value of variables for all env

Each env.tfvars contains specific environment value, for example:

For dev

resource_group = "hol-terraform-dev"

For production

resource_group = "hol-terraform-prod"

The global.tfvars contain all common values for all environments

location="West Europe"

I intentionally use the same file names, in order to simplify the automatic execution of Terraform in the DevOps process (which we will see in future article).

Terraform execution with this model

At finally I will plan/apply my Terraform.

At the root of my project folder I run the command:

For deploy to dev

terraform plan -var-file="dev/env.tfvars" -var-file="global_vars/global.tfvars"

For deploy to production

terraform plan -var-file="production/env.tfvars" -var-file="global_vars/global.tfvars"

For all environments it will be the main.tf that will be executed and therefore it will be the same resources that will be created.

What Next

In this post, we saw how to use variables to optimize the terrafom code that will deploy an infra on several environments. In the next article, I will show you how I use backends remotes in Azure storage account to manage tfstates of all my environments.

My DevOps Links #2

My DevOps Links #2. Continue reading

My DevOps Links #1

Published on January 02, 2019

Get list of Azure VM IP addresses based on tags

Published on October 23, 2018