TLDR: This blog post explores Terraform modules, their importance in infrastructure management, and provides a step-by-step demonstration of creating and using modules in Terraform projects.
Hello everyone, my name is Abhishek and welcome back to my channel. Today marks day three of the Terraform Zero to Hero series. In this video, we will dive deep into one of the most important concepts of Terraform: modules. We will cover both the theoretical aspects, including scenarios and advantages of using modules, as well as a practical demonstration where we will write a regular Terraform project and convert it into a Terraform module.
Recap of Day Two
Before we begin, let’s quickly recap what we learned in day two:
Providers in Terraform: We explored what a provider is and how to use multiple providers in a single Terraform project.
Multiple Regions: We learned how to automate infrastructure across different regions using Terraform.
Variables: We understood the concept of variables, output variables, and how to pass values to these variables.
Conditional Expressions: We also covered conditional expressions in Terraform.
All of these concepts will be utilized in today’s video as we create our initial project and convert it into a Terraform module.
plugin → hashicorp terraform , hashicorp hcl —> give suggestion while writing code |
main.tf → provider (where to create? → AWS,AZURE,GCP) and resource (what to create? →EC2,S3,VPC,EKS) |
variables.tf → all variables without default value |
terraform.tfvars → (.env) → consist of all default value , which you don’t want to expose to world |
outputs.tf → print information to terminal → value = <resource>.<resource-name>.<> |
—> terraform init → terraform plan (overview of steps) → terraform apply (create resource) → terraform destroy (remove resource) |
What is a Module in Terraform?
To understand what a module is, let’s take a real-life example. Imagine you are working in an organization with millions of lines of source code written in Java, and the company uses a monolithic architecture. In this scenario, if a new developer is assigned to fix a bug, they would have to sift through all the code, which could take a significant amount of time. This lack of modularity leads to challenges in ownership, maintenance, and testing.
Similarly, in Terraform, if you create a single project that encompasses all resources for a development team, it can become unwieldy. If a bug arises, it would be difficult for a new team member to identify the issue or understand who owns which part of the code. This is where the concept of modules comes into play.
Advantages of Using Modules
Using modules in Terraform provides several advantages:
Modularity: Just like microservices in software development, modules allow you to break down your infrastructure into manageable pieces.
Reusability: Once a module is created, it can be reused across different projects or by different teams.
Simplified Collaboration: Teams can work on different modules independently, reducing conflicts and improving collaboration.
Versioning and Maintenance: Modules can be versioned, making it easier to manage changes and updates.
Abstraction: Modules provide a level of abstraction, allowing users to interact with complex infrastructure without needing to understand all the underlying details.
Testing and Documentation: Modules can be tested independently, and documentation can be provided for each module, making it easier for others to use.
Scalability and Security: Modules can be designed to scale and can encapsulate security best practices.
Demonstration: Creating a Terraform Module
Now, let’s put these concepts into action. We will create a Terraform project using the concepts learned in day two and then modularize it.
Step 1: Setting Up the Project
We will start by creating a new Terraform project. In the project, we will create an EC2 instance using the AWS provider. We will use variables for the AMI, instance type, and subnet ID.
Step 2: Writing the Main Terraform File
In the main.tf
file, we will define the provider and the resource for the EC2 instance. We will also create a variables.tf
file to define our variables and a terraform.tfvars
file to provide values for these variables.
Step 3: Initializing and Applying the Configuration
After writing the configuration, we will run terraform init
, terraform plan
, and terraform apply
to create the EC2 instance. This will demonstrate how Terraform processes our configuration and provisions the resources.
Step 4: Adding Outputs
To provide useful information after the resource is created, we will add an outputs.tf
file to output the public IP address of the EC2 instance.
Step 5: Modularizing the Project
Now that we have a working Terraform project, we will modularize it. We will create a new folder called modules_ec2_instance
and move our Terraform files into this folder. We will then create a new main.tf
file in the root directory that references the module.
Step 6: Executing the Module
Finally, we will execute the module by running terraform init
, terraform plan
, and terraform apply
again. This will show how easy it is to reuse the module across different projects.
Conclusion
In this blog post, we explored the concept of Terraform modules, their advantages, and how to create and use them in a Terraform project. By modularizing our infrastructure, we can improve maintainability, reusability, and collaboration among teams. I encourage you to practice creating your own modules and explore the Terraform Registry for publicly available modules that you can use in your projects.
Thank you for watching, and stay tuned for future classes where we will learn more about Terraform and its capabilities!