【发布时间】:2020-10-15 22:08:02
【问题描述】:
我尝试创建具有多个入站规则的 AWS 安全组,通常我们需要在 sg 中为多个入站规则创建多个入口。我没有单独创建多个入口规则,而是尝试创建入口列表,以便我可以轻松地将模块重用于不同的应用程序。
PFB,
模块/sg/sg.tf >>
resource "aws_security_group" "ec2_security_groups" {
name = var.name_security_groups
vpc_id = var.vpc_id
}
模块/sg/rules.tf >>
resource "aws_security_group_rule" "ingress_rules" {
count = lenght(var.ingress_rules)
type = "ingress"
from_port = var.ingress_rules[count.index][0]
to_port = var.ingress_rules[count.index][1]
protocol = var.ingress_rules[count.index][2]
cidr_blocks = var.ingress_rules[count.index][3]
description = var.ingress_rules[count.index][4]
security_group_id = aws_security_group.ec2_security_groups.id
}
模块/sg/variable.tf >>
variable "vpc_id" {
}
variable "name_security_groups" {
}
variable "ingress_rules" {
type = list(string)
}
在应用程序文件夹中,
应用程序/dev/sg.tf >>
module "sg_test" {
source = "../modules/sg"
vpc_id = "vpc-xxxxxxxxx"
name_security_groups = "sg_test"
ingress_rules = var.sg_ingress_rules
}
应用程序/dev/variable.tf >>
variable "sg_ingress_rules" {
type = list(string)
default = {
[22, 22, "tcp", "1.2.3.4/32", "test"]
[23, 23, "tcp", "1.2.3.4/32", "test"]
}
}
错误:
Error: Missing attribute value
on test-sgs.tf line 21, in variable "sg_ingress_rules":
20:
21:
22:
Expected an attribute value, introduced by an equals sign ("=").
请帮助纠正这个问题,或者如果有任何其他方法,请提出建议。
问候,
【问题讨论】:
-
您的数据结构没有意义。如果您希望它是地图列表,您可以使用
[{from_port: 22, to_port: 22, protocol: "tcp", cidr_range: "1.2.3.4/32", description: "test"}, {from_port: 23, to_port: 23, protocol: "tcp", cidr_range: "1.2.3.4/32", description: "test"}]之类的内容。但是现在你有一张没有钥匙的破损地图。您还需要更改访问它的方式。或者,您可以有一个列表列表来保存您访问它的方式,但这对我来说描述性较差。 -
@ydaetskcoR 回复,我也尝试使用本地人:- locals { sg_ingress_rules = [ { from_port = 80, to_port = 80, protocol = tcp, cidr_blocks = "1.2.3.4/32",描述 = “测试”},{ from_port = 443,to_port = 443,协议 = tcp,cidr_blocks = “1.2.3.4/32”,描述 = “测试”},{ from_port = 22,to_port = 22,协议 = tcp, cidr_blocks = "1.2.3.4/32", description = "test" }, ] } 出现错误:- 未在根模块中声明托管资源“locals”“sg_egress_rules”。
标签: amazon-web-services terraform terraform-provider-aws