【问题标题】:Terraform - expected cidr_block to contain a valid Value, got: 0.0.0.0 with err: invalid CIDR address: 0.0.0.0Terraform - 预期 cidr_block 包含有效值,得到:0.0.0.0 错误:无效 CIDR 地址:0.0.0.0
【发布时间】:2020-11-12 03:26:59
【问题描述】:

我正在浏览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"
  }
}

【问题讨论】:

    标签: amazon-web-services terraform aws-security-group


    【解决方案1】:

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

    aws_vpccidr_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

    【讨论】:

    • 如果我必须使用现有的 vpc 怎么办?
    • 您可以只在安全组的vpc_id 参数中传递现有 VPC 的 ID,如果您没有在 terraform 中定义,您可以硬编码或作为 terraform 参数传递。如果您的意思是您已经有一个 VPC 并想开始在 terraform 中进行管理,您可以通过 terraform import 导入
    猜你喜欢
    • 2021-11-17
    • 2021-11-17
    • 2011-04-09
    • 2017-08-05
    • 2020-05-08
    • 2019-10-05
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多