This blog post demonstrates a sample Terraform configuration for AWS, including VPC and subnet resources with dynamic CIDR blocks and availability zones to determines cout and count.index.
Variables
variable "cidr_block" {
type = string
default = "10.0.0.0/16"
}
variable "common_tags" {
type = map(any)
default = {
Terraform = "True"
Environment = "Dev"
}
}
variable "subnet_cidr" {
type = list(string)
default = ["10.0.1.0/24", "10.0.11.0/24"]
}
variable "az" {
type = list
default = ["ap-south-1a","ap-south-1b"]
}
variable "subnet_names" {
type = list(string)
default = [ "GS Pub Subnet", "Gs Pvt Subnet" ]
}
AWS VPC Resource
resource "aws_vpc" "gs-vpc" {
cidr_block = var.cidr_block
instance_tenancy = "default"
tags = merge(
{
Name = "GS VPC"
},
var.common_tags
)
}
AWS Subnet Resources
resource "aws_subnet" "gs-pub-1a" {
vpc_id = aws_vpc.gs-vpc.id
count = length(var.subnet_cidr)
cidr_block = var.subnet_cidr[count.index]
availability_zone = var.az[count.index]
tags = merge(
{
Name = var.subnet_names[count.index]
},
var.common_tags
)
}
The provided Terraform configuration sets up a VPC and subnets in multiple availability zones. The subnets' CIDR blocks and names are dynamically determined using lists, which are a common practice for managing multiple environments or regions.

Comments
Post a Comment