Terraform Functions - Part 2 - Count


In part two of this blog series we will take a look at the count 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 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.

The count function is very useful for easily deploying multiple resources of the same type and configuration. For example, we may need a web farm and all the Virtual Machines need the same configuration, using the count function we can deploy the same resource multiple times and include the count number in the name so we deploy web1, web2, web3. We can also use the count function when deploying the related resources NICs, IP addresses, disks etc. We can easily change this count number to scale up or down the number of resources deployed. 

In our example we use Web Apps as they are quick to deploy. We will deploy one Application Service plan and as many Web Applications as determined by the count variable. 

Count Demo

  1. Review the code in \app-service-count-demo\variables.tf
  2. This is where we set the webappcount variable which is currently set to 1
  3. Review the code in \app-service-count-demo\main.tf
  4. Under resource azurerm_app_service is where we set the count function referencing the webappcount variable 
  5. We include count.index in the name, this will iterate through the count and add a number to the resource name so each one is unique 
    count = var.webappcount
    name = join("", [var.environment,"-WebApp-"count.index])
  1. In the Visual studio code terminal shell, make sure you are in folder \app-service-count-demo\
  2. Run terraform init
  3. Run terraform plan check there are no errors and confirm that it is going to deploy the resources you expect.
  4. Run terraform apply and type yes to approve the change
  5. Check the Azure Portal to review what has been deployed, you should have;
    1. One App service Plan, one App service
  6. Edit variables.tf and change webappcount to 3
  7. Save the changes
  8. Run terraform plan check there are no errors and confirm that it is going to deploy the resources you expect.
  9. Run terraform apply and type yes to approve the change
  10. Check the Azure Portal to review what has been deployed, you should have;
    1. One App service Plan, three App services
  11. From the terminal run terraform destroy
  12. Confirm that it is going to destroy only the resources you expect type yes to approve and cleanup the resources
  13. Check the resources have been deleted in the Azure Portal
After running through this lab you have experienced how we can use the count function to deploy multiple resources of the same type and configuration. When you come to deploy other resources like virtual machines these have related resources that will also need to be added to each instance as we increase the count i.e. disks, NICs, IPs etc. Later in the blog series we explore this scenario and how we scale a virtual machine and all related resources using the count function.

Comments

  1. It differs from one on-line on line casino in South Korea to the next, and it depends on by} your gaming preferences. Check out our in-depth evaluations 메리트카지노 of the most effective on-line casinos in South Korea search out|to search out} the right platform for you and your price range. 18+ | T&C's Apply | Gamble Responsibly and only guess money you could afford to lose.

    ReplyDelete

Post a Comment

Popular posts from this blog

Working with WSL and AKS

Azure Kubernetes Service (AKS) and Managed Identities

Azure DevOps Microsoft Terraform Task and Terraform 0.15 issue