我有许多嵌套模块的 terraform 设置。简化它看起来像这样
├── modules
│ ├── sec-groups
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ ├── variables.tf
│ │ └── versions.tf
├── dev
│ ├── env.dev.tfvars
│ ├── main.tf
│ ├── versions.tf
│ └── variables.tf
└── prod
├── env.prod.tfvars
├── main.tf
├── versions.tf
└── variables.tf
在开发中的位置:
主文件
module "aws_dev_sec-groups" {
source = "../modules/sec-groups"
vpc_name = aws_vpc.dev_bp_vpc
vpc_id = aws_vpc.dev_bp_vpc.id
localip = var.localip
}
变量.tf
variable "localip" {
type = string
}
env.dev.tfvars
localip = "1.1.1.1/32"
在 sec-groups 模块中:
主文件
resource "aws_security_group" "servers_sg" {
name = "servers_sg"
description = "Traffic allowed to and from Servers"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.localip]
}
}
变量.tf
variable vpc_id {}
variable localip {
type = string
}
--
'terraform init' 产生预期的结果。但是,“terraform plan”会产生以下错误。对我来说,这表明一个空的 localip 变量,这意味着我没有正确声明该变量,是这样吗?
Error: "" is not a valid CIDR block: invalid CIDR address:
on ../modules/sec-groups/main.tf line 63, in resource "aws_security_group" "servers_sg":
63: resource "aws_security_group" "servers_sg" {
}
提前致谢
--
% terraform -v
Terraform v0.13.0
+ provider registry.terraform.io/hashicorp/aws v3.2.0
+ provider registry.terraform.io/hashicorp/random v2.3.0