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.
- Review the code in \pip-conditional-expression-demo\variables.tf
- See that variable public_ip is currently set to false
- Review the code in \pip-conditional-expression-demo\main.tf
- Check the configuration of resource azurerm_public_ip
- See that we use the count function, this uses a conditional expression to check the value of var.public IP
- count = var.public_ip == "true" ? 1 : 0
- If true count = 1, if false count = 0
- This has the effect of either creating or not creating a public IP
- Now check under resource azurerm_network_interface
- We again use a conditional expression to check if a PIP will be deployed
- public_ip_address_id = var.public_ip == "true" ? azurerm_public_ip.azurevm.0.id : ""
- 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
- If var.public_ip is false we will set the public ip address a blank value, and no ID is set
- In the Visual studio code terminal shell, make sure you are in folder \pip-conditional-expression-demo\
- Run terraform init
- Run terraform plan check there are no errors and confirm that it is going to deploy the resources you expect.
- Run terraform apply and type yes to approve the change
- 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
- VNET and Subnet
- NIC
- No Public IP address should have been deployed
- If you review the NIC settings there should not be a public IP associated with it
- Edit variables.tf and change change public_ip from false to true
- Save the changes
- Run terraform plan check there are no errors and confirm that it is going to deploy the resources you expect.
- Run terraform apply and type yes to approve the change
- 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
- VNET and Subnet
- NIC
- Public IP
- A Public IP address should have been deployed
- 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)
- Now we need to clean up the resources we deployed in this lab
- From the terminal run terraform destroy
- Confirm that it is going to destroy only the resources you expect type yes to approve and cleanup the resources
- 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.
Sands Casino | Las Vegas, NV | SEGATIC PLAY
ReplyDeleteSands Casino is the only casino in Las Vegas with a true Vegas hospitality. You can expect everything at your fingertips in the most stylish and 샌즈 카지노 회원가입 modern Las
Slots by Pragmatic Play - AprCasino
ReplyDeletePragmatic Play. Pragmatic kadangpintar Play. Pragmatic Play. Slot febcasino Machine. The Dog House. Slots. novcasino Wild West apr casino Gold. Pragmatic Play. septcasino Jackpot Party.