Quantcast
Channel: Cloud Training Program
Viewing all articles
Browse latest Browse all 1900

What are Terraform Modules and their purposes?

$
0
0

Loading

You must be somewhat familiar with Terraform’s fundamentals and have used it at least once, I’m assuming. If not, read this introduction to Terraform before moving on.

It is simpler to expand your infrastructure and maintain a tidy configuration with Terraform. Yet when the infrastructure expands, maintaining a single directory gets challenging. Terraform modules can be used in this situation.

In this article, you will discover what Terraform modules are, how to utilize them, and what issues they resolve.

  1. What Is a Terraform Module?
  2. Types of Terraform Modules
  3. What does a module do?
  4. Why Modules?
  5. Module: Input
  6. Module: Output
  7. Module best practices
  8. Conclusion

What Is a Terraform Module?

In Terraform,  Modules are groups of .tf files that are kept in a different directory from the configuration as a whole. A module’s scope encompasses all of its resources. So, if the user needs information about the resources that a module creates, the module must be explicitly stated. To do this, declare an output on the module that exposes the necessary data and permits references to that output from outside the module.

tf modulesA typical module would resemble this:

. 
├── LICENSE 
├── README.md 
├── main.tf 
├── variables.tf 
├── outputs.tf

Types of Terraform Modules

With Terraform, you are most likely to come across one of three main categories of modules:

  1. Root Module
  2. Child Module
  3. Published Modules

module types

Modules – Local and Remote

Both local and distant sources can be used to load modules. The majority of version control systems, HTTP URLs, the Terraform Registry, and private module registries in Terraform Cloud or Terraform Enterprise are just a few of the external sources that Terraform supports.

What does a module do?

By building logical abstraction on top of a resource set, you may use a Terraform module. To put it another way, a module enables the grouping of resources for subsequent reuse—possibly repeatedly.

Assume we have a cloud-hosted virtual server with various functionalities. Which group of resources would best sum up that server? For instance:

  1. A virtual machine built from a picture
  2. A block device with a connected storage space that is a specific size.
  3. A public static IP address that is assigned to the server’s virtual network interface
  4. A set of firewall guidelines that should be attached to the server
  5. Other factors, such as an extra network interface or block device.

Let’s now imagine that you will frequently need to establish this server with a certain set of resources. You don’t want to keep repeating the same setup code, do you? That’s where modules come in incredibly handy.

Why Modules?

  • Organise Configuration: Easier to navigate, understand, and update your configuration
  • Encapsulate Configuration: Helps prevent unintended consequences
  • Re-use Configuration: Share modules that you have written with your team or the general public
  • Consistency: help to provide consistency in your configurations

Module: Input

  • Serve as parameters for a Terraform module
  • Input variables are the API of the Modules
  • For variables in child modules, the calling module should pass values in the module block.
  • Each input variable accepted by a child module must be declared using a variable block
  • the variable declaration can also include a default argument
resource "aws_instance" "appserver" { 
    ami = var.ami
    instance_type = "t2.medium"
    tags = { 
        name = "${var.app_region}-app-server" 
    }
}
module "primaryapp" { 
    source = "./modules/appstorage"
    app_region = "us-east-2"
    ami = "ami-0010d386b82bc06f0"
}

Module: Output

  • The module can also return values
  • access module output variables using the module.<MODULE_NAME>.<OUTPUT_NAME>
  • Description argument is used to inform the module user what is contained in the output
  • value argument takes an expression whose result is to be returned to the user
output "subnetid" {
    value = "aws_instnace.appserver.subnet_id"
}

Module best practices

  1. Terraform modules offer many of the same advantages as the notions of libraries, packages, or modules present in most programming languages. Real-world Terraform setups should nearly always employ modules to give the aforementioned advantages, much like practically any non-trivial computer application.
  2. We advise that all Terraform practitioners employ modules while adhering to the following best practices:
  3. Your provider should be named Terraform-<PROVIDER>-<NAME>. This protocol must be followed in order to publish to the Terraform Cloud or Terraform Enterprise module registries.
  4. Start out by considering modules as you write your setup. You’ll discover that the advantages of utilizing modules outweigh the effort required to utilize them effectively, even for somewhat complicated Terraform settings handled by a single person.
  5. To structure and contain your code, employ local modules. Even if you don’t use or publish remote modules, structuring your configuration in terms of modules from the start will significantly lessen the work required to maintain and update your configuration as your infrastructure becomes more complex.\
  6. To locate relevant modules, utilize the Terraform Registry. By using the efforts of others to construct typical infrastructure scenarios, you may implement your setup more quickly and confidently.
  7. Modules may be shared and published with your team. The majority of infrastructure is maintained by teams, and modules are a crucial tool for teams to collaborate while building and maintaining infrastructure. You have the option to publish modules privately or openly, as was already described.

Conclusion

This essay went into great detail on the Terraform Module. And to be honest, if you understand how to use Terraform Modules, you’ll have a lot of new options for organizing the growth of your infrastructure code.

Also, you don’t always need to build a module from start. We can download and make modifications to any module from any cloud provider by searching for it on GitHub and the Terraform Registry, which will save us a lot of time.

Related/References

Join FREE Class

Join our FREE Class to know more about Terraform and get access to all Hands-On labs that you must perform to clear the Terraform Certified Associate certification exam.

Click on the below image to Register for Our FREE Masterclass Now!

Terrafrom free class

 

The post What are Terraform Modules and their purposes? appeared first on Cloud Training Program.


Viewing all articles
Browse latest Browse all 1900

Trending Articles