我收到一个错误:错误:引用未声明的模块
在 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
}
但我收到了这个错误
我做错了什么?我该如何解决?谢谢马库斯