Terraform Classroom Series – 19/Sep/2019

What happends when you execute terraform apply

Terraform apply command does the following * Every terraform apply command creates a plan if you dont pass the pass. Plan can be created using terraform plan * Before terraform creates anything it will check state file. If state file is not present. It will create every thing. * State will be maintained by terraform by making necessary calls to the provider

What terraform init does?

Terraform init downloads the provider executable into local directory ".terraform" in the working directory of terraform

Commands used in the sesssion

terraform plan -out "./myplan" .
terraform apply  "./myplan"
# input the plan

Sample script without provider in Paris Region of AWS

resource "aws_vpc" "my_network" {
    cidr_block              = "10.10.0.0/16"
    enable_dns_hostnames    = true
    tags = {
        Name = "openmrs"
    }
}

resource "aws_subnet" "subnet_1" {
    cidr_block              = "10.10.0.0/24"
    vpc_id                  = "${aws_vpc.my_network.id}"
    availability_zone       = "eu-west-3a"
    tags = {
        Name = "openmrs"
    }
  
}

resource "aws_subnet" "subnet_2" {
    cidr_block              = "10.10.1.0/24"
    vpc_id                  = "${aws_vpc.my_network.id}"
    availability_zone       = "eu-west-3b"
    tags = {
        Name = "openmrs"
    }
  
}

resource "aws_internet_gateway" "my_igw" {
    vpc_id      = "${aws_vpc.my_network.id}"
    tags = {
        Name = "openmrs"
    }
  
}

resource "aws_route_table" "my_rt" {
    vpc_id = "${aws_vpc.my_network.id}"

    route {
        cidr_block  = "0.0.0.0/0"
        gateway_id  = "${aws_internet_gateway.my_igw.id}"
    }

    tags = {
        Name = "openmrs"
    }
  
}

resource "aws_security_group" "my_sg" {
    name            = "my_sg"
    description     = "created from terraform"
    vpc_id          = "${aws_vpc.my_network.id}"
    ingress{
        cidr_blocks = ["0.0.0.0/0"]
        protocol    = "-1"
        from_port   = "0"
        to_port     = "0"
    }
    egress{
        cidr_blocks = ["0.0.0.0/0"]
        protocol    = "-1"
        from_port   = "0"
        to_port     = "0"
    }
    tags = {
        Name = "openmrs"
    }
}


resource "aws_instance" "web1" {
    ami                         = "ami-0ad37dbbe571ce2a1"
    instance_type               = "t2.micro"
    subnet_id                   = "${aws_subnet.subnet_1.id}"
    associate_public_ip_address = true
    vpc_security_group_ids      = [ "${aws_security_group.my_sg.id}" ]
    tags = {
        Name = "openmrs"
    }
  
}


resource "aws_instance" "web2" {
    ami                         = "ami-0ad37dbbe571ce2a1"
    instance_type               = "t2.micro"
    subnet_id                   = "${aws_subnet.subnet_2.id}"
    associate_public_ip_address = true
    vpc_security_group_ids      = [ "${aws_security_group.my_sg.id}" ]
    tags = {
        Name = "openmrs"
    }
  
}

note: this script works only for paris. If you want to make it work for other regions
change ami
dont forget to add provider

Areas to be improved

  1. Making Terraform script generic
  2. Need to do provisioning

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner