【问题标题】:Getting Terraform error: "Error: expected cidr_block to contain a valid Value, got: with err: invalid CIDR address:"获取 Terraform 错误:“错误:预期 cidr_block 包含有效值,得到:错误:无效 CIDR 地址:”
【发布时间】:2021-11-17 07:35:14
【问题描述】:

尝试制作一个简单的 vpc/安全组模块集。 VPC 资源在这里:

resource "aws_vpc" "main" {
  cidr_block = var.cidr
}

安全组资源在这里:

resource "aws_security_group" "default" {
  name        = var.sg-name
  vpc_id      = aws_vpc.main.id
  description = var.sg-description
}

resource "aws_security_group_rule" "ingress" {
  count = length(var.allowed_ip)

  type = "ingress"
  from_port = element(var.allowed_ports, count.index)
  to_port = element(var.allowed_ports, count.index)
  protocol = var.protocol
  cidr_blocks = var.allowed_ip
  security_group_id = aws_security_group.default.id
}

resource "aws_security_group_rule" "egress" {
  type              = "egress"
  from_port         = 0
  to_port           = 65535
  protocol          = "-1"
  cidr_blocks       = ["0.0.0.0/0"]
  security_group_id = aws_security_group.default.id
}

模块在这里:

module "vpc" {
  source = "../"

  cidr = "10.0.0.0/16"
}

module "security_group" {
  source = "../"

  sg-name = "Test"
  sg-description = "Test Description"

  protocol = "tcp"
  allowed_ip = ["10.10.0.0/20", "10.20.0.0/20"]
  allowed_ports = [22, 443]
}

当我运行 Terraform Plan 时,我收到此错误:

错误:预期 cidr_block 包含有效值,得到:错误: 无效的 CIDR 地址:

在 ..\vpc.tf 第 2 行,在资源“aws_vpc”“main”中:2:
cidr_block = var.cidr

我做了很多研究,但无法完全确定问题所在。有人有什么想法吗?

【问题讨论】:

  • 我认为您的目录结构有些奇怪。您源上的那些目录路径是否正确?

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

您正在尝试使用变量但没有 variables.tf,您在资源上指定 cidr 块,因此,它应该如下所示:

resource "aws_vpc" "main" {
  cidr_block = module.vpc.cidr
}

否则,您可以创建一个 variables.tf,其中包含类似的内容(通常是这样做的,但我们也倾向于通过扩展名为“.tfvars”的文件传递这些值):

variable "cidr" {
   cidr = "10.0.0.0/16"
}

一旦定义了变量,就可以使用“var.variable”格式。

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多