Create and handle reusable infrastructure with Terraform modules

Create and handle reusable infrastructure with Terraform modules

Overview:

In this blog, we will use Modules to easily create and handle multi-environment deployments. We will have three Environments which will be:-

  1. Dev

  2. QA

  3. Prd

What are Modules in Terraform?

Modules are like sets of instructions or Templates that help us to build parts of our infrastructure, and we can use them over and over again to create an organized and reusable infrastructure.

Creating an EC2 server, Connecting the server using SSH, Setting up Terraform on the Server and Linking the AWS Account with our server:-

We have to create an EC2 server, connect it using SSh, Set up Terraform and Link the AWS Account with the connected Server. Follow my blog:

Blog Link

Follow the Steps which are highlighted in the red area.

Creating Terraform and providers files:-

  • We have created the server, connected it using SSH, Installed Terraform and linked our AWS Account with our connected server.

  • First, Create a folder named terraform-project and navigate to it.

      mkdir terraform-project
      cd terraform-project
    

  • Create a file here by the command:

      vim terraform.tf
    

In this file paste this code:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.16.1"
    }
  }
}

Save and Exit the file.

  • Now create another file by the command:

      vim providers.tf
    

In this file paste this code:

provider "aws" {
  region = "us-east-1"
}

Creating S3 Bucket, EC2 Instance and DynamoDB:-

  • We will create an S3 bucket, an EC2 Instance and a DynamoDB table.

  • So first create a folder named modules, navigate to it and again create a folder named demo-app and navigate to demo-app.

      mkdir modules
      cd modules
      mkdir demo-app
      cd demo-app
    

We are in the demo-app directory.

  • We will create EC2 Instance first. Run the Command:

      vim ec2.tf
    

Paste this code in this file:

resource "aws_instance" "my-demo-instance" {
  ami           = var.ami_id
  instance_type = var.instance_type
  tags = {
    Name = "${var.env_name}-${var.instance_name}"
  }
}

Save and exit this file.

  • Now we will create a DynamoDB table. Run the Command:

      vim dynamo.db
    

Paste this code in this file:

resource "aws_dynamodb_table" "my-demo-table" {
  name         ="${var.env_name}-${var.table_name}"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "emailID"
  attribute {
    name = "emailID"
    type = "S"
  }
}

Save and exit this file.

  • We will create a S3 Bucket now. Run the Command:

      vim s3.tf
    

    Paste this code in this file:

      resource "aws_s3_bucket" "my-demo-bucket" {
        bucket = "${var.env_name}-${var.bucket_name}"
      }
    

  • We will use the Variables file to pass data so let's create this file also by the command:

      vim variables.tf
    

    In this file paste this code:

      variable "ami_id" {
        type = string
      }
    
      variable "instance_type" {
        type = string
      }
    
      variable "instance_name" {
        type = string
      }
    
      variable "bucket_name" {
        type = string
      }
    
      variable "table_name" {
        type = string
      }
    
      variable "env_name" {
        type = string
      }
    

    Save and exit this file.

  • We have all the 4 files ready.

Creating the Template for Dev, Qa and Prd:-

  • Our main focus is to use the same code again and again in different Environments with different Configurations. Developers will not write code to launch anything every time instead they will use the Template and just modify their requirements in the Template file.

  • I will name this Template file as main. tf.

  • Navigate to the terraform-project directory by the command:

      cd ..
      cd ..
    

  • Now here run the command:

      vim main.tf
    

In this file paste this code:

#Dev Environment
module "dev-demo-app" {
source = "./modules/demo-app"
env_name = "dev"
instance_type = "t2.micro"
ami_id = "ami-053b0d53c279acc90"
instance_name = "batch4-demo-instance"
bucket_name = "batch-4-demo-bucket"
table_name = "batch4-demo-table"
}

#Qa Environment
module "qa-demo-app" {
source = "./modules/demo-app"
env_name = "qa"
instance_type = "t2.small"
ami_id = "ami-01c647eace872fc02"
instance_name = "batch4-demo-instance"
bucket_name = "batch-4-demo-bucket"
table_name = "batch4-demo-table"
}

#Prd Environment
module "prd-demo-app" {
source = "./modules/demo-app"
env_name = "prd"
instance_type = "t2.micro"
ami_id = "ami-026ebd4cfe2c043b2"
instance_name = "batch4-demo-instance"
bucket_name = "batch-4-demo-bucket"
table_name = "batch4-demo-table"
}

Here this code has 3 Environments and we can use different Configurations and settings in each environment. We don't need to write code to launch anything. We will just make the changes here.

Applying the Code to launch Services in our AWS Account:-

  • We have created all the files and folders now we will apply and launch the services that will be created in our AWS Account.

  • At first, run the command:

      terraform init
    

  • Now run the Command:

      terraform plan
    

    This Command will mainly tell us what services we are creating.

Here we can see that 9 Services will be created. It's because we have 3 Environments and these 3 Env has 1 S3 Bucket, 1 EC2 Instance and 1 DynamoDB.

  • At last, we will write the command so that these services will start to get created on our AWS Account.

  • Run the Command:

      terraform apply
    

  • When we Open our AWS Account we will see that 3 EC2 Instances, 3 S3 Buckets and 3 DynamoDB have been created. These are Environment Dev, Prd and Qa.

Thanks.