jaffar shaik
2 min readJan 5, 2021

Terraform Modules

In this article we will Teach you what is a module how to write write a module , how to call a module what is root module, child module.

Ø A module is a collection of .tf files

Ø the main use of mosules is reusability

In the above diagram we can see

Ø providers.tf means it consists of providers information.

Ø Main.tf means it consists of resources information

Ø Outputs.tf consists of attributes information of a resource

Ø Variables.tf consists of variables information of the resources

In the above diagram we can see

  • tf means it consists of providers information.
  • tf means it consists of resources information
  • tf consists of attributes information of a resource
  • tf consists of variables information of the resources

Definition of Module:

A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects

Procedure for using Modules:

  • Create a Folder and put it in .Tf file that defines the resource creation.this file is treated as parent Module.

Example:

resource "aws_instance" "myec23" {
ami = "ami-08f63db601b82ff5f"
instance_type = "t2.large"
tags = {
name = "myec3"
}
}
output "hello" {
value = aws_instance.myec23.id
}

In the above example we referred code to create a server and in order to view the id of the server on the console we used ID attribute.

Calling the module:

To call a module we need to specify the code of parent module in source block .

The syntax is as follows:

module "helloworld"
{
source = "./parentmoduleloaction”
}
To acess the ouput values in modules we need to fallow the syntax
output "vn"{
value=module.mn.routputvariablename.atrn
}

Here in this example “helloworld is module name and Hello is the output variable name of the parent module.

Publishing Modules to Terraform Registry:

Once the module is created we can publish it to terraform registry.

The steps are as follows:

Step1:we need to push the code to GITHub

Procedure for pulling Code to Github.

Execute the following list of commands

Ø create a repository that is a Folder

Ø Intitize the Repositoty

Ø Execute the command git init

Ø Add the files using command git add .

Ø git commit -m “myfirstcommitec2”

Ø git tag -a “v1.o” -m “version1 code”

Ø git push

Ø git push -uf origin master –fallow-tags

step2:

Step2:we need to login to https://registry.terraform.io/ and click on publish module.

Wow that’s cool you created and published module successfully.hope this artcle helpful for you in creating and publishing modules.

jaffar shaik
jaffar shaik

Written by jaffar shaik

Am DevOps Engineer and SRE based in india.

No responses yet