【问题标题】:Mapping Port Ranges of AWS Security Groups in Terraform在 Terraform 中映射 AWS 安全组的端口范围
【发布时间】:2020-08-20 12:42:21
【问题描述】:

我正在尝试迭代端口和端口范围的映射以在 Terraform 中创建 AWS 安全组。对于 Ingress 端口,它们提供了一个 from_port 字段和一个 to_port 字段。这应该定义安全组中特定规则的端口范围。当规则是单个端口时,我可以很好地进行映射,因为从端口和到端口都是相同的。但是当规则需要一系列端口时,我无法弄清楚如何做到这一点。这是我一直在测试的代码。我认为我对如何执行此操作有正确的想法,但遇到了类型错误。

我实际上是在尝试使用 from_port 的索引值来查找应该映射到 to_port 的端口。

provider "aws" {
  region  = "us-east-2"
  profile = "default"
}

locals {
  start = [22, 33, 44] # from_ports
  end   = [25, 35, 45] # to_ports
  vpcid = "vpc-xxxxxxxxxxxx"
}

resource "aws_security_group" "traffic_secgrp" {
  name        = "traffic_sec_grp"
  description = "Allows traffic"
  vpc_id      = local.vpcid

  dynamic "ingress" {
    for_each = local.start
    content {
      from_port   = ingress.value
      to_port     = element(local.end, [index(local.start, ingress.value)])
      protocol    = "UDP"
      cidr_blocks = ["10.2.0.0/20"]
    }
  }
}

这是我从to_port 行得到的错误:

Error: Invalid function argument

  on main.tf line 21, in resource "aws_security_group" "traffic_secgrp":
  21:       to_port     = element(local.end, [index(local.start, ingress.value)])
    |----------------
    | ingress.value is 44
    | local.start is tuple with 3 elements

Invalid value for "index" parameter: number required.

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:

    我发现了上面代码的问题。在to_port 行中,我将element 函数的索引包裹在导致类型问题的括号中。一旦我删除了这些括号,它就可以正常工作了。

    工作to_port线

    to_port     = element(local.end, index(local.start, ingress.value))
    

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 2021-04-06
      • 1970-01-01
      • 2021-03-06
      相关资源
      最近更新 更多