#
Bonus Hands-On Exercise: Creating a Networking Module for Virtual Networks
#
Exercise Objective
In this bonus exercise, participants will create a reusable Terraform module to manage virtual networks (VNets) in Azure. They will then use this module to deploy virtual networks in different environments, such as development and production.
#
Steps
#
Step 1: Create a New Directory for the Networking Module
Create the Module Directory:
- Open your terminal and navigate to your project directory.
- Create a new directory named
modules/networkto hold the networking module:mkdir -p modules/network
Verify Directory Structure:
- Your project directory should now contain:
. ├── main.tf └── modules └── network
- Your project directory should now contain:
#
Step 2: Create the main.tf for the Networking Module
Create a
main.tfFile in the Module Directory:- In
modules/network, create a file namedmain.tfto hold the virtual network configuration.
- In
Add Virtual Network and Subnet Resource Definitions:
- In
modules/network/main.tf, define resources for the virtual network and a subnet:resource "azurerm_virtual_network" "vnet" { name = var.vnet_name location = var.location resource_group_name = var.resource_group_name address_space = var.address_space } resource "azurerm_subnet" "subnet" { name = var.subnet_name resource_group_name = var.resource_group_name virtual_network_name = azurerm_virtual_network.vnet.name address_prefixes = var.subnet_address_prefixes } - This configuration defines:
- Virtual Network: Using parameters for the name, location, resource group, and address space.
- Subnet: Using parameters for the subnet name, address prefixes, and associations to the virtual network.
- In
Save the
main.tfFile.
#
Step 3: Define Input Variables in variables.tf for the Module
Create a
variables.tfFile in the Module Directory:- In
modules/network, create a file namedvariables.tf.
- In
Define Variables for VNet and Subnet Properties:
- Add the following variable definitions to parameterize the VNet and subnet configuration:
variable "vnet_name" { description = "The name of the virtual network" type = string } variable "location" { description = "The Azure location for the network resources" type = string } variable "resource_group_name" { description = "The name of the resource group" type = string } variable "address_space" { description = "The address space for the virtual network" type = list(string) } variable "subnet_name" { description = "The name of the subnet" type = string } variable "subnet_address_prefixes" { description = "The address prefixes for the subnet" type = list(string) }
- Add the following variable definitions to parameterize the VNet and subnet configuration:
Save the
variables.tfFile.
#
Step 4: Create Outputs in outputs.tf for the Module
Create an
outputs.tfFile in the Module Directory:- In
modules/network, create a file namedoutputs.tf.
- In
Define Output Values for VNet and Subnet Information:
- Add outputs to provide information about the created virtual network and subnet:
output "vnet_id" { description = "The ID of the virtual network" value = azurerm_virtual_network.vnet.id } output "subnet_id" { description = "The ID of the subnet" value = azurerm_subnet.subnet.id }
- Add outputs to provide information about the created virtual network and subnet:
Save the
outputs.tfFile.
#
Step 5: Call the Networking Module in the Root Configuration for Each Environment
Create a
main.tfFile in the Root Directory:- In the root directory of your project, open or create
main.tfto use the networking module.
- In the root directory of your project, open or create
Add Module Calls for Development and Production Environments:
- Call the
networkmodule twice, passing in different parameters for each environment:# Development Environment module "dev_network" { source = "./modules/network" vnet_name = "dev-vnet" location = "East US" resource_group_name = "dev-resource-group" address_space = ["10.0.0.0/16"] subnet_name = "dev-subnet" subnet_address_prefixes = ["10.0.1.0/24"] } # Production Environment module "prod_network" { source = "./modules/network" vnet_name = "prod-vnet" location = "West US" resource_group_name = "prod-resource-group" address_space = ["10.1.0.0/16"] subnet_name = "prod-subnet" subnet_address_prefixes = ["10.1.1.0/24"] } - The module is sourced from
./modules/network, and each environment (dev and prod) has unique values forvnet_name,location,resource_group_name, andaddress_space.
- Call the
Save the
main.tfFile.
#
Step 6: Run Terraform Commands to Test the Module
Initialize the Project:
- In the terminal, run
terraform initto initialize the project and prepare for deployment:terraform init
- In the terminal, run
Run
terraform planto Preview the Changes:- Run
terraform planto review the resources that will be created for both the development and production environments:terraform plan - Verify that the output includes the virtual network and subnet resources for both
devandprodenvironments.
- Run
Run
terraform applyto Deploy the Module:- Apply the configuration to create the virtual networks and subnets in Azure:
terraform apply - Type
yeswhen prompted to confirm the apply operation.
- Apply the configuration to create the virtual networks and subnets in Azure:
Review the Output Values:
- After the apply completes, observe the output values defined in
outputs.tf, such asvnet_idandsubnet_idfor both environments.
- After the apply completes, observe the output values defined in
#
Verification
Confirm Resource Creation in Azure:
- Log in to the Azure Portal and navigate to the Resource Groups for both
dev-resource-groupandprod-resource-group. - Verify that the virtual networks (
dev-vnetandprod-vnet) and their respective subnets have been created with the correct configurations.
- Log in to the Azure Portal and navigate to the Resource Groups for both
Check Output Values:
- Ensure that the output values displayed in the terminal match the expected VNet and subnet information for each environment.