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

Terraform - expected cidr_block to contain a valid Value, got: 0.0.0.0 with err: invalid CIDR address: 0.0.0.0

我正在查看 documentation,他们在资源中有 aws_vpc.main.cidr_block。我定义了文档中没有的资源,但出现以下错误。

Terraform - expected cidr_block to contain a valid Value, got: 0.0.0.0 with err: invalid CIDR address: 0.0.0.0

为什么无效?我想让入口所有源IP都能达到443。

文件vpc.tf

resource "aws_vpc" "main" {
    id = "vpc-0da86af9876e72d66c"
    cidr_block = "0.0.0.0/0"
}

文件test.tf

resource "aws_security_group" "allow_tls" {
  name        = "allow_tls"
  description = "Allow TLS inbound traffic"
  vpc_id      = aws_vpc.main.id

  ingress {
    description = "TLS from VPC"
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = [aws_vpc.main.cidr_block]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "allow_tls"
  }
}

VPC 是您的网络,它不是您已经在 aws_security_group 资源上定义的防火墙规则。如果你想向全世界公开 HTTP 服务器,ingress 块中的 cidr_blocks 也将是 0.0.0.0/0

aws_vpc

cidr_block 参数定义网络的范围和大小,如 10.0.0.0/16172.31.0.0/16192.168.0.0/24.

您可以在 AWS docs 上阅读有关 VPC 和子网的更多信息。

你也没有通过id。这是由 AWS 自动生成的。

示例:

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

检查列出所有支持的参数的 terraform docs for aws_vpc