๐ Building Infrastructure as Code with Terraform Modules ๐
In the vast landscape of Infrastructure as Code (IaC), orchestrating the deployment and configuration of infrastructure components can be a formidable challenge. Fear not, for Terraform, a widely-adopted IaC tool, comes to the rescue, simplifying this process with its declarative configuration language. ๐ ๏ธ One of Terraform's standout features is the use of modules, empowering developers to achieve code reuse and maintainability. ๐
Understanding Terraform Modules ๐ฆ
Terraform modules, akin to neatly wrapped packages, consist of self-contained configurations that serve as the building blocks for expansive infrastructure deployments. Modules offer the power to encapsulate and reuse infrastructure code, embodying best practices such as modularity, abstraction, and reusability. ๐๏ธ
In this exhilarating journey, we'll delve into a straightforward example of employing Terraform modules to craft AWS EC2 instances with customizable configurations.
Project Structure ๐
my_demo_moduls/
|-- main.tf
|-- template/
| |-- my_variables.tf
| |-- my_instance.tf
|-- terraform.tf
|-- terraform.tfstate
|-- terraform.tfstate.backup
Terraform Configuration Files ๐งฉ
template/my_
variables.tf
variable "ami" {
type = string
description = "This is the AMI Id for EC2 Instance"
}
variable "instance_type" {
type = string
description = "This is the Instance Type for EC2 Instance"
}
variable "instance_name" {
type = string
description = "This is the Instance Name for EC2 Instance"
}
variable "env" {
type = string
description = "This is the env for Infra"
}
template/my_
instance.tf
resource "aws_instance" "my_demo_instance" {
ami = var.ami
instance_type = var.instance_type
tags = {
Name = "${var.env}-${var.instance_name}"
}
}
main.tf
module "my_dev_app" {
source = "./template"
env = "dev"
instance_type = "t2.micro"
ami = "ami-0c7217cdde317cfec"
instance_name = "my-ec2"
}
module "my_prod_app" {
source = "./template"
env = "prod"
instance_type = "t2.micro"
ami = "ami-0c7217cdde317cfec"
instance_name = "my-ec2"
}
terraform.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.63.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
Explanation ๐ฃ๏ธ
template/my_
variables.tf
: This file defines input variables for the module.template/my_
instance.tf
: Specifies an AWS EC2 instance resource using the input variables.main.tf
: Uses the module block to instantiate two instances of the my_dev_app and my_prod_app modules.terraform.tf
: Configures Terraform settings, including the required AWS provider.
Running Terraform ๐โโ๏ธ
Change into the
my_demo_moduls
directory.cd ~/my_demo_moduls
Initialize the Terraform configuration.
terraform init
Apply the configuration to create the EC2 instances.
terraform apply
Type "yes" to proceed.
After the deployment is complete, Terraform will output information about the created resources.
This revamped example adheres to your existing project structure, maintaining a clear distinction between module files and the main configuration. It showcases the prowess of modules in building AWS EC2 instances with configurations tailored to your needs. Happy coding with Terraform! ๐ง๐