Monday, June 14, 2021

Terraform - Modules

 T of Terraform:

Terraform is a toll that helps us in building, changing and versioning in the resources of an infrastructure. Not necessarily though, because, Terraform an execute configuration files and request a API to bring the state of the infrastructure or that entity close to that as desired in configuration file (as much as possible). Terraform can serve multiple platforms including AWS, Azure, GCP, IBM-cloud, Alibaba cloud, VMware. To makes things interesting, these state dependent programs can handle any platform which serves requests from API call, when the providers are written for that platform. 

Talking about GCP, we can use existing provider, already written and just write down the configuration file and request API to achieve the state as provided in the configuration file. It has to be written in HashiCorp Configuration Language (HCL), wherein the descriptions of resources using blocks, arguments and expressions are provided.

Little into depth:

It is always better to organize things in such a way that they are easy to comprehend, well structured and reusable in many cases, but also not so vague to make the user rewrite a number of parameters every time they use it. Modules serve the purpose. A module can be created, stored and called whenever required. This is more like using classes and functions in other OOPs based languages. 

They can be organized in general style or any convenient method as preferred by the user. An example is shown, wherein the modules are stored under a single directory, categorized based in the resources they refer or work with. 

The environments such as global, production etc.. are kept separately and the modules are called and used whenever required, hence organizing it systematically and also using comfortably!

Example!

Here, I have tried to create couple of modules for resources and a code to use those modules to accomplish the task. This below is a module file of creating an instance. We can declare all the variables in the same file or make a separate file for that, Terraform reads all the files in the folder with the extension .tf  .

https://gist.github.com/group4pgs/1cf6a514c0ab8d78bae18bad20451b57


This file is saved in a location and the address is noted down, this helps in locating the module for the instance creation. We call this module in the way, shown below. One must make sure that every variable required by the module is addressed correctly.


https://gist.github.com/group4pgs/3ca92c45ec7094bcc0e386e767c203a7

 

Concluding things

Just one of the few functionalities that Terraform provides which are of great use. One can check the Official website of Terraform which already has many modules, preconfigured, you can directly refer to the github repo, and make use of the publicly available modules to build their own resources in an infrastructure. 

No comments:

Post a Comment