Terraform 错误:没有名为“;的模块调用;联网”;已在安全声明中声明。-地形
我得到一个错误: 错误:引用未声明的模块 在modules\security\security.tf第6行的资源“aws\U安全组”“Web sg”中: 6:vpc_id=module.networking.vpcid 安全性中未声明名为“networking”的模块调用Terraform 错误:没有名为“;的模块调用;联网”;已在安全声明中声明。-地形,terraform,terraform-modules,terraform-aws-modules,Terraform,Terraform Modules,Terraform Aws Modules,我得到一个错误: 错误:引用未声明的模块 在modules\security\security.tf第6行的资源“aws\U安全组”“Web sg”中: 6:vpc_id=module.networking.vpcid 安全性中未声明名为“networking”的模块调用 This is directory structure that I am following currently, │ main.tf │ provider.tf │ terraform.tfstate │ terrafor
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\u安全组”“Web sg”中创建了{
vpc_id=module.networking.vpcid
和outputs.tf文件
output "vpcid" {
value = aws_vpc.vpc.id
}
但我得到了这个错误
我做错了什么?我怎样才能修复它?
谢谢
Marcus在./main.tf中声明模块时:
module "networking" {
source = "./modules/networking"
}
module "security" {
source = "./modules/security"
}
对module.networking和module.security的引用仅适用于同一目录中TF文件中的局部变量、资源、数据源、输出和表达式(本例中为./*.TF)
由于./modules/security/security.tf与./main.tf不在同一目录中,因此它不能像您在这里尝试的那样引用module.networking
一个简单的解决方法是将vpcid作为输入变量提供给安全模块,并从模块中获取其值。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
// ...
}
请看,非常感谢阿兰!!我已经在这里测试过了,它正在工作!!!