The real challenge for developers and software factories in recent years has been to stop writing the portions of code that are repeated between applications and even between teams. Hence the emergence of language, framework, and software packages that are easily reusable in several applications and that can be shared between several teams (such as NuGet, NPM, Bower, PyPI, RubyGems, and many others). In Infrastructure as Code (IaC) in general, we also encounter the same problems of code structure, its homogenization, and its sharing in the company.
We learned in the Provisioning infrastructure in multiple environments recipe of Chapter 2, Writing Terraform Configuration, some topologies of the structure of the Terraform configuration, that gave us a partial answer to the question of how to structure a Terraform configuration well. But that doesn't stop there—Terraform also allows you to create modules with which you can share Terraform configuration between several applications and several teams.
In this chapter, we will study the main stages of the modules, which are: the creation, use, and publishing of Terraform modules. We will learn about the creation of a Terraform module and its local use, as well as the rapid bootstrapping of the code of a module. We will also study the use of Terraform modules using the public registry or a Git repository. Finally, we will study how to test a module as well as an example of a CI/CD pipeline of a Terraform module in Azure Pipelines and GitHub Actions.
In this chapter, we cover the following recipes:
- Creating a Terraform module and using it locally
- Using modules from the public registry
- Sharing a Terraform module using GitHub
- Using another file inside a custom module
- Using the Terraform module generator
- Generating module documentation
- Using a private Git repository for sharing Terraform modules
- Applying a Terrafile pattern for using modules
- Testing Terraform module code with Terratest
- Building CI/CD for Terraform modules in Azure Pipelines
- Building a workflow for Terraform modules using GitHub Actions
Technical requirements
In this chapter, for some recipes, we will need certain prerequisites, which are as follows:
- To have Node.js and NPM installed on your computer: The download website is here: https://nodejs.org/en/.
- To have a GitHub account: If you don't have one, the creation of the account is free and can be done here: https://github.com/.
- To have an Azure DevOps organization: You can create one with a Live or GitHub account here: https://azure.microsoft.com/en-in/services/devops/.
- To have a basic knowledge of Git commands and workflow: The documentation is available here: https://git-scm.com/doc.
- To know about Docker: The documentation is here: https://docs.docker.com/.
- To install Golang on our workstation: The documentation is here: https://golang.org/doc/install. We will see the main steps of its installation in the Testing a Terraform module using Terratest recipe.
The complete source code for this chapter is available on GitHub at https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05.
Check out the following video to see the Code in Action: https://bit.ly/3ibKgH2
Creating a Terraform module and using it locally
A Terraform module is a Terraform configuration that contains one or more Terraform resource. Once created, this module can be used in several Terraform configuration files either locally or even remotely.
In this recipe, we will look at the basics of a Terraform module with the steps involved in creating a module and using it locally.
Getting ready
To start this recipe, we will use the Terraform configuration that we have already written in the Provisioning infrastructure in multiple environments recipe in Chapter 2, Writing Terraform Configuration, and whose sources can be found at https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05/sample-app.
The module we will create in this recipe will be in charge of providing a Service Plan, one App Service, and an Application Insights resource in Azure. Its source code is available here: https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05/moduledemo/Modules/webapp. Then, we will write a Terraform configuration that uses this module and the code is here: https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP05/moduledemo/MyApp.
How to do it…
To create the module, perform the following steps:
- In a new folder called moduledemo, create the Modules and webapp folders.
- In the webapp folder, create a new variables.tf file with the following code:
variable "resource_group_name" {
description = "Resource group name"
}
variable "location" {
description = "Location of Azure resource"
default = "West Europe"
}
variable "service_plan_name" {
description = "Service plan name"
}
variable "app_name" {
description = "Name of application"
} - Then, create the main.tf file with the following code:
resource "azurerm_app_service_plan" "plan-app" {
name = var.service_plan_name
...