본문 바로가기

♻ Terraform(테라폼)/Module-EC2

테라폼 모듈을 2개 사용하는 방법

When you declare modules as follows in ./main.tf:

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

module "security" {
  source = "./modules/security"
}

The references to module.networking and module.security are only in scope for local variables, resources, data sources, outputs, and expressions in TF files in the same directory (./*.tf in this case).

Since ./modules/security/security.tf is notin the same directory as ./main.tf, it cannot make reference to module.networking as you are trying here.

A simple fix is to supply vpcid as an input variable to the security module and get its value from module.networking.vpcid:

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

module "security" {
  source = "./modules/security"
  vpcid = module.networking.vpcid
}

For this to work you need to modify ./modules/security/variables.tf to declare vpcid as an input variable:

variable "vpcid" {
  description = "ID of the VPC in which security resources are deployed"
  type = string
}

And change the reference in ./modules/security/security.tf:

resource "aws_security_group" "Web-sg" {
  // ...
  vpc_id = var.vpcid
  // ...
}

https://stackoverflow.com/questions/62707624/error-no-module-call-named-networking-is-declared-in-security-terraform

 

Error No module call named “networking” is declared in security. - Terraform

I am getting an error : Error: Reference to undeclared module on modules\security\security.tf line 6, in resource “aws_security_group” “Web-sg”: 6: vpc_id = module.networking.vpcid No module call n...

stackoverflow.com