Day 70 - Terraform Modules
Sayali Shewale
DevOps Engineer | AWS cloud Engineer | AWS DevOps | Linux | Docker | Jenkins | Terraform | Kubernetes | Ansible | Grafana
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 directory
A module can call other modules, which lets you include the child module's resources into the configuration in a concise way.
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.
Below is the format on how to use modules:
Creating a AWS EC2 instance:
resource "aws_instance" "server-instance" {
? # Define number of instance
? instance_count = var.number_of_instances
?
? # Instance Configuration
? ami? ? ? ? ? ? ? ? ? ? = var.ami
? instance_type? ? ? ? ? = var.instance_type
? subnet_id? ? ? ? ? ? ? = var.subnet_id
? vpc_security_group_ids = var.security_group
?
? # Instance Tagsid
? tags = {
? ? Name = "${var.instance_name}"
? }
}
The code provided is an example of a Terraform configuration file that creates an EC2 instance in AWS using the aws_instance resource. Here is a brief description of the different sections in this code:
Server Module Variables:
variable "number_of_instances" {
? description = "Number of Instances to Create"
? type? ? ? ? = number
? default? ? ?= 1
}
?
variable "instance_name" {
? description = "Instance Name"
}
?
variable "ami" {
? description = "AMI ID"
? default? ? ?= "ami-xxxx"
}
?
variable "instance_type" {
? description = "Instance Type"
}
?
variable "subnet_id" {
? description = "Subnet ID"
}
?
variable "security_group" {
? description = "Security Group"
? type? ? ? ? = list(any)
}
The code provided is an example of a Terraform configuration file that defines several variables that can be used to configure the creation of EC2 instances in AWS. Here is a brief description of each variable:
Server module Output:
output "server_id" {
description = "Server ID"
value = aws_instance.server-instance.id
}
The code provided is an example of a Terraform configuration file that creates an EC2 instance in AWS using the aws_instance resource and outputs its ID using the output section. Here is a brief description of the different sections in this code:
Task-01
Explain the below in your own words.
领英推荐
1.Write about different modules Terraform.
Terraform is a popular open-source infrastructure as code (IAC) tool that allows you to manage and provision cloud resources across multiple cloud providers. One of the key features of Terraform is its modular architecture, which allows you to break down your infrastructure code into smaller, reusable components called modules. This makes it easier to manage complex infrastructures and promote code reuse.
There are different types of modules in Terraform, including:
1.Root modules: The root module is the top-level module that serves as the entry point for your entire Terraform configuration. It typically includes your provider configuration and any variables that are needed to define your infrastructure resources.
2. Child modules: A child module is a sub-module that is nested within the root module or another child module. It can be used to group related resources and configurations together for easier management.
3. Published Modules: A published module in Terraform refers to a module that has been shared with the community through the Terraform Module Registry. The Terraform Module Registry is a public repository where users can share their modules with others to promote code reuse and collaboration..
Each module contains a set of resources and configurations that can be managed independently. This makes it easier to manage and test your infrastructure code, and promotes code reuse across your organization. You can create your own custom modules, or use existing modules from the Terraform Module Registry, which is a public repository of Terraform modules contributed by the community.
2.Difference between Root Module and Child Module.
The main difference between a root module and a child module in Terraform is their role in the overall configuration.
A root module is the top-level module of a Terraform configuration. It serves as the entry point for your entire Terraform configuration and typically includes your provider configuration and any variables that are needed to define your infrastructure resources. A root module is responsible for initializing Terraform, configuring providers, and declaring resources.
On the other hand, a child module is a sub-module that is nested within the root module or another child module. It is used to group related resources and configurations together for easier management. A child module is typically created to encapsulate a set of resources that perform a specific function. Child modules are reusable components that can be used in multiple root modules.
Child modules can be defined within a root module using a module block, which specifies the source of the module and any input variables that need to be set. The source can be a local file path or a remote location, such as a version control system or a Terraform Module Registry.
Another difference between root modules and child modules is their visibility of variables. A root module can declare and reference variables that are visible to all child modules, while a child module can declare and reference variables that are only visible within that module.
3.Is modules and Namespaces are same? Justify your answer for both Yes/No
No, modules and namespaces are not the same concept, although they may share some similarities in certain contexts.
A module in Terraform is a self-contained unit of infrastructure code that encapsulates a set of related resources and can be reused across multiple configurations. Modules help to promote code reuse, modularity, and abstraction in Terraform, allowing users to manage complex infrastructures more easily.
On the other hand, a namespace is a way of organizing names or identifiers in a way that avoids naming conflicts. Namespaces are commonly used in programming to avoid naming conflicts between different modules, functions, or variables. They provide a way to group related names together under a common prefix or label.
While both modules and namespaces are used for organization and abstraction, they serve different purposes in different contexts. Modules in Terraform are used to organize and reuse infrastructure code, while namespaces are used to organize and avoid naming conflicts between different identifiers in programming.
Thank you for reading!
TS/SCI CI Poly | SRE | Cloud SysAdmin | Cloud Cybersecurity Engineer | Cloud Security | DevSecOps | Cloud Engineer |
1 年Almost there. Congratulations
TS/SCI CI Poly | SRE | Cloud SysAdmin | Cloud Cybersecurity Engineer | Cloud Security | DevSecOps | Cloud Engineer |
1 年Great job