How to publish Terraform module in Terraform registry

In this post let's publish a module in Terraform. We will be creating a module to create an S3 bucket then we will be publishing it to Terraform registry.

Anil Augustine Chalissery
4 min readAug 2, 2022

Introduction

It was a few months back I started using Terraform modules, till then I was more into root module setup. But using the root module for large projects will be a hectic task. Since I discovered Terraform module I never went back to the root module setup due to its convenience. When I started using modules in Terraform I came to know we can publish modules in Terraform. Anyone can publish and consume providers and modules on the public Terraform Registry. It's very easy to publish a public Terraform module. There are only a few requirements that need to be fulfilled to publish a terraform module. Now let's check into the requirements by Terraform to publish a module.

Requirements

These are the requirements specified by Terraform to publish a public Terraform module

  1. GitHub. The module must be on GitHub and must be a public repo. This is only a requirement for the public registry. If you’re using a private registry, you may ignore this requirement.

2. Named terraform-<PROVIDER>-<NAME>. Module repositories must use this three-part name format, where <NAME> reflects the type of infrastructure the module manages and <PROVIDER> is the main provider where it creates that infrastructure. The <NAME> segment can contain additional hyphens. Examples: terraform-aws-ec2instance.

Here as we are creating an S3 bucket in AWS we will be naming the repo as terraform-aws-s3bucket

3. Repository description. (optional) The GitHub repository description is used to populate the short description of the module. This should be a simple one-sentence description of the module.

4. Standard module structure. The module must adhere to the standard module structure. This allows the registry to inspect your module and generate documentation, track resource usage, parse submodules and examples, and more. main.tf, variables.tf, outputs.tf. These are the recommended filenames for a minimal module, even if they're empty. main.tf should be the primary entry point. For a simple module, this may be where all the resources are created.

Standard module structure

For this let's make a simple s3 bucket with minimal configuration.

main.tf

In variables.tf let's declare the name variable as a string type.

The output.tf is optional but it will be good to at least commit an empty file. we are outputting arn of the S3 bucket with output.tf.

I also added README.md with some basic explanations. once you are ready with files we can commit and push them to the Github repo.

5. x.y.z tags for releases. The registry uses tags to identify module versions. Release tag names must be a semantic version, which can optionally be prefixed with a v. For example, v1.0.4 and 0.9.2. To publish a module initially, at least one release tag must be present. Tags that don't look like version numbers are ignored.So here we are using 0.0.1

Created release tag

Publishing a Public Module

As the requirements are met, we can now publish a public module by going to the Terraform Registry and clicking the “Upload” link in the top navigation.

publish -> module

If you’re not signed in, this will ask you to connect with GitHub.

The upload page will list your available repositories, filtered to those that match the naming convention described above. This is shown in the screenshot below. Select the repository of the module you want to add and click “Publish Module.”

In few moments our module will be published

Publishing Module

Once the publishing ends you will be directed to your module in Terraform registry

After publishing public Terraform module

Voila..! We successfully published our Terraform Module. Now if you set up a Readme file it would be populated there as follows

Readme generated

If we need some changes to be done, we can edit the repo and create a new version. This new version will be automatically brought to the module registry. So I hope this post has helped you to publish a public terraform module. Happy Terraforming folks..!

Reference

--

--