Wednesday, 23 September 2020

Terraform Part-2

 In this section lets discuss how we can build reusable modules.

Here in this example lets create Ec2 and VPC, subnets dynamically by using separate modules.

Folder structure will be like below


First lets create VPC and subnets.

for this, create below files

network.tf: for vpc ,subnets

resource "aws_vpc" "main" {

  cidr_block       = var.vpc_cidr

  instance_tenancy = var.tenancy

    tags = {

    Name = "main"

  }

}


resource "aws_subnet" "main" {

  vpc_id     = var.vpc_id

  cidr_block = var.subnet_cidr

  

  tags = {

    Name = "main"

  }

}


output "vpc_id"{

    value=aws_vpc.main.id

   }

output "subnet_id"{

    value=aws_subnet.main.id

   }

vars.tf: To initialize the variables , this will be used in vpc and subnets creation.

vars.tf:

variable "vpc_cidr" {

    default="10.0.0.0/16"

}

varible "tenancy" {

    default=dedicated

}

variable "vpc_id"{

 

}

variable "subnet_cidr"{

    default="10.0.1.0/24" ---> if the value is mandatory initialize the same else assign empty.

}

Now lets create ec2 instance by following the same.

create a file : instances.tf in ec2 folder, in this case get the subnet value where you want to create instance.

instances.tf:

resource "aws_instance" "web" {

  count=var.ec2_count

  ami           = var.ec2_ami

  instance_type = "var.instatype

  subnet_id=var.subnet_id

  tags = {

    Name = "myinstance"

  }

}

vars.tf

variable "ec2_ami"{  }

variable "instatype"{

    default="t2.micro"

  }

variable "subnet_id"{  }

variable "ec2_count"{

   default="1"

  }

Lets use this to create resources in dev and prod environment.
Create two instances  

create a main.tf file in dev folder and source is where you are getting the values for CIDR or vpc or subnets.

In case of source , you can get value from anywhere like git/bitbucket or etc with relative path.

We need to get the vpc_id which is dynamically creating in runtime. To get this value from modules/vpc , we need to declare this in output (refer network.tf) file.

Dev:

main.tf:

provider "aws" {

    region = "ap-south-1"

}

module "my_vpc"{

    source="../modules/vpc"

    vpc_cidr="192.168.0.0/16"

    tenancy="default"

    vpc_id="${module.my_vpc.vpc_id}"

    subnet_cidr="192.168.1.0/24"

}

module "my_ec2"{

    source="../modules/ec2"

    ec2_count=1

    ec2_ami="ami-09052aa9bc337c78d"

    instatype="t2.micro" 

    subnet_id="${module.my_vpc.subnet_id}"

}

Now apply terraform and check the output.

VPC:

Subnets:

EC2:




Same main.tf
file we can use it for dev too and update required vpc and subnets , EC2 values and try.

main.tf:

provider "aws" {

    region = "ap-south-1"

}

module "my_vpc"{

    source="../modules/vpc"

    vpc_cidr="10.0.0.0/16"

    tenancy="default"

    vpc_id="${module.my_vpc.vpc_id}"

    subnet_cidr="10.0.1.0/24"

}

module "my_ec2"{

    source="../modules/ec2"

    ec2_count=1

    ec2_ami="ami-09a7bbd08886aafdf"

    instatype="t2.micro" 

    subnet_id="${module.my_vpc.subnet_id}"

}

This is how we can create modules and provision the AWS resources using Terraform.

Thank you for reading 

No comments:

Post a Comment