What are Terraform Modules?
Modules are
containers
for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directoryA module can call other modules, which lets you include the child module's resources in the configuration concisely.
Modules can also be called multiple times, either within the same configuration or in separate configurations, allowing resource configurations to be packaged and re-used.
Key Points to Remember:
Abstraction: Modules provide a level of abstraction. You can define the
inputs
andoutputs
of a module, which allows you to customize the behavior of the module when it is used in different contexts.Reusability: You can reuse modules across different parts of your infrastructure or in different projects, promoting
code reuse
andreducing duplication
.Encapsulation: Modules encapsulate related resources and their configurations, making it easier to understand and manage complex infrastructure setups.
Versioning: You can version your modules to ensure that different parts of your infrastructure are using the same version of a module, which helps in maintaining consistency.
Remote Modules: Modules can be stored remotely in version control systems (e.g., GitHub) or Terraform Module Registry. You can reference these remote modules in your configuration, making it easy to share and update them.
Directory Structure:
my_module/
├── main.tf
├── variables.tf
└── outputs.tf
Difference between Root Module and Child Module.
Root Module:
The main entry point for your Terraform configuration.
Configures the core components of your infrastructure.
Orchestrates the use of child modules.
Specifies providers, backends, variables, and outputs.
Can be applied independently to deploy your entire infrastructure.
Child Module:
Modular components encapsulating related resources and configurations.
Stored in separate directories.
Used within the root module or other child modules.
Promote code reusability and maintainability.
Can have input variables and output values.
Customizable and used multiple times within the infrastructure.
Root Module and Child Module Structure:
my-infrastructure/
├── main.tf (Root module)
├── variables.tf (Root module)
├── outputs.tf (Root module)
├── child-module-1/
│ ├── main.tf (Child module)
│ ├── variables.tf (Child module)
│ ├── outputs.tf (Child module)
├── child-module-2/
│ ├── main.tf (Child module)
│ ├── variables.tf (Child module)
│ ├── outputs.tf (Child module)
Are Modules and Namespaces are same?
Modules are a way to organize and reuse code components in Terraform for managing infrastructure.
Namespaces are used in various contexts, such as Kubernetes, to create isolated environments to prevent naming conflicts and manage resources separately.
Modules organize code, while namespaces isolate resources. They serve different purposes.
Task-01:
- Step-01: First create a folder named "modules/ec2_instance" in which we will create our module files like main.tf, variable.tf and output.tf.
- Step-02: Now create a file named main.tf in which we will create our aws_instance resource inside the module (modules/ec2_instance).
provider "aws" {
access_key = "provide the access key"
secret_key = "provide the secret key"
region = "ap-south-1"
}
resource "aws_instance" "example" {
ami = var.ami_value
instance_type = "t2.micro"
}
- Step-03: Now create a file named variable.tf in which we will create our input variables for our module (modules/ec2_instance).
variable "ami_value" {
description = "value for the ami"
}
variable "instance_type_value" {
description = "value for instance_type"
}
- Step-04: Now create a file named as output.tf in which we will create our output variables for our module (modules/ec2_instance).
output "public-ip-address" {
value = aws_instance.example.public_ip
}
- Step-05: Now let's create a main.tf file outside the module (modules/ec2_instance) in which we will call our module (modules/ec2_instance).
provider "aws" {
region = "ap-south-1"
}
module "ec2_instance" {
source = "/home/rohit/Documents/terraform/day12/modules/ec2_instance"
ami_value = "ami-099b3d23e336c2e83"
instance_type_value = "t2.micro"
}
So why do we create a module?
- We created a module because we wanted to use the same code again and again in our project.
- Step-06: Now let's run the terraform init command to initialize the terraform.
- Step-07: Now let's run the terraform apply command to create the resources.
- Now we can see that our module has been created.
- Step-08: Thus at last we can verify it on our AWS console.