Terraform - 所需字符串列表(AWS 中的 cidr_blocks)

Terraform - list of string required (cidr_blocks in AWS)

在 Terraform 中,我在输入将存储在变量中的列表时遇到问题。

在执行 terraform plan 时,系统要求我输入 cidr_blocks(应该是字符串列表)。

我尝试输入几个可能代表字符串列表的 "forms",但总是出错。 示例:

第一次尝试:

$terraform plan
...
var.monitoring_access_ips_mysystem
  Enter a value: "10.180.1.0/24", "10.180.2.0/25", "10.180.3.0/23"

第二次尝试:

var.monitoring_access_ips_mysystem
  Enter a value: ["10.180.1.0/24", "10.180.2.0/25", "10.180.3.0/23"]

第三次尝试:

var.monitoring_access_ips_mysystem
  Enter a value: '["10.180.1.0/24", "10.180.2.0/25", "10.180.3.0/23"]'

第四次尝试:

var.monitoring_access_ips_mysystem
  Enter a value: "["10.180.1.0/24", "10.180.2.0/25", "10.180.3.0/23"]"

第 5 次尝试:

var.monitoring_access_ips_mysystem
  Enter a value: "10.180.1.0/24"

对于任何尝试,错误总是相同的:

Error: Incorrect attribute value type

  on ecs/security_group.tf line 10, in resource "aws_security_group" "ecs-cluster-sg":
  10:     cidr_blocks = var.monitoring_access_ips_mysystem

Inappropriate value for attribute "cidr_blocks": list of string required.

ecs/security_group.tf 文件如下所示 ecs/security_group.tf:

resource "aws_security_group" "ecs-cluster-sg" {
  name   = "${var.app_name}-cluster-sg"
  vpc_id = var.vpc_id

  ingress {
    description = "Ingress from monitoring VPC on custom port"
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = var.monitoring_access_ips_mysystem
  }
  ...

我可以 type/pass IP 的什么有效格式才能被接受为 'list of strings'?

UI 输入(在 运行 执行未定义某些变量的 Terraform 命令后出现提示时看到的内容)only supports string values so if you want to pass something that isn't a string then you will need to do so non interactively. This can be one of any of the listed options in the variables documentation。它们是:

  • In a Terraform Cloud workspace.
  • Individually, with the -var command line option.
  • In variable definitions (.tfvars) files, either specified on the command line or automatically loaded.
  • As environment variables.

在您的情况下,您可以 运行 使用以下命令制定计划:

terraform plan -var='monitoring_access_ips_mysystem=["10.180.1.0/24", "10.180.2.0/25", "10.180.3.0/23"]'

除非这可能会在每个 运行 Terraform 上发生变化,否则通常应该将其放入 terraform.tfvars 文件中,如下所示:

monitoring_access_ips_mysystem = [
  "10.180.1.0/24",
  "10.180.2.0/25",
  "10.180.3.0/23",
]