【问题标题】:Iterate over key/value on map variable迭代地图变量上的键/值
【发布时间】:2019-06-26 14:32:42
【问题描述】:

在 Terraform 中,我正在尝试构建一个带有 IP 映射和相关评论的 SecurityGroup。 我正在尝试做的是遍历允许网络的映射键值,并关联描述字段的映射值。

代码看起来像这样,

resource "aws_security_group_rule" "ingress" {
  type              = "ingress"
  (...)
  cidr_blocks       = "${var.ingress_cidr_blocks}"
  description       = "${var.ingress_description}"
  security_group_id = "${aws_security_group.this.id}"
}

module "securitygroup-ssh" {
  source = ""
  (...)
  ingress_from_port = "22"
  ingress_cidr_blocks = ["${var.ipLlist}"]
  ingress_description = "${var.allowed-network}"
}

以此为变量,

variable "allowed-network" {
    type = "map"
    default = {
        "From Customer1" = "1.1.1.1/32"
        "Network this" = "10.0.0.0/24"
    }
}

已经在使用 map 和 lookup 内置函数而没有满意的结果。也能够以列表的形式遍历网络,但描述字段似乎被最后一个值覆盖。

有什么想法吗?目前在 Terraform 中这是否可能?

【问题讨论】:

    标签: terraform


    【解决方案1】:

    不完全是一张地图,但它应该做你想做的事:

    provider "aws" {
        region = "ca-central-1"
        version = "~> 2.7"
    }
    
    resource "aws_security_group" "this" {
      name_prefix = "this"
    }
    
    resource "aws_security_group_rule" "allowed-network" {
      count = length(var.allowed-network)
      type            = "ingress"
      from_port       = 0
      to_port         = 65535
      protocol        = "tcp"
      description = split(",", var.allowed-network[count.index])[0]
      cidr_blocks = [split(",", var.allowed-network[count.index])[1]]
      security_group_id = aws_security_group.this.id
    
    }
    
    variable "allowed-network" {
        type = "list"
        default = [
            "From Customer1,1.1.1.1/32",
            "Network this,10.0.0.0/24"
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2016-05-31
      • 2010-09-20
      相关资源
      最近更新 更多