DevOps Classroom Series – 08/Jan/2020

Resource Dependencies

  • When Resource A requires resource B to be present (or already existing) this is called as dependency
  • In Terraform terms you have to create resource B before resource A
  • To Demonstrate this lets add subnets to VPC. To create subnet resource vpc id is required (vpc has to be existing)
  • To create resource dependencies use the following expression
"${<resource-type>.<resource-name>.<attribute-name>}"

"${aws_vpc.myvpc.id}"

  • If we apply the above expressions to create two subnets in the vpc
provider "aws" {
    access_key = "<accesskey>"
    secret_key = "<secret-key>"
    region = "us-west-2"
}

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}"
  
}


  • Now Execute terraform apply to create infra and also observe the folder where terraform.tfstate is created where the information about created resources are stored.
  • Now Lets add one more subnet to existing template
provider "aws" {
    access_key = "<accesskey>"
    secret_key = "<secret-key>"
    region = "us-west-2"
}

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 if you execute terraform apply command

    • Terraform will get the current status from provider (aws)
    • It compares the status of provider with tfstate file, if any differences are found they will be added for execution. (Deleting from aws and executing terraform apply)
    • during apply plan will be created and compared to current status, if plan is not matching the current state, then the changes will be added to terraforms execution.
  • Experiment Terraform

    • By deleting one subnet from template
    • change the tag value for one subnet
    • change the avaliability zone for one subnet

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

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

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube