Search code examples
amazon-web-servicesterraformaws-security-group

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


I'm going through the documentation and they have aws_vpc.main.cidr_block in a resource. I defined the resource which isn't in the documentation but I the following error.

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

Why is it invalid? I want to allow ingress all source IP to be able to reach 443.

File vpc.tf

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

File 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"
  }
}

Solution

  • VPC is your network, it's not a firewall rule like you already defined on aws_security_group resource. If you want to expose the HTTP server to the world, the cidr_blocks in the ingress block will be 0.0.0.0/0 as well.

    cidr_block parameter of aws_vpc defines the range and size of your network, like 10.0.0.0/16, 172.31.0.0/16 and 192.168.0.0/24.

    You can read more about VPCs and subnets on AWS docs.

    You also don't pass the id. This is auto-generated by AWS.

    Example:

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

    Check the terraform docs for aws_vpc that lists all arguments supported.