Get startedGet started for free

Introduction to Terraform Modules

1. Introduction to Terraform Modules

Let’s get started with an introduction to Terraform modules. Before defining modules, let’s view a simple scenario demonstrating why modules are necessary. Let’s say you must create a web server in a custom network. Typical attributes that represent the web server include: The machine type, disk, a static IP address, and a Google service account. The associated code to deploy a server with these attributes is similar to the code shown in this example. What if you want to deploy several servers of the same kind? You might have to manually copy the code. What if you have to update one attribute for all these servers? You have to manually find and update that attribute in every occurrence. When new resources are added or the code is updated, the code becomes bigger, unmanageable, and harder to read. Copying the code becomes a cumbersome process where every little change has to be applied error-free across all environments. It also makes the code inefficient. Similar, duplicated blocks can cause discrepancies when updated. In the programming world, disadvantages of code repetition are addressed with the “Don’t Repeat Yourself” principle, also known as DRY. The idea of DRY is to avoid repeating the same set of codes multiple times. Instead, replace it with abstraction to avoid redundancy. This practice encourages building efficient code that is readable and reusable. In general-purpose programming languages like Ruby, Java, and Python, functions are used to implement the DRY principle. Any piece of code that has to be manually copied is placed inside a function and reused across the main code. With Terraform, you can place your reusable code inside a module and reuse that module in multiple places. Define the reusable code in a module named server, so that any changes you make to the module are reflected across all the environments that you plan to reuse. The definition of a module ia a group of one or more Terraform configuration files in a directory. Modules allow you to group a set of resources together and reuse them later, and they can be referenced from other modules. So far, we have unintentionally written our terraform configuration in a module, called the root module. The root module is the directory from which you run terraform commands. Notice that the root module consists of . tf files that are stored in your working directory. This working directory is where terraform plan and terraform apply are run. Other modules and resources are instantiated in the root module. Now that you know what modules are and why they’re useful, let’s examine the creation process. In this example, let’s create two modules, one called “server, and the other called “network”. The network module will contain networking resources to host the server. The server module will contain necessary compute resources that define the server. To create a module, first create a directory of Terraform files. In this example, create two directories, named network and server. Each directory has its own main.tf file. Next, enter the code in the main.tf file to create a custom network within the network directory. Notice that multiple resources are grouped in the network module. In the main.tf file of the server directory, enter the code to create a virtual server. And that’s all there is to it! Two modules have been created. At this point, you created the modules and wrote the configuration code, but this process does not create the infrastructure resources. These modules are instantiated when they are called with the terraform apply command, which you’ll learn about in the next lesson.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.