Publish Terraform Module

Reading Time: 3 minutes

So, Before moving to publishing terraform modules , if you guys dont have any idea about what is terraform modules , then Just go through my this blog first.

Ok, Lets get started without wasting any time. So we saw in the previous blog that how we can use the module locally or create them. But what if i want to publish my own module. And use them anywhere just by providing the source same as the ‘public terraform registry provides the functionality’ ?

We can achieve thatas well by creating our own “Private Modul Registry”. And sharing our module there.

So before moving to the steps that how we are gonna achieve that , Let’s see the

Prerequisites:

  1. A Terraform Cloud account.
  2. A GitHub account
  3. An AWS account with IAM Access Keys
  4. OAuth Access to GitHub configured. If you belong to an organization where you don’t have permission to configure this access, you can create a new organization for this 

Steps to follow:

  1. Create the repository ‘terraform-aws-ec2′ , you can give you own what ever you want. Or you can directly clone my repo. Review the module configuration for the ‘ec2’ module. The repository looks similar to the directory structure below.


The variables.tf  file will define your Terraform Cloud variables as inputs for the ‘aws_instance’ resource that Terraform will create.


Note: In order to publish modules to the module registry, module names must have the format terraform-<PROVIDER>-<NAME>, where <NAME> can contain extra hyphens.

If you name your repo instead of this format just rename it.

2. Tag a Release:

Terraform Cloud modules should be semantically versioned, and pull their versioning information from repository release tags. To publish a module initially, at least one release tag must be present. Tags that don’t look like version numbers are ignored. Version tags can optionally be prefixed with a v.

To tag a release, under your repository’s Code tab, click on the tag icon, which says “0 releases.” As i alraedy created a release for my repo that’s why it is showing “tag 1” in following dig.

After clicking on tag you will get the following page. Just Select Choose a tag , and put you release version you want to provide e.g “v1.0.0” . Then click on “publish Release”

Now that you have created and versioned your module, you need to add a VCS connection to your Terraform Cloud organization.

NOTE: The Private Module Registry requires OAuth verification to GitHub. If you haven’t already, follow the OAuth instructions before proceeding.

3. Publish Module: To publish your module into your “private module registry”. Fist Login to your terraform cloud account. Select “Registry” –> “Modules” –> “Publish” from the upper right corner.

Choose the GitHub(Custom) VCS provider you configured and find the name of the module repository “terraform-aws-ec2″

Select repo

Select the module and click the “Publish module” button.

Once you have added your module, notice the “Usage Instructions” portion. You will use these as the building blocks for your workspace configuration.

So In this way you can publish your own modules to your “Private Module Registry”.

Referances:

knoldus footer

Written by 

Sakshi Gawande is a software consultant at "KNOLDUS" having more than 2 years of experience. She's working as a DevOps engineer. She always wants to explore new things and solve problems herself. on personal, she likes dancing, painting, and traveling.