Open In App

Azure VPC Using Terraform

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Azure VPC also known as Azure Virtual Network is service that provides networking services in the Azure cloud. Azure VNet provides networks, subnets, and other networking resources. Terraform can be used for easy and hassle-free deployment of networks in Azure. Let’s see how we can use Terraform for Azure VPC.

Primary Components of Azure VPC with Terraform

  • Terraform: It is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.
  • Azure VNet: In Microsoft Azure, a Virtual Network (VNet) is a fundamental building block that allows you to create private, isolated networks within the Azure cloud.
  • IaaC: Infrastructure as a Code allows to representation of cloud infrastructure in the form of code.

Steps To Setup Azure VPC or VNet Using Terraform

Step 1: Set Up Terraform

  • Download the Terraform zip from the installation page of the Terraform website.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.

Step 2: Set Up Azure CLI

  • Download the Azure CLI setup from the official website.
  • Run the installer and follow the steps to install.

Azure CLI

Step 3: Configure Azure CLI

  • Open terminal and run below command.
az login


  • A browser window will open for login. Login with your azure credentials. Once it is done you will see output as below.

Terraform login

Step 4: Create Terraform Code

  • Goto your project folder and create main.tf file.
  • Add terraform block to code with azure as required provider with latest version. You can find the latest version at hashicorp registry.
  • Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
  • Now add provider as azurerm like below. Specify other details as required.
provider "azurerm" {
features {}
}
  • Add configuration for VNet. For this article we will set up a network and subnet. Similar procedure can be followed for creating multiple subnets.
resource "azurerm_virtual_network" "gfg_vnet" {
name = "gfg-vnet"
address_space = ["10.0.0.0/16"]
location = "eastus"
resource_group_name = "<YOUR RESOURCE GROUP NAME>"
}

resource "azurerm_subnet" "vnet_subnet_1" {
name = "subnet-1"
resource_group_name = "<YOUR RESOURCE GROUP NAME>"
virtual_network_name = "gfg-vnet"
address_prefixes = ["10.0.0.0/24"]
}
  • We have specified name for virtual network. We have specified the address space for it.
  • As we can see we have also added subnet configuration with specified network name.
  • After specifying these many details, we are ready for deployment. You can further provide more option as requirement.
  • The complete code will look like below.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_virtual_network" "gfg_vnet" {
name = "gfg-vnet"
address_space = ["10.0.0.0/16"]
location = "<YOUR RESOURCE GROUP NAME"
resource_group_name = "DeepsLab"
}

resource "azurerm_subnet" "vnet_subnet_1" {
name = "subnet-1"
resource_group_name = "<YOUR RESOURCE GROUP NAME"
virtual_network_name = "gfg-vnet"
address_prefixes = ["10.0.0.0/24"]
}

Step 5: Apply The Terraform Code

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.
terraform init

Terraform init

  • After successful output apply the changes using below command.
terrraform apply

Terraform Apply

  • After verifying type “yes” to confirm and apply.
  • Terraform will start creating network and subnet.

Terraform Created VN

  • You can also verify deployment by visiting Virtual Networks page of Azure.

Virtual Networks

  • we might get error for subnet as the vnet might not be ready . For this apply the configuration again and subnet will be created successfully. Subnets can be verified from subnets tab of virtual network created.

terraform Plan

GFG-Subnet

Conclusion

We have successfully deployed an Azure VPC or VNet with the help of terraform in this article. the configuration described can be further modified to make changes to the network and subnets in azure. This is how terraform allows reusable and modifiable configuration of infrastructure.

Azure VPC Using Terraform – FAQ’s

What is Terraform, and why use it for creating Azure VNets?

Terraform is an open-source infrastructure as code (IaaC) tool that allows you to define and provision infrastructure in a declarative configuration language. It supports multiple cloud providers, including Azure. Using Terraform for Azure VNets provides benefits such as version-controlled infrastructure, ease of collaboration, and repeatability.

What is the Azure Terraform provider?

The Azure Terraform provider is a plugin for Terraform that enables you to define and manage Azure resources using Terraform configurations. It translates the Terraform configuration into Azure Resource Manager (ARM) templates and provisions resources accordingly.

Can I create multiple subnets within a Virtual Network?

Yes, you can create multiple subnets within a Virtual Network. In your Terraform configuration, use the azurerm_subnet resource to define each subnet. Associate these subnets with the main azurerm_virtual_network resource using the virtual_network_name attribute.

How can I configure network security groups (NSGs) using Terraform?

You can configure NSGs using the azurerm_network_security_group resource in Terraform. Define the NSG rules using the azurerm_network_security_rule resource. Associate the NSG with subnets using the network_security_group_id attribute in the azurerm_subnet resource.

How do I destroy resources created by Terraform?

To destroy resources created by Terraform, use the terraform destroy command. This command reads your Terraform configuration, identifies the resources, and prompts you to confirm the destruction. It’s important to use this command carefully to avoid unintended data loss.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads