Terraform Module Loading: How to load modules from different sources

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Terraform, an open-source infrastructure as code software tool created by HashiCorp, allows users to define and provision data center infrastructure using a high-level configuration language known as HCL (HashiCorp Configuration Language). One of Terraform’s core concepts is the use of modules to organize and reuse code. Modules in Terraform allow you to package and encapsulate a set of resources and configurations for reusability and manageability. This tutorial will guide you through the process of loading Terraform modules from various sources, ranging from the local file system to remote repositories.

What are Terraform Modules?

Before diving into how to load modules from different sources, it’s crucial to understand what Terraform modules are. A Terraform module is essentially a container for multiple resources that are used together. Modules can be used to create reusable components in your Terraform configuration, simplify complex systems, and organize your infrastructure code.

Loading Modules from the Local File System

To begin with, loading modules from the local file system is as straightforward as specifying the path to the module in your main Terraform configuration file. Here’s how you can do it:

module "my_module" {
  source = "./modules/my-module"
}

In the above example, the source attribute points to a relative path where your module is located. This is the simplest form of module loading, well-suited for when the modules are maintained alongside your infrastructure code.

Loading Modules from a Terraform Registry

The Terraform Registry is a repository for sharing Terraform modules and providers. You can load modules from the Terraform Registry by specifying the module’s registry format as the source. Here’s an example:

module "aws_network" {
  source = "terraform-aws-modules/vpc/aws"
  version = "2.77.0"
}

In this example, the module source points to the Terraform Registry URL for a specific version of the AWS VPC module. Using modules from the registry enables you to leverage community knowledge and reusable components for faster development.

Loading Modules from GitHub

Beyond local and registry sources, Terraform can load modules from various version control systems, including GitHub. Loading modules from GitHub is straightforward:

module "vpc" {
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-vpc.git?ref=v2.77.0"
}

This method specifies the version of the module to load using the ref parameter. Loading modules from GitHub or any version control system allows you to specify the exact version or branch you want to use, which is beneficial for maintaining consistency across environments.

Advanced Source Techniques

For more complex scenarios, such as when you want to load modules from a private repository or specify a different branch, you might need to use more advanced source techniques.

Using SSH for Private Repositories

module "private_repo" {
  source = "git::ssh://[email protected]/terraform-aws-modules/terraform-aws-vpc.git?ref=tags/v2.77.0"
}

By using SSH, you can securely access private repositories. This method requires SSH authentication setup but offers a secure way to access and load modules from private sources.

Using Local Paths for Module Development

module "local_dev" {
  source = "../local-modules/my-module"
}

When developing new modules or testing changes, using a local path source allows for easy and immediate iteration without the need to push changes to a repository. This method is especially useful during the development phase of module creation.

Conclusion

Terraform’s module system is powerful and versatile, supporting a wide range of sources to load modules. By understanding how to effectively load modules from the local file system, the Terraform Registry, version control systems like GitHub, and private repositories, you can significantly improve the reusability and manageability of your Terraform code. With practice and exploration, leveraging different module sources becomes an invaluable skill in optimized Infrastructure as Code development.