#
Exercise: Creating a Configuration with Variables and Outputs
#
Objective
Participants will create a Terraform configuration that uses variables to define a Resource Group and outputs to display its name and location. This exercise will help participants understand how to use variables for flexible configurations and outputs to extract useful information.
#
Steps
#
Step 1: Create a variables.tf File with Input Variables
Create a
variables.tfFile:- Open your code editor (e.g., VS Code or IntelliJ).
- In your project directory, create a new file named
variables.tf.
Define Variables for Resource Group Name and Location:
- In
variables.tf, add the following code to define theresource_group_nameandlocationvariables:variable "resource_group_name" { description = "The name of the resource group" type = string default = "myResourceGroup" } variable "location" { description = "The Azure region to deploy resources" type = string default = "East US" } - These variables allow the configuration to dynamically set the Resource Group name and location, making it easy to reuse the code for different projects or environments.
- In
Save the
variables.tfFile.
#
Step 2: Create a main.tf File that Uses the Variables
Create a
main.tfFile:- In the same project directory, create a new file named
main.tf.
- In the same project directory, create a new file named
Add Resource Group Configuration Using Variables:
- In
main.tf, define a Resource Group resource and use the variables defined invariables.tfto set itsnameandlocation:resource "azurerm_resource_group" "example" { name = var.resource_group_name location = var.location } - Here,
var.resource_group_nameandvar.locationreference the values of the variables, making the configuration flexible.
- In
Save the
main.tfFile.
#
Step 3: Create an outputs.tf File to Display Resource Information
Create an
outputs.tfFile:- In your project directory, create a new file named
outputs.tf.
- In your project directory, create a new file named
Define Output Values for Resource Group Name and Location:
- In
outputs.tf, add the following code to define outputs that display the Resource Group’s name and location:output "resource_group_name" { description = "The name of the resource group" value = azurerm_resource_group.example.name } output "resource_group_location" { description = "The location of the resource group" value = azurerm_resource_group.example.location } - These output values will provide useful information about the provisioned resources when
terraform applycompletes.
- In
Save the
outputs.tfFile.
#
Step 4: Run terraform plan and terraform apply to Provision the Resource Group and Display Outputs
Initialize the Terraform Project:
- In the terminal, navigate to the project directory and run:
terraform init - This command will download necessary provider plugins and prepare the project for deployment.
- In the terminal, navigate to the project directory and run:
Run
terraform planto Preview the Changes:- Use
terraform planto verify the configuration and preview the changes Terraform will make:terraform plan - Review the output to ensure that Terraform will create a Resource Group with the specified name and location.
- Use
Run
terraform applyto Create the Resource Group:- Apply the configuration to create the Resource Group on Azure:
terraform apply - Type
yeswhen prompted to confirm the apply operation.
- Apply the configuration to create the Resource Group on Azure:
Review the Output Values:
- After
terraform applycompletes, check the terminal output to see the values forresource_group_nameandresource_group_location. - Example output:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: resource_group_name = "myResourceGroup" resource_group_location = "East US"
- After
#
Verification
Check the Azure Portal:
- Go to the Azure Portal and navigate to Resource Groups.
- Verify that the Resource Group named
myResourceGroupwas created in the specified location (e.g., "East US").
Confirm Output Values:
- Ensure that the output values for
resource_group_nameandresource_group_locationdisplayed in the terminal match the values in the Azure Portal.
- Ensure that the output values for