fbpx

Terraform has an inline If/Else conditional expression that enables you to set parameters and variables programmatically based on the condition being evaluated. The syntax of this “If/Then” or “If/Else” expression is similar to other programming languages where you have a condition to evaluate, then the result to return if either True or False are the evaluated conditions. It follows the below format in HCL:

condition ? true_value : false_value

This conditional expression can be used to programmatically assign resource parameters and variables based on an expression being evaluated. However, this feature is also very useful for other scenarios as well.

When setting up a block of HCL code to deploy / manage a resource, you can combine the inline If/Else conditional expression with the Resource count feature to easily be able to check a condition, then decide programmatically whether to deploy / manage the resource or not in your deployment. This can be useful with implementing Feature Flags in your Terraform code, as well as other scenarios where you may need to conditionally deploy resources.

You simply set the value of the count property of the resource using the If/Else conditional expression to assign the values of either 1 (to deploy the resource) or 0 (to not deploy the resource). This is a useful features that helps when developing custom Terraform Modules or other blocks of HCL that are reusable across may different Terraform projects.

The following is an example of using this technique to implement the conditional deployment of a resource based on a feature flag variable:

resource "azurerm_function_app" "b59func" {
  # Deploy conditionally based on Feature Flag variable
  count = local.deploy_b59func == true ? 1 : 0

  # resource attributes here
}

The count property on the resource block in Terraform used in conjunction with a conditional expression is the closest to supporting an “if” property to determine conditional resources. The conditional expression above essentially set the count to 1 to provision a single instance of the resource. When set to 0 then it will not provision the resource at all.

The count property is also supported on Terraform module blocks as well; as of Terraform 0.13 or later. So, this conditional deployment technique is not just available for resource blocks, but also Terraform Modules with the module block too!

The following is an example of using this technique to implement the conditional deployment of a module based on a feature flag variable:

module "azure-web-app-1" {
  source = "../../modules/azure-web-app"

  # Deploy conditionally based on Feature Flag variable
  count = local.deploy_b59webapp == true ? 1 : 0

  # module attributes here
}

Using these conditional expressions to conditionally deploy / manage resources in Terraform does not throw any exceptions or raise any errors based on the conditional expression. This is just an easy way to programmatically determine whether to deploy / manage the resource. If you need to raise an error if a certain condition is met within your Terraform code, then you’ll want to look into the Preconditions and Postconditions features.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading