1
votes

I am trying to build an aws vpc and security groups in the same module. I have this structure in my project:

.
├── README.md
├── commit.sh
├── main.tf
├── modules
│   └── networking
│       ├── README.md
│       ├── main.tf
│       ├── outputs.tf
│       └── variables.tf
├── plans
├── terraform.sh
├── variables.tf
├── vars
└── versions.tf

I am calling the module with a very simple main.tf in my project root:

## Networking
module "networking" {
  source = "./modules/networking/"
}

I am needing the VPC ID for the Security Groups, and the SGs are defined as different modules inside the same file as the VPC module:

module "web_ingress_sg" {
  source = "terraform-aws-modules/security-group/aws//modules/http-80"

  name        = "wordpress-ingress"
  description = "Security group for web-server with HTTP ports open within VPC"
  vpc_id      = module.vpc.vpc_id

  ingress_cidr_blocks = ["50.82.222.12/32"]
  computed_egress_with_source_security_group_id = [
    {
      rule                     = "http-80-tcpp"
      source_security_group_id = module.wordpress_instance_sg.security_group_id
    },
  ]
}

The VPC ID is defined in the outputs.tf file in the module, but I keep getting the error msg "The argument "vpc_id" is required, but no definition was found."

1
This is difficult to answer with your example being incomplete. Could you try to edit your question to provide a minimal reproducible example please? Seeing how you've structured your modules would make it much easier to answer. - ydaetskcoR

1 Answers

1
votes

A module can't reference its own outputs. You have to get the vpc id directly. Assuming you have the vpc defined in this networking module, then the following should be enough:

vpc_id      = aws_vpc.vpc.vpc_id