DevOps Classroom notes 03/Jan/2025

Terraform

Terraform state file

  • On every apply (success) a state file representing resources created is maintained by default in the terraform.tfstate file

Accessing attributes

  • syntax for resource attributes
<type>.<id/name>.<attribute>
  • To play with this we can use terraform console
  • If you use attribute of resource A in resource B, the creation order will be first A then B this is referred as implcit dependency
  • Resource creation order can be controlled implicitly or even explicitly by using depends_on

Activity 1: I want to create a vpc with 4 subnets in AWS (contd)

  • Lets try adding 4 subnets
  • Lets start with one subnet
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.82.2"
    }
  }
}

provider "aws" {
  region = "ap-south-1"
}


resource "aws_vpc" "network" {
  cidr_block = "10.100.0.0/16"
  tags = {
    Name = "from tf"
  }

}

resource "aws_subnet" "web1" {
  vpc_id            = aws_vpc.network.id
  availability_zone = "ap-south-1a"
  cidr_block        = "10.100.0.0/24"
  tags = {
    Name = "web-1"
  }
  depends_on        = [aws_vpc.network]

}
  • Lets add three other subnets
    • web-2:
      • cidr: 10.100.1.0/24
      • az: ap-south-1b
    • db-1:
      • cidr: 10.100.2.0/24
      • az: ap-south-1a
    • db-2:
      • cidr: 10.100.3.0/24
      • az: ap-south-1b
  • Refer Here for the changes
  • Lets understand the work we have done,
  • This template creates a vpc with 4 subnets in mumbai region with fixed ip ranges

Terraform style guide

TFLint

  • This is a linter for terraform which cross checks your terraform templates for
    • best practices
    • stylers
  • For aws related templates add a specific plugin for aws checks Refer Here
  • Refer Here for aws specific configuration for tflint

Versioning constraints

Using terraform recommended file structure

  • Refer Here for specific rule
  • Refer Here for the changes done to re arrange work across files in terraform.
Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a ReplyCancel reply

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

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

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

Continue reading

Exit mobile version
%%footer%%