Day 70 - Terraform Modules ????
Below is the format on how to use modules:
Creating an 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}"
? }
}
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 of Terraform.
Terraform is a well-known open-source infrastructure as code (IAC) application for managing and provisioning cloud resources across numerous cloud providers. One of Terraform's primary advantages is its modular architecture, which allows you to divide your infrastructure code into smaller, reusable components known as modules. This simplifies the management of complicated infrastructures and encourages code reuse.
There are different types of modules in Terraform, including:
1. Root modules: The top-level module that acts as the entry point for your entire Terraform configuration is the root module. It usually comprises your provider setup as well as any variables required to specify 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 Terraform published module is one that has been shared with the community via the Terraform Module Registry. The Terraform Module Registry is a publicly accessible repository where users can share their modules with others in order to promote code reuse and collaboration.
Each module includes a set of resources and configurations that can be managed separately. This simplifies the management and testing of infrastructure code and encourages code reuse throughout your organization. You can write your own custom modules or use ones from the Terraform Module Registry, a public repository of Terraform modules supplied by the community.
2. Difference between Root Module and Child Module.
In Terraform, the primary distinction between a root module and a child module is their involvement in the overall configuration.
A root module is a Terraform configuration's top-level module. It is the starting point for your whole Terraform configuration, and it usually includes your provider configuration as well as any variables required to specify your infrastructure resources. A root module is in charge of launching Terraform, configuring providers, and defining resources.
A child module, on the other hand, is a sub-module that is nested within the root module or another child module. It is used to bundle together related resources and setups for better management. A child module is often developed to include a collection of resources that fulfill a certain function. Child modules are reusable components that can be utilized in many root modules at the same time.
A module block, which describes the source of the module and any input variables that need to be set, can be used to define child modules within a root module. A local file path or a remote site, such as a version control system or a Terraform Module Registry, can be used as the source.
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 thing, but they may appear similar in some instances.
In Terraform, a module is a self-contained unit of infrastructure code that contains a collection of linked resources and can be reused across many configurations. Modules facilitate code reuse, modularity, and abstraction in Terraform, making it easier for users to manage complicated infrastructures.
A namespace, on the other hand, is a method of organizing names or identifiers in order to avoid naming conflicts. In programming, namespaces are widely used to avoid naming conflicts between various modules, functions, or variables. They enable the grouping of related names under a common prefix or label.
While both modules and namespaces are used to organize and abstract data, they serve different functions in various settings. In Terraform, modules are used to organize and reuse infrastructure code, whereas namespaces are used in programming to organize and avoid naming conflicts between various identifiers.
Thank you for taking the time to read this! ??
Docker Captain @Docker.Inc ? Google Program Mentor ? Cloud Intern @Gavedu ? DevSecOps Culture ? Kubernetes ? 3x Azure Certified ? Technical Speaker ??
1 年Thanks for sharing Vinay Kumar
Dev(Sec)Ops Engineer ? Mentoring
1 年quite informative..