main.tf
-----------
resource "google_compute_network" "vpc_network" {
project = var.project_name
name = var.vpc_name
auto_create_subnetworks = false
mtu = 1460
}
resource "google_compute_subnetwork" "vpc_subnet" {
for_each = var.subnet_map
name = "${var.vpc_name}-${each.value.name}"
ip_cidr_range = "${each.value.cidr}"
region = var.region
network = google_compute_network.vpc_network.id
dynamic "secondary_ip_range" {
for_each = var.subnet_map
content {
range_name = "${each.value.secondary_address.name}"
ip_cidr_range = "${each.value.secondary_address.cidr}"
}
}
}
variables.tf
----------------
variable "project_name" {
type = string
default = "test"
}
variable "region" {
type = string
default = "us-central1"
}
variable "vpc_name" {
type = string
default = "gcp-vpc"
}
variable "subnet_name" {
type = string
default = "gcp-subnet"
}
variable "subnet_map" {
type = map(object({
name = string,
cidr = string,
sec = map(string)
}))\
// creating 2 different subnets, each having 2 different secondary addresses
default = {
"sub1" = {
name = "subnet01"
cidr = "10.1.0.0/16"
secondary_address = {
"name" = "secondary01"
"cidr" = "10.6.0.0/16"
}
secondary_address = {
"name" = "secondary02",
"cidr" = "10.7.0.0/16"
}
}
"sub2" = {
name = "subnet02"
cidr = "10.2.0.0/16"
secondary_address = {
"name" = "secondary03"
"cidr" = "10.8.0.0/16"
}
secondary_address = {
"name" = "secondary04",
"cidr" = "10.9.0.0/16"
}
}
}
}
1. create one vpc
2. there will be 2 subnets created in the vpc
3. for each subnet there should be 2 secondary alias to be created.
Could any one please help how to achieve this