#
Exercise: Refactoring Configurations into Reusable Modules
#
Objective
Participants will refactor their existing configurations (such as the one for provisioning a web server) into reusable modules. This exercise will help participants organize and parameterize their Terraform code, making it easier to reuse across different projects and environments.
#
Steps
#
Step 1: Create a New Directory for the Module
Create a Module Directory:
- Open your terminal and navigate to the project directory.
- Create a new directory named
modules/webserverto hold the reusable web server module:mkdir -p modules/webserver
Verify Directory Structure:
- Your project directory should now contain the following structure:
. ├── main.tf └── modules └── webserver
- Your project directory should now contain the following structure:
#
Step 2: Move the Existing Web Server Configuration into the Module
Open Your Web Server Configuration:
- Locate your existing web server configuration (typically defined in
main.tf).
- Locate your existing web server configuration (typically defined in
Move the Web Server Configuration to the Module Directory:
- Move the resource blocks that define the web server (e.g., Virtual Machine, Network Interface, Public IP) from
main.tfto a new file namedmain.tfinsidemodules/webserver.
- Move the resource blocks that define the web server (e.g., Virtual Machine, Network Interface, Public IP) from
Example
main.tffor the Module:- In
modules/webserver/main.tf, the configuration might look like this:resource "azurerm_network_interface" "example" { name = "example-nic" location = var.location resource_group_name = var.resource_group_name ip_configuration { name = "internal" subnet_id = azurerm_subnet.example.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_virtual_machine" "example" { name = "example-vm" location = var.location resource_group_name = var.resource_group_name network_interface_ids = [azurerm_network_interface.example.id] vm_size = "Standard_DS1_v2" storage_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "18.04-LTS" version = "latest" } os_profile { computer_name = "examplevm" admin_username = "adminuser" admin_password = "P@ssw0rd123!" } os_profile_linux_config { disable_password_authentication = false } }
- In
Save the Module Configuration.
#
Step 3: Define Input Variables in variables.tf for the Module
Create a
variables.tfFile in the Module Directory:- In
modules/webserver, create a new file namedvariables.tf.
- In
Define Variables for Resource Group Name and Location:
- Add variables to parameterize the Resource Group name and location:
variable "resource_group_name" { description = "The name of the resource group" type = string } variable "location" { description = "The location for the web server resources" type = string }
- Add variables to parameterize the Resource Group name and location:
Save the
variables.tfFile.
#
Step 4: Create outputs.tf to Capture and Return Useful Information
Create an
outputs.tfFile in the Module Directory:- In
modules/webserver, create a new file namedoutputs.tf.
- In
Define Outputs for Useful Information:
- Capture information such as the public IP address of the Virtual Machine:
output "vm_id" { description = "The ID of the virtual machine" value = azurerm_virtual_machine.example.id } output "public_ip_address" { description = "The public IP address of the virtual machine" value = azurerm_network_interface.example.ip_configuration[0].private_ip_address }
- Capture information such as the public IP address of the Virtual Machine:
Save the
outputs.tfFile.
#
Step 5: Call the Module in the Root Configuration (main.tf)
Open the Root
main.tfFile:- Open the
main.tffile in the root directory of your project.
- Open the
Add a Module Block to Call the
webserverModule:- Call the
webservermodule by specifying its source directory and passing in the necessary variables:module "webserver" { source = "./modules/webserver" resource_group_name = "myResourceGroup" location = "East US" }
- Call the
Save the
main.tfFile.
#
Step 6: Run Terraform Commands to Test the Module
Initialize the Project:
- In the terminal, run
terraform initto ensure that all module dependencies are loaded and the project is ready:terraform init
- In the terminal, run
Run
terraform planto Preview the Changes:- Run
terraform planto review the resources that will be created by thewebservermodule:terraform plan - Ensure that the output includes the expected resources, such as the Virtual Machine and Network Interface.
- Run
Run
terraform applyto Deploy the Module:- Apply the configuration to create the web server resources using the module:
terraform apply - Type
yeswhen prompted to confirm the apply operation.
- Apply the configuration to create the web server resources using the module:
Review the Output Values:
- After the apply completes, observe the output values defined in
outputs.tf, such as the VM ID and public IP address.
- After the apply completes, observe the output values defined in
#
Verification
Confirm Resource Creation in Azure:
- Log in to the Azure Portal and check the Resource Group specified in the module call.
- Verify that the web server resources (e.g., Virtual Machine, Network Interface) have been created with the correct settings.
Check Output Values:
- Ensure that the output values displayed in the terminal match the expected details for the VM and network configurations.