Skip to main content

Command Palette

Search for a command to run...

πŸš€ Building Infrastructure as Code with Terraform Modules 🌐

Published
β€’3 min readβ€’View as Markdown
S

πŸš€ Sagar Shah πŸš€

πŸ’» DevOps Engineer πŸ’‘ 🌐 Automating the digital universe, one script at a time βš™οΈ πŸ“š Passionate learner and tech enthusiast πŸ€“ ☁️ Cloud explorer, making servers dance in the cloud πŸŒ₯️ πŸ› οΈ Building bridges between development and operations πŸŒ‰ πŸ“– Sharing insights and knowledge on all things DevOps πŸ“’ 🌟 Let's transform the world of IT together! ✨

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 πŸƒβ€β™‚οΈ

  1. Change into the my_demo_moduls directory.

     cd ~/my_demo_moduls
    
  2. Initialize the Terraform configuration.

     terraform init
    
  3. Apply the configuration to create the EC2 instances.

     terraform apply
    

    Type "yes" to proceed.

  4. 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! πŸš§πŸš€