Publishing Private modules on Terraform Cloud

Vaibhav Srivastava
5 min readSep 3, 2021

Modules are containers for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directory. A Terraform module is very simple: any set of Terraform configuration files in a folder is a module.

What does a module do?

A Terraform module allows you to create logical abstraction on the top of some resource set. In other words, a module allows you to group resources together and reuse this group later, possibly many times.

Reference:
https://www.terraform.io/docs/language/modules/index.html

What is Terraform Cloud?

Terraform Cloud is an application that helps teams use Terraform together. It manages Terraform runs in a consistent and reliable environment, and includes easy access to shared state and secret data, access controls for approving changes to infrastructure, a private registry for sharing Terraform modules, detailed policy controls for governing the contents of Terraform configurations, and more.

Creating a Terraform Module

Terraform modules allow you to reuse, share, and store your Terraform configurations using version control like Github. In the next steps, you will move your New Relic configurations into a reusable module.

First, in your project root, create a new directory to store your modules named modules. In the modules directory, create a new directory for a new module called VPC

Using input variables with modules is very similar to how you use variables in any Terraform configuration. A common pattern is to identify which module input variables you might want to change in the future and create matching variables in your configuration’s variables.tf file with sensible default values. Those variables can then be passed to the module block as arguments.

variable "vpc_cidr_block" {  
description = "CIDR block for VPC"
type = string
}
variable "vpc_name" {
description = "Name of VPC"
type = string
}
variable "environment" {
description = "This variable is for tagging"
type = string
}

create another file for VPC in main.tf

resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr_block
instance_tenancy = "default"
tags = {
Name = "${var.vpc_name}"
Environment = "${var.environment}"
}
}

Modules also have output values, which are defined within the module with the output keyword. You can access them by referring to module.<MODULE NAME>.<OUTPUT NAME>. Module outputs are usually either passed to other parts of your configuration or defined as outputs in your root module.

Sample Output file as output.tf, you can add more outputs based on your project requirements.

output "vpc_id" {
value = aws_vpc.main.id
description = "The ID of the VPC"
}
output "vpc_arn" {
value = aws_vpc.main.arn
description = "The ARN of the VPC"
}
screenshot to show folder content

Let's commit the code to our Github Repository

Add the changes that you want to commit, in my case I want to commit all the files in the current Modules folder

git add .git commit -m "Adding VPC Module"
screenshot from console/terminal

don't forget to add the tag while committing your code as it is important to publish the module

git tag -a v1.0 -m "my vpc version 1.0"

Now that our files are pushed to our GitHub repository, let's Login to Terraform cloud account

Click on “Publish private modules” and choose from available integration to connect to a version control provider where your code exists

This is one time activity, but since I am using Terraform Cloud for the first time, I have to perform a handshake between Terraform Cloud to my GitHub account so that it could look into my code repository for deployment pipeline setup

On Add VCS Provider page, click on the “register a new OAuth Application”, this will take you to your version provider page where you have to add the Application name, your homepage URL and Authorization callback URL which is provided by Terraform cloud

Then click on the Register application. This generates a unique Client ID and Client Secret, which you will enter on the Terraform Cloud VCS registration screen.

Once the setup is completed, you will see your repository visible under the “Choose a repository” section. Step 2 wants us to tell Terraform Cloud which repos to link to. It will show a list of your repositories. If you have a lot of them, there is a search bar under the selection area. Select the repository and click next

This is the final screen, lets validate our repository selection, ensure the Provider information and the selected repository is right. If everything looks right, click on Publish module

Now that our module is published, it will take us to the private modules page, where we can see a list of all modules that have been published by your organization.

Click on the listed module to view details on the usage and configuration.

Conclusion

Terraform is a powerful tool to enable you and your teams to define and deploy infrastructure in a controllable and maintainable way. Performing these best practices can help you to reduce downtime and allow engineers to focus on their primary job — providing business value.

TF Cloud’s solution is somewhat inflexible compared to other CI/CD solutions, but it is infinitely more simple than the other ones I’ve deployed so far.

Thanks all. Good luck out there!

--

--

Vaibhav Srivastava

Solutions Architect | AWS & Azure Certified | Hybrid & Multi-Cloud Exp. | Technophile