DevOps Classroom notes 13/Aug/2026

Terraform count:

  • count is meta arugment to create multiple resources with single block

basic count


## variables

variable "vpc_name" {
    type = string
    default = "mynetwork"
    description = "name of the vpc"
    sensitive = true
}

variable "subnet_cidrs" {
    type = list(string)
    default = [ "10.2.1.0/24", "10.2.2.0/24", "10.2.3.0/24"]
}


variable "az-range" {
    type = list(string)
    default = [ "us-east-1a", "us-east-1b", "us-east-1c"]
}



################################ resources #########################
resource "aws_vpc" "qt-demo-vpc" {
  cidr_block = "10.2.0.0/16"

  tags = {
    Name = var.vpc_name
    env  = "dev"
  }
}

resource "aws_subnet" "mysubnets" {
  count = length(var.subnet_cidrs)
  vpc_id     = aws_vpc.qt-demo-vpc.id
  cidr_block = var.subnet_cidrs[count.index]
  availability_zone = var.az-range[count.index]
  tags = {
    Name = "app1"
  }
}


count with if conditions

Ex: create_instance is true create 1 ec2 instance

Task

  1. create vpc & 4 public subnets and 4 pravite subnets
    1. myvpc – name vpc 10.5.0.0/16
    2. public subnets – pub-sub1, pub-sub2, pub-sub3, pub-sub4
    3. pravite subnets – pvsubnet1, pvsubnet2, pvsubnet3, pvsubnet4
    4. create 2 route table pubroute & pvroute
    5. IGW
    6. NAT
    7. create route in pub route table attach IGW
    8. create route in pv route table attach NAT
    9. ass subnets pv and pub route tables

use count and create resources

Leave a Reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

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