Hi All,
In this post I am going to explain how to install Terraform and then we will create an Azure virtual network using Terraform.
Install Terraform
In order to install Terraform you will need to download the latest version from the Terraform website here. I’ve downloaded the Windows version.
I extracted the zip that I downloaded from above and moved the terraform.exe into the following folder structure.
1 |
C:\dev\terraformcli |
I then proceed to update my environment variables specifically my Path user environment variable, pointing to the location of my Terraform.exe client.
Create Azure Virtual Network
Azure CLI
Before we can start to use Terraform, we first need to login into our Azure account from our local development workstation/laptop. To do this you will need to install the Azure CLI you can learn more on how to do this and download the CLI from this Microsoft Learn page. (will open in new tab)
Once you have the CLI installed you can issue the following commands, using your windows terminal.
1 2 3 |
az login az account set --subscription "YOUR SUBSCRIPTION ID" |
Terraform Configuration
Now that we have installed Terraform, we can test it and get some experience using Terraform to create a simple Virtual Network in Azure. I first created a folder structure as shown:
1 |
C:\SourceControl\dev\terraform\azure\vnet-tutorial |
I then opened the vnet-tutorial folder using Visual Studio code.
With Visual studio code now opened I can then install the HashiCorp Terraform plugin extension to help with syntax highlighting and auto completion. I’ve already installed this extension but you would need to click on the install button.
We are now ready to start writing some Terraform code. Back in VS code I created a new file called main.tf with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "3.73.0" } } } provider "azurerm" { features { } } resource "azurerm_resource_group" "rg" { name = "tf-learn-rg" location = "uksouth" tags = { Environment = "Terraform Getting Started" Team = "DevOps" } } resource "azurerm_virtual_network" "vnet" { name = "tf-vnet-uks" address_space = ["10.0.0.0/16"] location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name } |
the below image shows my view of VS code.
Terraform Workflow
Once we have written our config file we can now perform the following Terraform workflow
- terraform init
- terraform validate
- terraform plan
- terraform apply
Terraform Init
we start with the following command:
1 |
terraform init |
this command will perform the following:
- initialize the Working Directory: When you’re using Terraform, you have a folder where you keep all your configuration files, exactly like we have. “Terraform init” is like going into that folder and getting it ready for work.
- Download Dependencies: Terraform uses plugins and modules to interact with cloud providers like AWS, Azure, or Google Cloud. “Terraform init” checks your configuration files and downloads any necessary plugins or modules if they’re not already on your computer. in our case it will download the Azure plugins.
- Initialize a State File: Terraform keeps track of the infrastructure it manages in a special file called the state file. “Terraform init” sets up this file, which is like having a map to keep track of what you’re building.
below is an image of what this looks like:
Terraform Validate
next issue the following command:
1 |
terraform validate |
Here’s what “terraform validate” does:
- Syntax Check: It looks at your Terraform configuration files (which describe how you want your infrastructure to be) and checks if there are any syntax errors. Syntax errors are like typos or mistakes in your instructions.
- Semantic Check: It also performs a deeper check to ensure that your configuration makes sense in the context of Terraform. This includes checking if the resources you’re trying to create or modify are supported by the cloud provider you’re using.
- Variable Validation: If you’re using variables in your configuration (like placeholders for values that can change), Terraform will make sure they are defined correctly and have valid values.
Terraform Plan
next issue the terraform plan, it can take a few seconds to complete.
1 |
terraform plan |
Here’s what “terraform plan” does:
- Dry Run: It performs a “dry run” of your Terraform configuration. This means it analyzes your configuration files and compares them to the current state of your cloud infrastructure to determine what changes, if any, need to be made. However, it doesn’t actually make those changes yet.
- Output Plan: After analyzing your configuration, “terraform plan” generates a detailed report that tells you what actions Terraform intends to take. It shows you which resources it will create, modify, or delete and provides information on any changes in configuration.
- Validation: “terraform plan” also validates your configuration, checking if it’s syntactically correct and if all variables and resources are properly defined.
Terraform Apply
we can now apply our configuration to azure, issue the command, again it can take a few seconds to complete. you will be prompted to confirm the changes.
1 |
terraform apply |
Here’s what “terraform apply” does:
- Execution: When you run “terraform apply,” Terraform takes the instructions and plans generated from your configuration files (after you’ve used “terraform plan”) and executes those instructions against your cloud provider’s APIs. It creates, modifies, or deletes resources as needed to match your desired infrastructure state.
- Interactions with Cloud Provider: Terraform communicates with your cloud provider (e.g., AWS, Azure, Google Cloud) to carry out the requested actions, in our case this is Azure. It might create virtual machines, set up networks, configure storage, or perform other cloud-related tasks, depending on your configuration.
- State File Update: After successfully applying the changes, Terraform updates its state file to reflect the new state of your infrastructure. This state file acts as a record of what Terraform has created, allowing it to track changes and manage your infrastructure in subsequent runs.
Lets Confirm, that the resources where created.
as you can see from the above image, the resources where created.
Cleaning Up
issue the following command to remove all the resources from this tutorial.
1 |
terraform destroy |