How to implement DRY principles in terraform with modules

DRY principles are crucial for your codebase’s scalability in any situation. If you frequently create the same components in Terraform, this blog is for you.

DRY is an acronym that stands for Don’t Repeat Yourself. That is, if your code accomplishes the same objective, you should not repeat it. For example, if you construct a virtual machine or an EC2 instance, you will add several peripherals such as an interface, an IP address, and certain security rules. If you are building another ec2 for another environment that serves the same function, you should not duplicate the code but rather execute the code from the previous environment. Which we shall discover in this blog with terraform modules.

Understanding of arguments and attributes in resource blocks

First, we need to understand the simple resource blocks well, Let’s understand this with the below example of one resource group and virtual network.

Understanding Arguments and Attributes
Logical understanding of the above code

The code in the top image converts logically into the left image, we are creating two blocks namely rg and vnet which are in yellow color. Arguments are the inputs required to run the block which is represented in red. After passing the arguments and creating the respective resource, they give out attributes that will help be connected or used by other blocks that are called attributes, which are in blue color. Here for rg, name and id are attributes, and also vnet block uses the name attribute of rg

Similarly for modules also, we need to create arguments to make customizations with user inputs and also need attributes so that this module will be connected and useful to others as well

How to structure the modules with folders

The workspace border in Terraform is a folder, which implies that if your code is spread across two folders, you must import one from the other or it will not work. It also means that you can have numerous tf files inside a folder and not have to import anything across those files.

--virtual_machine_module
  --vm_main.tf
  --vm_variables.tf
  --vm_outputs.tf
--env1
  --env1_main.tf
--env2
  --env2_main.tf

So, from the previously mentioned file structure, we have the virtual machine module folder, which contains the code for creating virtual machines. Assume you need to construct two environments with virtual machines that use the same code, thus we generated two directories env1 and env2 that call that virtual machine module. I hope you understand why we need multiple directories.

How are modules called?

There is a module block, which is similar to fundamental blocks like resource, variable, and output. which defines as follows

simple module block

To define the block, use the keyword “module” as you would for any other block, and then give it a unique name. The most significant argument that you must present is the source, as indicated in the image. The value of the source argument should be the path to the folder containing your module.

I hope you now understand the fundamentals of DRY principles and how to use them in terraform. In subsequent blogs, I will go into greater detail with examples. Thank you for reading. Happy infra!!

Leave a comment

Your email address will not be published. Required fields are marked *