Terraform Functions - Part 3 - Conditional expression


In part three of this blog series we will take a look at the conditional expression function in Terraform and see how we can use this with a basic Azure Web App deployment. In a future blog we will explore how to use the conditional expression alongside the count function in a more complex resource deployment. 

In part one we cover an overview of the blog series and some pre-requisites you will need to get started when working through the deployment steps in this blog series, if you haven't seen this information please review this here before you get started with the steps below.

Conditional expressions can be used to dynamically omit or change an argument depending on a certain condition. This can be useful for several scenarios, one example may be that you create a module for deploying VMs and have a variable for VM size of small or large, you can set this as a variable and evaluate this variable to change configurations like SKU, disk size etc. 

The example we will go through is the conditional deployment of a Public IP (PIP) on a NIC and associating this with a NIC depending on the value of a variable named 'public_ip'. The scenario could be that we are deploying VMs using a module or template and not all VMs will need a PIP. We have the variable 'public_ip' that is set to true or false and using a conditional expression we determine what resources we need to deploy. 

WARNING!! In this lab we will be deploying a VNET (10.10.0.0/16) and subnet (10.10.2.0/24) check these do not conflict with other networks in your Azure subscription before proceeding.

Condition Expression Demo

  1. Review the code in \pip-conditional-expression-demo\variables.tf
  2. See that variable public_ip is currently set to false
  3. Review the code in \pip-conditional-expression-demo\main.tf
  4. Check the configuration of resource azurerm_public_ip
  5. See that we use the count function, this uses a conditional expression to check the value of var.public IP
    1. count = var.public_ip == "true" ? 1 : 0
  6. If true count = 1, if false count = 0
  7. This has the effect of either creating or not creating a public IP
  8. Now check under resource azurerm_network_interface
  9. We again use a conditional expression to check if a PIP will be deployed
    1. public_ip_address_id = var.public_ip == "true" ? azurerm_public_ip.azurevm.0.id : ""
  10. If var.public_ip is true we will set the public ip address value to be that of the ID of the PIP we create in resource azurerm_public_ip.azurevm
  11. If var.public_ip is false we will set the public ip address a blank value, and no ID is set
  12. In the Visual studio code terminal shell, make sure you are in folder \pip-conditional-expression-demo\
  13. Run terraform init
  14. Run terraform plan check there are no errors and confirm that it is going to deploy the resources you expect.
  15. Run terraform apply and type yes to approve the change
  16. Check the Azure Portal to review what has been deployed in the resource group terraform-demos-prd-rg you should have the following resources deployed
    1. VNET and Subnet
    2. NIC
  17. No Public IP address should have been deployed
  18. If you review the NIC settings there should not be a public IP associated with it
  19. Edit variables.tf and change change public_ip from false to true
  20. Save the changes
  21. Run terraform plan check there are no errors and confirm that it is going to deploy the resources you expect.
  22. Run terraform apply and type yes to approve the change
  23. Check the Azure Portal to review what has been deployed in the resource group terraform-demos-prd-rg you should now have the following resources
    1. VNET and Subnet
    2. NIC
    3. Public IP
  24. A Public IP address should have been deployed
  25. The NIC should have a public IP associated with it (it will not have an IP assigned as it is not actively attached to a VM)
  26. Now we need to clean up the resources we deployed in this lab
  27. From the terminal run terraform destroy
  28. Confirm that it is going to destroy only the resources you expect type yes to approve and cleanup the resources
  29. Check the resources have been deleted in the Azure Portal
After running through this lab you will have learned how we can use conditional expressions to determine whether we should deploy a resource and what value a resource should be configured with.  We will use this function again in the last blog in the series when we run a more complex VM deployment bringing the count and conditional expression functions together.   This is a useful function to use in your Terraform templates and modules to help make them re-usable for multiple scenarios.   I hope you have found this useful, please keep an eye out for the last blog in this series.

Comments

Popular posts