0
0
Terraformcloud~30 mins

Module outputs in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Module Outputs in Terraform
📖 Scenario: You are building a Terraform configuration to deploy cloud resources. You want to organize your code using modules and then access the outputs from those modules in your main configuration.
🎯 Goal: Create a Terraform module that defines a resource and outputs a value. Then, in the main Terraform configuration, call the module and access its output value.
📋 What You'll Learn
Create a Terraform module with a resource and an output
Call the module from the main Terraform configuration
Access the module's output in the main configuration
💡 Why This Matters
🌍 Real World
Modules help organize Terraform code for reusable infrastructure components. Outputs allow sharing information between modules and the main configuration.
💼 Career
Understanding modules and outputs is essential for managing complex infrastructure as code in professional cloud engineering roles.
Progress0 / 4 steps
1
Create a Terraform module with a resource and output
Create a Terraform module directory called my_module. Inside it, create a file main.tf that defines a resource random_pet.example with length = 2. Then create an output called pet_name that outputs the random_pet.example.id.
Terraform
Need a hint?

Use the random_pet resource to generate a random name. Then create an output block with the exact name pet_name.

2
Call the module from the main Terraform configuration
In the main Terraform configuration file main.tf, add a module block called example_module that uses the source ./my_module.
Terraform
Need a hint?

Use a module block with the exact name example_module and set source = "./my_module".

3
Access the module output in the main configuration
Create an output in the main Terraform configuration called module_pet_name that outputs the value of module.example_module.pet_name.
Terraform
Need a hint?

Define an output block with the exact name module_pet_name and set its value to module.example_module.pet_name.

4
Complete the Terraform configuration with required provider
Add the terraform block with required providers specifying random provider with source hashicorp/random and version constraint "~> 3.0".
Terraform
Need a hint?

Use the terraform block at the top level with required_providers specifying the random provider.