Previous page >

Best Practice 2

In this article we will see how to set Terraform, provider and modules version (see this article from terraform.io website to learn more about managing Terraform version).

In this article we will perform the following action :

  1. Get a Resource Group
  2. Create a Virtual Network and a Subnet in this Virtual Network

Prerequisite


Item Description
Azure Subscription An Azure subscription id
Resource Group An Azure resource group is available
Storage Account An Azure storage account is available and is located in the upper resource group, it contains a container named tfstate
Service Principal An Azure service principal is available and has the owner privilege on the upper resource group
Terraform file Clone this repository and fill in the following files with the upper prerequisite items :
Variable used for the Terraform init : secret/backend-jdld.json
Variable used for the Terraform plan and apply : main.tf & main-jdld.tfvars & secret/main-jdld.json

What should we do?

We will create the upper mentioned element using remote backend (see the previous article BestPractice-1 for more information about remote backend).

The Terraform executable file, the AzureRm provider and our modules version will be set as described in the following bracket (also available in our main-jdld.tf Terraform file).

Declare the Terraform required version

terraform {
  required_version = ">= 0.12.6"

  backend "azurerm" {
    storage_account_name = "infrasdbx1vpcjdld1"
    container_name       = "tfstate"
    key                  = "BestPractice-2.tfstate"
    resource_group_name  = "infr-jdld-noprd-rg1"
  }
}

Specify the AzureRm version

provider "azurerm" {
  version         = ">= 1.31.0"
  subscription_id = "${var.subscription_id}"
  client_id       = "${var.client_id}"
  client_secret   = "${var.client_secret}"
  tenant_id       = "${var.tenant_id}"
  version         = "~> 2.0"
  features {}
}

Specify the module version

module "Az-VirtualNetwork" {
  source                      = "JamesDLD/Az-VirtualNetwork/azurerm"
  version                     = "0.1.4"
  net_prefix                  = "demo"
  network_resource_group_name = data.azurerm_resource_group.bp2.name
  virtual_networks = var.virtual_networks
  subnets = var.subnets
  route_tables            = {}
  network_security_groups = {}
}

1. Usage


This step ensures that Terraform has all the prerequisites to build your template in Azure.

terraform init -backend-config="secret/backend-jdld.json" -reconfigure

The terraform plan command is used to create an execution plan. This step compares the requested resources to the state information saved by Terraform and then gives as an output the planned execution. Resources are not created in Azure.

terraform plan -var-file="secret/main-jdld.json" -var-file="variable/main-jdld.tfvars"

If all is ok with the proposal you can now apply the configuration.

terraform apply -var-file="secret/main-jdld.json" -var-file="variable/main-jdld.tfvars"

2. Analysis


Description Screenshot
The Terraform init highlights our version version
Check the Terraform init when you remove the version noversion

See you!

JamesDLD