Terraform Modules

Saloni Patidar
Google Cloud - Community
3 min readDec 2, 2022

This article describes the use and benefits of modules in Terraform, and its implementation.

Terraform implements the DRY (don’t repeat yourself) principle using modules, with which grouping of sets of resources is possible for later reuse. Modules are very similar to the functions that are used in programming languages to avoid repetition of code.

Why use modules in Terraform?

  • Readable : Eliminates lines of code by calling source module.
  • Reusable : Modules can be written once and reused multiple times.
  • Abstract : Separation of configuration into logical units.

At the end of this article you will be able to:

  • Define/ Create module.
  • Use module to reuse configuration
  • Use input variables to parameterize your configurations and outputs to pass resource attributes outside a module.

1. Defining Module in Terraform:

Terraform comes pre-installed in Cloud Shell. Verify that Terraform is available by running terraform in the cloud shell. You can also check the version of Terraform by running terraform — version.

  1. Create a directory of terraform files.
  2. Each directory has its own main.tf file.
  3. Write the code to create your requirements in the main.tf file.

For example, if you want to create a network configuration file, create a network.tf file and write the following code.

# Create network
resource "google_compute_network" "net1" {
name = "net1"
auto_create_subnetworks = "true"
}
# Add a firewall rule on net1
resource "google_compute_firewall" "net1-allow-http-ssh-rdp-icmp" {
name = "net1-allow-http-ssh-rdp-icmp"
network = google_compute_network.net1.self_link
allow {
protocol = "tcp"
ports = ["22", "80", "3389"]
}
allow {
protocol = "icmp"
}
source_ranges = ["0.0.0.0/0"]

2. Use module to reuse configuration:

To use the module, call the module to reference the code in the module block.

The parent module uses the source argument to call the defined modules. Run terraform init command to download any module reference by a configuration. Specify the source argument i.e. the mandatory meta argument which defines the location of module source.

Module Sources:

Local path:

Used to reference a module stored within the same directory as the calling module.

module "web-server"{
source = ./server
}

Use module from public registry:

Terraform Registry:

This contains a registry of publicly available modules for various infrastructure modules.

Source name format : namespace/name/provider

Github Registry:

To use this, directly enter the Github URL where the source code is located.

Run the command terraform init to install the provider binary and initialize the new terraform configuration. Apply your configuration now by running the command terraform apply, once all the codes are written in the respective files.

3. Use input variables to paramaterize your configurations and outputs to pass resource attributes outside a module.

Variables help you to customize the aspects of the module without altering the source code. They are useful when working with different environments such as production, development and staging.

  1. Replace hard coded value within the module with the variable.
resource "google_compute_network" "net1"{
name = var.<variable_name>
}

2. Create a file variable.tf and declare the variables in the file.

variable "<variable_name>" {
type = string
description = "description about the variable"
}

3. Pass the value for the input variable when you call the module.

module "prod_network"{
source = ./network
<variable_name> = "mynet1"
}

(Replace <variable_name> with the name you want to give to your variable.)

Passing value to a variable at runtime is not possible. Therefore, to pass resource arguments from one module to another the arguments are configured as an output value in Terraform.

  1. To pass a attribute from a module, declare the name of the attribute as an output value to expose it outside the module.
  2. Define this attribute name as a variable in the module that needs to use the value.
  3. Refer to the output value when calling the module that needs to use the value.

Note: You can refer the output value by using the format module.<module_name>.<output_value>.

Conclusion:

In Terraform, the concept of modules is used to reuse configuration and to write effective, reusable and readable code. Further, consider the best practices for writing modules.

--

--