0

我收到一个错误:错误:引用未声明的模块

在 modules\security\security.tf 第 6 行,在资源“aws_security_group”“Web-sg”中:6:vpc_id = module.networking.vpcid

在安全性中没有声明名为“networking”的模块调用。

This is directory structure that I am following currently,
│ main.tf
│ provider.tf
│ terraform.tfstate
│ terraform.tfstate.backup
│ variables.tf
│
└───modules
├───networking
│ networking.tf
│ outputs.tf
│ variables.tf
│
└───security
security.tf
variables.tf

我的 main.tf

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

}

我创建了一个名为networking的模块,在其中创建了所有网络资源:

    resource "aws_vpc" "vpc" {
    cidr_block = var.cidr_block
    
    tags = {
        Name = var.vpc_name
    }
    }
#Creating Public Subnets
  resource "aws_subnet" "public" {
  count = var.subnet_count
  cidr_block = element(var.subnet_cidr_public,count.index)
  availability_zone = element(var.azs,count.index)
  vpc_id = aws_vpc.vpc.id
  map_public_ip_on_launch = var.map_public_ip_on_launch

  tags =  {
      Name = "Subnet-Public-${element(var.subnet_cidr_public,count.index)}"
  }    
  }


# Creating and Associating to the VPC the Internet Gateway

resource "aws_internet_gateway" "IGW-VPC" {
   vpc_id = aws_vpc.vpc.id 

   tags = {
     Name = var.igw-name
   }

}

# Creating Route Table - Public
# Creating Public Route Table 

resource "aws_route_table" "public-route" { 
  vpc_id = aws_vpc.vpc.id
  
  route { 
    cidr_block = var.cidr_block_route 
    gateway_id = aws_internet_gateway.IGW-VPC.id 
  }

    tags = {
      Name = var.public_route_name
    }
    
}

# Associating Subnet Public-1a to the Public Route Table
resource "aws_route_table_association" "association-public1a" {
  subnet_id = var.subnet_public_1a
  route_table_id = aws_route_table.public-route.id
}

resource "aws_route_table_association" "association-public1b" {
  subnet_id = var.subnet_public_1b
  route_table_id = aws_route_table.public-route.id
}

我创建了一个名为 security 的模块,在其中创建了所有安全组:

我做错了什么?我该如何解决?

resource "aws_security_group" "Web-sg" {
  name = var.web-sg_name
  description = var.web-sg_description
  vpc_id = module.networking.vpcid
  ingress { 
    description = var.description22
    from_port = var.port22
    to_port  = var.port22
    protocol = var.protocol 
    cidr_blocks = var.cidr000
  }
  ingress { 
    description =  var.description80 
    from_port = var.port80
    to_port  = var.port80
    protocol = var.protocol 
    cidr_blocks = var.cidr000 
  }
  ingress { 
    description = var.description443
    from_port = var.port443
    to_port  = var.port443
    protocol = var.protocol 
    cidr_blocks = var.cidr000 
  }
  egress {
    from_port   = var.port0 
    to_port     = var.port0 
    protocol    = var.protocol0
    cidr_blocks = var.cidr000 
  }
  tags = {
    "Name" = var.web-sg_name
  }
}

如您所见,我在资源“aws_security_group”“Web-sg”{ vpc_id = module.networking.vpcid

和一个 output.tf 文件

output "vpcid" {
    value = aws_vpc.vpc.id
}

但我收到了这个错误

我做错了什么?我该如何解决?谢谢马库斯

4

1 回答 1

1

当您在 ./main.tf 中如下声明模块时:

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

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

对module.networkingmodule.security的引用仅在同一目录中的 TF 文件(本例中为 ./*.tf)中的局部变量、资源、数据源、输出和表达式的范围内。

由于 ./modules/security/security.tf 与 ./main.tf 不在同一目录中,因此它不能像您在此处尝试的那样引用 module.networking。

一个简单的解决方法是将vpcid作为输入变量提供给安全模块,并从module.networking.vpcid获取其值:

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

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

为此,您需要修改 ./modules/security/variables.tf 以将 vpcid 声明输入变量:

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

并更改 ./modules/security/security.tf 中的引用:

resource "aws_security_group" "Web-sg" {
  // ...
  vpc_id = var.vpcid
  // ...
}
于 2020-07-03T22:31:34.877 回答