IaC has revolutionized the way we implement our architecture. Terraform is one of the most widely used tools for implementing the architecture. Terraform not only makes the implementation easy but also helps us define modules that can be reused again and again for future deployments. This blog is part of the Terraform series about the Terraform code for Azure Implementation.

Table of contents:


This code is parameterized and we are using different modules for every Azure Resource. This allows us to manage our code along with enabling the reusability. This repositiry contains two subfolders :

  1. terraform-modules
    • This directory contains all the modules.
    • Any variable conditional needs to be checked in the file under respective module
  2. terraform-resources
    • This folder has these files :
      • -> This file contains all the resources that we are going to deploy.
      • -> Variables are defined in this file.
      • terraform.tfvars -> Tfvars file is automatically loaded without any additional option. This is the file where you need to update your resource values\names.

Things to keep in mind are :

  1. We are using Azure BLOB as backend configuration, so you need to ensure the storage account and container mentioned in the backend configuration exists.
  2. Since we are using data block to fetch key vault secrets, where we store all our sensitive information. The key vault and the secrets should already exist.

To run this example, simply follow to steps below:

Navigate to terraform-resources folder, if the terminal is opened in Linux Virtual Machine Deployment directory, use :

  cd terraform-resources
  terraform init
  terraform plan
  terraform apply

Command usage :

  1. Terraform init -> It initializes the directory and downloads required provider along with configuring the module.
  2. Terraform plan -> This helps you verify the code is going to deploy the resources as expected. This also ensures we don’t face any unwanted surprise. This isn’t mandatory, but a recommended step.
  3. Terraform apply -> This step applies the resources specified in this code. This will ask you to approve this later. We can skip manual approval by using –auto-approve parameter.

Best Practices & Recommendations

  1. Use Terraform workspaces for easier management of the deployments. This can also help us manage Dev, UAT and Production deployments instead of creating multiple state files\directories.
  2. If you are creating new resources\variables. Ensure naming convention is easily relatable, since we have a lot of variables in this code.
  3. Use conditionals to avoid unwanted surprises.