Search This Blog

Friday 19 April 2019

TERRAFORM :

-> Terraform is a tool which makes Infrastructure as a code.
->Which allows you to create and improve the infrastructure.
->HCL (Hashi Crop Configuration Language).
->Open source/integrate with various clouds
->Executinon plan/change automation.

INSTALLING TERRAFORM:
wget https://releases.hashicorp.com/terraform/0.8.5/terraform_0.8.5_linux_386.zi
https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
unzip terraform_0.8.5_linux_386.zip
mv terraform /usr/local/bin/
                             /usr/local/bin/
                             /usr/bin/ (user environment)
 export PATH=$PATH:/terraform-path/  (~/.bashrc)
terraform   --version

* Terraform init--->Install all required plugins
*Terraform plan--->shows the plan to what infrastructure is going to build
      The terraform plan will let us know what changes, additions and deletions will be done to the 
      infrastructure before actually applying it.
*Terraform apply--->create infrastructure with code
*Terraform Destroy--->it will delete all the infrastructure
* Provider.tf ---> defines which provider is using as IAAS(AWS,AZURE,GOOGLE CLOUD,.........)
*main.tf--->Declare all the resources required /needed for this configuration.
*Module--->Reusable code(Insted of writing same configuration code we can reuse the module or recall )
Modules are collections of .tf files containing resources, input variables, and outputs, which exist outside the root folder of your configuration.
*Variable.tf--->it will holds the varibles to be used in Main.tf file
* .tfstate---> which records or stores the state of information done by terraform.
*Output.tf--->it will hold any output variables.
---------------------------------------------------------------------
main.tf
resource "aws_vpc" "default" {
  cidr_block = "${var.vpc_cidr}"
  enable_dns_hostnames = true
  tags {
    Name = "test-vpc"
  }
}
resource "aws_subnet" "public_subnet" {
  count = 2
  vpc_id = "${aws_vpc.default.id}"
  cidr_block = "${var.public_subnet_cidr[count.index]}"
  #public_subnet_cidr   = ["10.0.1.0/24","10.0.2.0/24","10.0.3.0/24"]
  availability_zone = "${var.availability_zone[count.index]}"
  tags {
    Name = "Web Public Subnet"
  }
}
# Define the private subnet
resource "aws_subnet" "private_subnet" {
  count = 2
  vpc_id = "${aws_vpc.default.id}"
  cidr_block = "${var.private_subnet_cidr[count.index]}"
  #private_subnet_cidr = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
  availability_zone = "${var.availability_zone[count.index]}"
  tags {
    Name = "Database Private Subnet"
  }
}
resource "aws_internet_gateway" "gw" {
  vpc_id = "${aws_vpc.default.id}"

  tags {
    Name = "VPC IGW"
  }
}
resource "aws_eip" "terraform-nat" {
vpc = true
}
resource "aws_nat_gateway" "My_NAT_GW" {
  count = 2
   allocation_id = "${aws_eip.terraform-nat.id}"
   subnet_id = "${aws_subnet.public_subnet.*.id[count.index]}"
   }
resource "aws_route_table" "web-public-rt" {
  vpc_id = "${aws_vpc.default.id}"
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.gw.id}"
  }

  tags {
    Name = "Public Subnet RT"
  }
}
# Assign the route table to the public Subnet
resource "aws_route_table_association" "web-public-rt" {
  subnet_id = "${aws_subnet.public_subnet.*.id[count.index]}"
  route_table_id = "${aws_route_table.web-public-rt.id}"
}

resource "aws_route_table" "db-private-rt" {
  vpc_id = "${aws_vpc.default.id}"
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_nat_gateway.My_NAT_GW.*.id[count.index]}"
  }

  tags {
    Name = "Private Subnet RT"
  }
}
# Assign the route table to the private Subnet
resource "aws_route_table_association" "db-private-rt" {
  subnet_id = "${aws_subnet.private_subnet.*.id[count.index]}"
  route_table_id = "${aws_route_table.db-private-rt.id}"
}
resource "aws_instance" "web" {
  count = 2
   ami  = "${var.ami}"
   instance_type = "t1.micro"
   subnet_id = "${aws_subnet.public_subnet.*.id[count.index]}"
  # vpc_security_group_ids = ["${aws_security_group.sgweb.id}"]
   associate_public_ip_address = true
   #source_dest_check = false
   #user_data = "${file("install.sh")}"
  tags {
    Name = "webserver"
  }
}
resource "aws_instance" "db" {
  count=2
   ami  = "${var.ami}"
   instance_type = "t2.micro"
   subnet_id = "${aws_subnet.private_subnet.*.id[count.index]}"
   #vpc_security_group_ids = ["${aws_security_group.sgdb.id}"]
   #source_dest_check = false
  tags {
    Name = "database"
  }
}

-----------------------------------------------
variable "aws_region" {
  default = "us-east-1"
}
variable "vpc_cidr" {
  #type = "list"
  default = "10.0.0.0/16"
}
variable "public_subnet_cidr" {
  type = "list"
  default = ["10.0.1.0/24" , "10.0.2.0/24"]
}
variable "private_subnet_cidr" {
    type = "list"
    default = ["10.0.4.0/24" , "10.0.5.0/24"]
}
variable "availability_zone" {
  type = "list"
  default = ["us-west-2a" , "us-west-2b"]

}

variable "ami" {
   default = "ami-4fffc834"
}
--------------------------------------
provider "aws" {
  region = "${var.aws_region}"
}
---------------------------------

No comments:

Post a Comment