Creating Azure Virtual Networks with Terraform

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and manage your cloud resources declaratively. In this post, we’ll explore how to use Terraform to create Azure Virtual Networks (VNets), a critical component for organizing and securing your Azure infrastructure.

Why use Terraform for Azure VNets?

Azure Virtual Networks (VNets) provide a secure and isolated environment for your Azure resources. By using Terraform, you can automate the creation and management of VNets, ensuring consistency and reducing manual effort.

Terraform’s declarative approach ensures that your infrastructure is always in the desired state.

Prerequisites

Before you begin, ensure you have the following:

  1. Terraform installed: Download and install Terraform from terraform.io.
  2. Azure CLI installed: Install the Azure CLI from Microsoft’s documentation.
  3. Azure account: Sign up for an Azure account if you don’t already have one.

Step-by-step guide to creating a VNet

1. Initialize your Terraform project

Create a new directory for your Terraform configuration files and navigate to it:

mkdir terraform-vnet && cd terraform-vnet

2. Write the Terraform configuration

Create a file named main.tf and add the following configuration:

// main.tf
provider "azurerm" {
  features {}
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = "East US"
  resource_group_name = "example-resource-group"
}

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = "example-resource-group"
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

This configuration defines a Virtual Network named example-vnet with an address space of 10.0.0.0/16 and a subnet named example-subnet with an address prefix of 10.0.1.0/24.

3. Initialize Terraform

Run the following command to initialize the Terraform project and download the Azure provider:

terraform init

4. Preview the changes

Use the terraform plan command to preview the changes Terraform will make:

terraform plan

This will show you the resources that will be created.

5. Apply the configuration

Apply the configuration to create the VNet and subnet:

terraform apply

Type yes when prompted to confirm the changes.

6. Verify the VNet

You can verify the VNet in the Azure portal or by using the Azure CLI:

az network vnet show --name example-vnet --resource-group example-resource-group

Best practices for managing VNets with Terraform

  1. Use variables: Parameterize your configuration to make it reusable across environments.
  2. State management: Use a remote backend like Azure Storage to store your Terraform state securely.
  3. Version control: Store your Terraform configuration files in a version control system like GitHub.

Example with variables

Here’s an updated configuration using variables:

// variables.tf
variable "vnet_name" {
  default = "example-vnet"
}

variable "address_space" {
  default = ["10.0.0.0/16"]
}

variable "subnet_name" {
  default = "example-subnet"
}

variable "subnet_prefix" {
  default = ["10.0.1.0/24"]
}

variable "location" {
  default = "East US"
}

variable "resource_group_name" {
  default = "example-resource-group"
}

// main.tf
provider "azurerm" {
  features {}
}

resource "azurerm_virtual_network" "example" {
  name                = var.vnet_name
  address_space       = var.address_space
  location            = var.location
  resource_group_name = var.resource_group_name
}

resource "azurerm_subnet" "example" {
  name                 = var.subnet_name
  resource_group_name  = var.resource_group_name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = var.subnet_prefix
}

To apply this configuration, create a terraform.tfvars file with your desired values:

vnet_name = "my-vnet"
address_space = ["10.1.0.0/16"]
subnet_name = "my-subnet"
subnet_prefix = ["10.1.1.0/24"]
location = "West US"
resource_group_name = "my-resource-group"

Conclusion

Using Terraform to create Azure Virtual Networks is a simple yet powerful way to automate your cloud infrastructure. By following best practices and leveraging Terraform’s features, you can ensure your resources are organized, consistent, and easy to manage.

Start small and gradually expand your Terraform configurations to include more complex networking setups, such as peering and network security groups.

# Example: Destroying the VNet
terraform destroy

With Terraform, managing your Azure networking infrastructure becomes efficient and scalable, allowing you to focus on building great applications.

Creating Azure Virtual Networks with Terraform
Creating Azure Virtual Networks with Terraform