๐Ÿš€ Building Infrastructure as Code with Terraform Modules ๐ŸŒ

ยท

3 min read

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! ๐Ÿšง๐Ÿš€

Did you find this article valuable?

Support SagarOps by becoming a sponsor. Any amount is appreciated!

ย