本文介绍了Terraform - 为类型 Map 创建类型约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为类型映射创建类型约束的正确方法是什么?

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

这似乎无效.

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

这是地图的样子..

  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"}
  }

推荐答案

你的 vpc_subnets 是对象的映射,所以你可以使用:

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})
  )

这篇关于Terraform - 为类型 Map 创建类型约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:55