Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Conditionally create resources in Terraform

I had scenario with Terraform to deploy resources based on the value given in variables. This is also an example of count and if condition. When you use count inside the resource block and if value is mentioned 0 that resource will not be deployed. Now I am giving the count number programmatically using if condition. If env value in var is dev, it will give me count 1 and resource will be deployed. Lets check this through demo below. 

#main.tf - Conditionally create resources in Terraform example
# We strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=3.0.0"
    }
  }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

variable "env" {
  type = string
}

resource "azurerm_resource_group" "rg" {
  count = "${var.env == "dev" ? 1 : 0}" #if count is 0 means it will not create a resource. If env -eq dev means count 1, or else 0 which will deploy resource accordingly
  name = "${var.env}-RG"
  location = "West US"
}

To make it understand, If I use env=prod in var, in the count value will be 0 for resources, No changes are applied in your infrastructure.

terraform apply --auto-approve -var="env=prod"

In second example I will use env=dev in var, the count will be 1 for resources, it will deploy the 1 resource as you can see in the screenshot.

terraform apply --auto-approve -var="env=prod"

Microsoft Powershell windows Hashicorp hcl terraform tf state apply complete destroy foreach index foreach no changes resource group.png

I have tested the same with already applied resource, It will show the current configuration but no changes are required.

terraform hcl no changes infrastructure microsoft azure hashicorp terraform apply -refresh-only resource group --auto-approve subscription location.png

Caution: But once you deploy the resource , and try to deploy other resource with different var value in count, it will destroy the deployed dev resource.

terrafrom apply --auto-approve -var="env=prod"

Terraform apply --auto-approve prod variable tfvar azurerm microsoft azure subscription index count 0 auto destroy automatically tf file.png

After deployment incase if you are trying to run different var in same state. make sure you remove the state if not required. Then use different variables.

terraform state list
terraform state rm azure_resource_group.rg[0]

Terraform apply --auto-approve environment dev prod devlopment production create hashicorp azure microsoft state list state remove no changes 0 index auto destroy.png

Useful Articles
Create an Azure App registrations in Azure Active Directory using PowerShell & AzureCLI
Get started and configure with certificate-based authentication in Azure
Create a Virtual machine on Microsoft Azure
PowerShell List All Azure Resverations
Powershell get the list of Azure Reservations Virtual Machines instances
Get the list Azure Reservation Catalog with PowerShell and AzureCLI
Azure automation account DSC for On-Premise Virtual Machine on boarding
Azure Powershell : Operation returned an invalid status code 'BadRequest'
Get Azure virtual machine backup reports using Powershell

 

Go Back

Comment

Blog Search

Page Views

8559616

Follow me on Blogarama