【问题标题】:Terraform tuple to map conversionTerraform 元组到地图的转换
【发布时间】:2020-05-17 12:27:02
【问题描述】:

我是 Terraform 的新手,我正在使用此功能重复使用相同的子网列表来启动比我拥有的子网更多的实例。 (它只会循环)如果我提供自己的子网图,但远程状态的数据是一个元组并且我收到此错误:

Invalid value for "inputMap" parameter: lookup() requires a map as the first argument. data.terraform_remote_state.vpc.outputs.private_subnets is tuple with 4 elements

我也尝试过 tomap( 函数,但失败了:

Invalid value for "v" parameter: cannot convert tuple to map of any single type.

这是我的代码:

  count                  = var.instance_count
  ami                    = var.ami
  instance_type          = "t2.medium"
  subnet_id              = lookup(data.terraform_remote_state.vpc.outputs.private_subnets, count.index%length(data.terraform_remote_state.vpc.outputs.private_subnets))
  vpc_security_group_ids = ["${data.terraform_remote_state.foo_sg.outputs.foo_sg_id}"]
  key_name               = var.key_name
  iam_instance_profile   = var.iam_instance_profile

  user_data = <<-EOF
              #!/bin/bash
              hostnamectl set-hostname --static "${var.app_name}-${count.index + 1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name}"
              echo "127.0.0.1 localhost.localdomain localhost4 localhost4.localdomain4 ${var.app_name}-${count.index + 1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name} localhost" > hosts
              echo "::1 localhost localhost.localdomain localhost6 localhost6.localdomain6" >> hosts
              EOF

  tags = {
    Name                 = "${var.app_name}-${count.index +1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name}"
}

就像我说的那样,我的目标是在我拥有的那个远程状态下重新使用 4 个子网,所以如果我想要 6 个实例,它将循环通过我拥有的 4 个实例,而第 5 和第 6 个实例将位于子网 1 和 2在元组中。任何建议将不胜感激!

【问题讨论】:

  • 如果数据存储在您的远程状态中,那么您可以控制其类型,并且可以根据需要预先定义它。

标签: terraform terraform-provider-aws


【解决方案1】:

你有 3 种形式,用来制作循环。

1º 使用dynamic block... 示例:

resource "aws_security_group" "ecc_default" {
  count       = length(var.clusters)
  vpc_id      = var.vpc_id
  name        = "${terraform.workspace}.ecc-${var.clusters[count.index].engine}.sg"
  description = "${terraform.workspace}.ecc-${var.clusters[count.index].engine}.sg"

  ingress {
    from_port   = var.clusters[count.index].port
    to_port     = var.clusters[count.index].port
    protocol    = "tcp"
    cidr_blocks = [var.vpc_cidr]
  }

  dynamic "ingress" {
    iterator = item
    for_each = var.enable_vpn ? var.vpn_cidr : []

    content {
      from_port   = var.clusters[count.index].port
      to_port     = var.clusters[count.index].port
      protocol    = "tcp"
      cidr_blocks = [item.value["cidr"]]
      description = item.value["description"]
    }
  }
...
...
}

2º 使用for_each... (click here for answer)

3º 使用算术迭代... (click here for answer) 示例:

resource "aws_security_group_rule" "ecc_internal" {
    count = length(var.ingress_security_groups) > 0 ? length(var.ingress_security_groups) * length(var.clusters) : 0
    security_group_id = aws_security_group.ecc_default[count.index % length(var.clusters)].id
    type = "ingress"
    from_port = var.clusters[count.index % length(var.clusters)].port
    to_port = var.clusters[count.index % length(var.clusters)].port
    protocol = "tcp"
    source_security_group_id = var.ingress_security_groups[floor(count.index / length(var.clusters))]
}

并且,对于转换错误: 尝试使用 coalescelist 进行假替换。像这样:

resource "aws_lb_listener_certificate" "default" {
  count           = (coalescelist(data.aws_alb_listener.https, [{ certificate_arn = "" }])[0].certificate_arn != var.certificate_arn) ? local.enabled : 0
  listener_arn    = coalescelist(data.aws_alb_listener.https, [{ arn = "" }])[count.index].arn
  certificate_arn = var.certificate_arn
}

元组 ([N1, N2, N3]) 到映射 ({N1 = "", N2 = ""}) 的转换确实是不允许的。但是,您可以使用伪造的替代品来完成这项工作。 tomap 需要一个对象作为参数。而map,需要多个参数来生成结果。尝试操作您的列表,在 map 中转换,使用另一种类似的形式(如果您只想转换,而不是解决问题):

environment_vars = jsonencode([
      for key in sort(keys(data.external.environment_vars.result)) : {
        name  = key
        value = lookup(data.external.environment_vars.result, key)
    }])

我为你推荐:Terraform tips & tricks: loops, if-statements, and gotchas

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 2019-06-09
    • 1970-01-01
    • 2021-07-20
    • 2020-11-17
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多