A Terraform Registry tutorial to build and share modules (original) (raw)

Software development encourages code reuse through reusable artifacts, such as libraries, packages and modules. Most programming languages enable developers to package and publish these reusable components and make them available on a registry or feed. For example, Python has Python Package Index and PowerShell has PowerShell Gallery.

For Terraform users, the Terraform Registry enables the distribution of Terraform modules, which are reusable configurations. The Terraform Registry acts as a centralized repository for module sharing, making modules easier to discover and reuse.

The Registry is available in two variants:

This tutorial will walk through the process to set up a module repository on GitHub, and then publish it to the public Terraform Registry.

Module prerequisites

As Terraform documentation outlines, a module must meet the following requirements to publish to the Terraform Registry:

Create a Terraform module

Let's create a GitHub repository to house our module, which enables us to create an Azure resource group. (This Terraform Registry tutorial features this sample public GitHub repository.)

Ensure the public repository meets the above prerequisites. For example, the repository name should follow the specified convention, and appear as terraform-azurerm-rg. Choose to initialize the repo with a README.gitignore file and a MIT License. Hit create repository.

sample new repository

Now clone this repository locally on your development workstation and open it in Visual Studio Code.

local repository

Next, start to put the basic module together. Populate the main.tf, variables.tf and outputs.tf files as per the content in the referenced repository.

Now, test this module locally by referencing it in a Terraform configuration. Create a sample Terraform configuration named tf_azure_rg.tf under any other directory than where you cloned the module. Here, we place the configuration one level up in the parent directory from the Terraform module repository.

Initialize the Terraform configuration and run the terraform plan or terraform apply command. This validates that the module works as it should.

sample Terraform config

Save all files under the Terraform module repository path, commit them and push to the remote GitHub repository. Also, add a Git tag to the repository using the version naming convention v.. -- for example, v0.0.1.

save, commit, push files

Steps to publish a module

The steps in this section only need to be followed once per repository to publish a module to the public Terraform Registry.

First, visit the registry and log in using your GitHub account.

Terraform registry

Click Publish once logged in.

publish Terraform module

Since we followed the module prerequisites to name the GitHub repository, the module repository is discoverable. Select the repository and hit Publish Module.

Select Terraform registry

Once the module is published, we will automatically be taken to the module page in the Terraform Registry.

Terraform Registry module page

At this point, we've published a module from the GitHub repository to the public Terraform Registry. Next, we can use the module in Terraform configurations.

Reference a published module

Open the old Terraform configuration file -- the tf_azure_rg.tf file we authored initially. The instructions are available on the module's landing page on the Terraform Registry as well.

Terraform provision instructions

Modify the source to use the public Terraform Registry module and specify the version to use.

TF registry source and version

Run the terraform init command again, since the source reference has changed. This downloads the referenced module.

Run terraform init command

To finish this Terraform Registry tutorial, run either the terraform plan or terraform apply command to confirm that referencing a public module in the Terraform configuration works.

confirm public module reference