DevOps Classroom Series – 09/Jan/2020

Terraform file organization

  • Since writing every thing in one file looks complex and unreadable, terraform allows you to write resources, providers etc in multiple .tf files
  • Lets organize the work into multiple files
touch provider.tf
touch network.tf
  • In Provider.tf add the following
provider "aws" {
    access_key = "<ACCESS-KEY>"
    secret_key = "<SECRET-KEY>"
    region = "us-west-2"
}
  • In network.tf add the following
resource "aws_vpc" "myvpc" {
    cidr_block = "192.168.0.0/16"

    tags = {
        "Name" = "from-tf"
    }
}

resource "aws_subnet" "subnet1" {
    cidr_block = "192.168.0.0/24"

    vpc_id = "${aws_vpc.myvpc.id}"

    availability_zone = "us-west-2a"

    tags = {
        "Name" = "subnet-1"
    }
  
}

resource "aws_subnet" "subnet2" {
    cidr_block = "192.168.1.0/24"
    availability_zone = "us-west-2b"
    tags = {
        "Name" = "subnet2"
    }
    vpc_id = "${aws_vpc.myvpc.id}"
  
}

resource "aws_subnet" "subnet3" {
  cidr_block = "192.168.2.0/24"
  availability_zone = "us-west-2c"
  tags = {
      "Name" = "subnet3"
  }
  vpc_id = "${aws_vpc.myvpc.id}"
}
  • Now execute terraform apply .

Adding some stuff to existing network

  • Add internet gateway to vpc and a public route table. Create a new tf file and add the following content to existing folder
resource "aws_internet_gateway" "igw" {
    vpc_id = "${aws_vpc.myvpc.id}"

    tags = {
        "Name" = "from tf"
    }
  
}

resource "aws_route_table" "publicrt" {
    vpc_id = "${aws_vpc.myvpc.id}"

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

    tags = {
        "Name" = "public rt-tf"
    }
}


  • Lets create a plan by executing
terraform plan --out="aws.plan" .
  • Now execute the plan by using terraform apply aws.plan

Variable support to Terraform

  • Create a new file input.tf and add variables as described over here
variable "accesskey" {
    type = "string"
}

variable "secretkey" {
   type = "string"
}

variable "region" {
    type = "string"
    default = "us-west-2"
}
    
  • Now lets use these varaibles in the provider section
provider "aws" {
    access_key = var.accesskey
    secret_key = var.secretkey
    region = var.region
}
  • Now Create a plan and extecute using
terraform plan -var 'accesskey=<your-access-key>' -var 'secretkey=<your-secret-key>' -out='aws.plan' .
terraform apply aws.plan
  • Add variables for vpc cidr, subnet cidr

variable "subnet1cidr" {
    type = "string"
    default = "192.168.0.0/24"
}


variable "subnet2cidr" {
    type = "string"
    default = "192.168.1.0/24"
}


variable "subnet3cidr" {
    type = "string"
    default = "192.168.2.0/24"
}

  • Resources will be looking like
resource "aws_subnet" "subnet1" {
    cidr_block = var.subnet1cidr

    vpc_id = "${aws_vpc.myvpc.id}"

    availability_zone = "us-west-2a"

    tags = {
        "Name" = "subnet-1"
    }
  
}

resource "aws_subnet" "subnet2" {
    cidr_block = var.subnet2cidr
    availability_zone = "us-west-2b"
    tags = {
        "Name" = "subnet2"
    }
    vpc_id = "${aws_vpc.myvpc.id}"
  
}

resource "aws_subnet" "subnet3" {
  cidr_block = var.subnet3cidr
  availability_zone = "us-west-2a"
  tags = {
      "Name" = "subnet3"
  }
  vpc_id = "${aws_vpc.myvpc.id}"
}

Leave a Reply

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

About learningthoughtsadmin