Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - Create type constraints for type Map

What would be the correct way to create type constraints for type map?

This doesn't seem valid.

variable "vpc_subnets" {
  type = map(
    key = {name  = string, cidr_block = string, map_public_ip_on_launch = bool, availability_zone = string}
  )
}

Here is what the map looks like..

  vpc_subnets = {
    "public_subnet_a" = {name = "public_test_a", cidr_block = "10.0.0.0/28", map_public_ip_on_launch = true,  availability_zone = "ap-south-1a"},
    "public_subnet_b" = {name = "public_test_b", cidr_block = "10.0.0.16/28", map_public_ip_on_launch = true,  availability_zone = "ap-south-1b"},
    "private_subnet_a" = {name = "private_test_a", cidr_block = "10.0.0.32/28", map_public_ip_on_launch = false,  availability_zone = "ap-south-1a"},
    "private_subnet_b" ={name = "private_test_b", cidr_block = "10.0.0.48/28", map_public_ip_on_launch = false,  availability_zone = "ap-south-1b"}
  }
like image 246
user630702 Avatar asked Oct 19 '25 04:10

user630702


1 Answers

Your vpc_subnets is map of objects, so you could use:

variable "vpc_subnets" {
  type = map(
    object({name  = string, cidr_block = string, map_public_ip_on_launch = bool, availability_zone = string})
  )
like image 82
Marcin Avatar answered Oct 22 '25 07:10

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!