Open In App

Azure VPC Using Terraform

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

Steps To Setup Azure VPC or VNet Using Terraform

Step 1: Set Up Terraform

Step 2: Set Up Azure CLI



Step 3: Configure Azure CLI

az login


Step 4: Create Terraform Code

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 = "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"]
}
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

terraform init



terrraform apply

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.


Article Tags :