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
}