Deploy Resources (Resource Group, Virtual Network And Subnet) In Azure Via Terraform

Authenticate using the Azure CLI

az login

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Write configuration

Create a folder called learn-terraform-azure.

New-Item -Path "c:\" -Name "learn-terraform-azure" -ItemType "directory"

Create a new file called main.tf and paste the configuration below.

New-Item -Path "c:\ learn-terraform-azure" -Name "main.tf"

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Note: The location of your resource group is hardcoded in this example. If you do not have access to the resource group location westus2, update the main.tf file with your Azure Region.

Above configurations mentioned to deploy below Resources,

Resource Group = myarticleResourceGroup
Location = WestUs2
Tags: -
            Environment = Terraform Getting Started
            Team = Devops
Virtual Network = myArticleVnet
Location = WestUs2
Address Space = 10.0.0.0/16
Subnet: -
            Subnet name = frontendsubnet
            Subnet = 10.0.1.0/24
            Subnet Name = backendsubnet
            Subnet = 10.0.2.0/24

Initialize your Terraform configuration

Initialize your learn-terraform-azure directory in your terminal. The terraform commands will work with any operating system. Your output should look similar to the one below.

terraform init

Initializing the backend...

Initializing provider plugins...

  • Finding hashicorp/azurerm versions matching "~> 3.0.2"...
  • Installing hashicorp/azurerm v3.0.2...
  • Installed hashicorp/azurerm v3.0.2 (signed by HashiCorp)

Terraform has been successfully initialized!

Format and validate the configuration

Format your configuration. Terraform will print out the names of the files if modified. In this case, your configuration file was already formatted correctly, so Terraform won't return any file names.

terraform fmt

You can also make sure your configuration is syntactically valid and internally consistent by using the terraform validate command.

Validate your configuration. The example configuration provided above is valid, so Terraform will return a success message.

terraform validate

Success! The configuration is valid.

Apply your Terraform Configuration

Run the terraform apply command to apply your configuration.

This output shows the execution plan and will prompt you for approval before proceeding. If anything in the plan seems incorrect or dangerous, it is safe to abort here with no changes made to your infrastructure. Type yes at the confirmation prompt to proceed.

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Inspect your state

When you apply your configuration, Terraform writes data into a file called terraform.tfstate. This file contains the IDs and properties of the resources Terraform created so that it can manage or destroy those resources going forward. Your state file contains all of the data in your configuration and could also contain sensitive values in plaintext, so do not share it or check it into source control.

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Inspect the current state using terraform show.

terraform show

You can see the deployed Resources via the Azure Portal.

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Deploy Resources (Resource Group, Virtual Network and Subnet) in Azure via Terraform

Summary

We learned how to deploy Resources (Resource Group, Virtual Network, and Subnet) in Azure via Terraform. Please leave a comment in the comment box if you have any questions.


Similar Articles
IFS R&D International (Pvt) Ltd
IFS develops and delivers enterprise software for customers around the world