Day 70 - Terraform Modules ????

Day 70 - 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 directory
  • A module can call other modules, which lets you include the child module's resources in 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 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}"
? }
}        

  • resource "aws_instance" "server-instance": This line declares a resource block for creating an AWS EC2 instance. It uses the aws_instance resource type and assigns the logical name "server-instance" to this resource block. You can refer to this resource later using the reference aws_instance.server-instance.
  • instance_count = var.number_of_instances: This line specifies the number of instances to be created using the instance_count attribute. The value is taken from the number_of_instances variable, which you can define elsewhere in your Terraform configuration.
  • ami = var.ami: Here, the ami attribute specifies the Amazon Machine Image (AMI) to be used for the EC2 instances. The value is taken from the ami variable, which you can define with the desired AMI ID.
  • instance_type = var.instance_type: This line defines the EC2 instance type to be launched. The value is obtained from the instance_type variable, which allows you to specify the desired instance type.
  • subnet_id = var.subnet_id: Here, the subnet_id attribute specifies the ID of the subnet in which the EC2 instance will be launched. The value is taken from the subnet_id variable, which you need to define with the appropriate subnet ID.
  • vpc_security_group_ids = var.security_group: This line configures the security group(s) associated with the EC2 instance. The value is obtained from the security_group variable, which should contain the relevant security group ID(s).
  • tags = { Name = "${var.instance_name}" }: This block defines tags for the EC2 instance. In this case, it sets a tag named "Name" with the value specified in the instance_name variable. You can add more tags if needed by extending this block.

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:

  • number_of_instances: This variable defines the number of EC2 instances to create. It has a default value of 1 if no value is provided.
  • instance_name: This variable is used to set the name of the EC2 instance. It does not have a default value, meaning that the user must provide a value for this variable.
  • ami: This variable is used to set the ID of the Amazon Machine Image (AMI) to use for the EC2 instance. It has a default value of "ami-xxxx" if no value is provided.
  • instance_type: This variable is used to set the instance type of the EC2 instance. It does not have a default value, meaning that the user must provide a value for this variable.
  • subnet_id: This variable is used to set the ID of the subnet where the EC2 instance will be launched. It does not have a default value, meaning that the user must provide a value for this variable.
  • security_group: This variable is used to set the security group(s) that will be associated with the EC2 instance. It has a type of list(any) to allow the user to specify one or more security groups. It does not have a default value, meaning that the user must provide a value for this 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:

  • output "server_id": This line declares a new output named "server_id".
  • value = aws_instance.server-instance.id: This line sets the value of the output to the ID of the EC2 instance created earlier with the aws_instance resource. The format aws_instance.server-instance.id refers to the attribute id of the resource aws_instance named "server-instance".

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! ??



Gauri Yadav

Docker Captain @Docker.Inc ? Google Program Mentor ? Cloud Intern @Gavedu ? DevSecOps Culture ? Kubernetes ? 3x Azure Certified ? Technical Speaker ??

1 年

Thanks for sharing Vinay Kumar

Mesut Oezdil

Dev(Sec)Ops Engineer ? Mentoring

1 年

quite informative..

要查看或添加评论,请登录

Vinay Kumar的更多文章

  • DevOps Project - 4 ????

    DevOps Project - 4 ????

    Project Description The project aims to deploy a web application using Docker Swarm, a container orchestration tool…

    7 条评论
  • DevOps Project 3 ????

    DevOps Project 3 ????

    Project Description The project involves hosting a static website using an AWS S3 bucket. Amazon S3 is an object…

    3 条评论
  • DevOps Project -2 ????

    DevOps Project -2 ????

    Project Description The project is about automating the deployment process of a web application using Jenkins and its…

  • Day 80: DevOps Project 1 ????

    Day 80: DevOps Project 1 ????

    Project Description The project aims to automate the building, testing, and deployment process of a web application…

    2 条评论
  • Day 73 - Setup Grafana on AWS EC2 Instance ????

    Day 73 - Setup Grafana on AWS EC2 Instance ????

    Task: Set up grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance To enable…

  • Day 72 - Grafana ????

    Day 72 - Grafana ????

    What is Grafana? No matter where your metrics are kept, Grafana is an open-source data visualization and monitoring…

    4 条评论
  • Day71 - Terraform Interview Questions ????

    Day71 - Terraform Interview Questions ????

    1. What is Terraform and how it is different from other IaaC tools? HashiCorp's Terraform is an Infrastructure as Code…

  • Day 69 - Meta-Arguments in Terraform ???

    Day 69 - Meta-Arguments in Terraform ???

    When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage…

    4 条评论
  • Day 65 - Terraform Resources ????

    Day 65 - Terraform Resources ????

    Understanding Terraform Resources A resource in Terraform represents a component of your infrastructure, such as a…

    2 条评论

社区洞察

其他会员也浏览了