使用terraform创建auto_scaling_group时,错误显示:
omkar@kws MINGW64 ~/repo/kiwi-infra (main)
$ terraform validate
╷
│ Error: Unsupported attribute
│
│ on main.tf line 46, in module "asg":
│ 46: tg = "${module.alb.tg}"
│ ├────────────────
│ │ module.alb is a list of object
│
│ Can't access attributes on a list of objects. Did you mean to access attribute "tg" for a specific element of the list, or across all elements of the list?
,我的代码如下:
铝青铜模块:
resource "aws_lb_target_group" "kiwi-dev-tg" {
health_check {
interval = 10
path = "/"
protocol = "HTTP"
timeout = 5
healthy_threshold = 3
unhealthy_threshold = 2
}
name = "${var.project}-dev-tg"
port = 80
protocol = "HTTP"
target_type = "instance"
vpc_id = "${var.vpc-id}"
}
asg模块:
resource "aws_autoscaling_group" "asg" {
launch_configuration = "${aws_launch_configuration.launch-conf.name}"
vpc_zone_identifier = ["${var.sn1}","${var.sn2 }"]
target_group_arns = "${var.tg}"
health_check_type = "ELB"
asg变量:
variable "tg" {}
输出文件:
output "tg" {
value = aws_lb_target_group.kiwi-dev-tg.arn
}
main.tf
module "asg" {
source = "./modules/asg"
project = "${var.project}"
app-sg = "${module.vpc.app-sg}"
sn1 = "${module.vpc.sn1}"
sn2 = "${module.vpc.sn2}"
tg = "${module.alb.tg}"
}
铝青铜
module "alb" {
source = "./modules/alb"
project = "${var.project}"
vpc-id = "${module.vpc.vpc-id}"
count = "${length(module.ec2.app)}"
app = "${element(module.ec2.app, count.index)}"
#app01 = "${module.ec2.app01}"
#app02 = "${module.ec2.app02}"
alb-sg = "${module.vpc.alb-sg}"
sn1 = "${module.vpc.sn1}"
sn2 = "${module.vpc.sn2}"
}
由于您对使用count
元参数的ALB进行了模块调用,这意味着您必须引用模块的特定索引(因为它成为列表),或者您需要在模块调用中使用相同的count
元参数ASG。例如:
module "asg" {
source = "./modules/asg"
project = "${var.project}"
app-sg = "${module.vpc.app-sg}"
sn1 = "${module.vpc.sn1}"
sn2 = "${module.vpc.sn2}"
tg = "${module.alb[0].tg}" # or any other index that falls into [0, length(module.ec2.app) - 1]
}
如果你需要一个单独的ASG为每个TG和应用程序,那么你需要这个:
module "asg" {
count = "${length(module.ec2.app)}"
source = "./modules/asg"
project = "${var.project}"
app-sg = "${module.vpc.app-sg}"
sn1 = "${module.vpc.sn1}"
sn2 = "${module.vpc.sn2}"
tg = "${module.alb[count.index].tg}"
}
作为旁注,您正在使用旧的地形插值语法,应该更改为:
module "asg" {
count = length(module.ec2.app)
source = "./modules/asg"
project = var.project
app-sg = module.vpc.app-sg
sn1 = module.vpc.sn1
sn2 = module.vpc.sn2
tg = module.alb[count.index].tg
}
此外,target_group_arns
参数应该是一个列表,因此您也必须更新ASG模块以考虑到这一点:
resource "aws_autoscaling_group" "asg" {
launch_configuration = "${aws_launch_configuration.launch-conf.name}"
vpc_zone_identifier = ["${var.sn1}","${var.sn2 }"]
target_group_arns = ["${var.tg}"]
health_check_type = "ELB"
min_size = 2
max_size = 4
tag {
key = "name"
value = "${var.project}-dev-asg"
propagate_at_launch = true
}
}
如果您希望将多个TG分配给ASG,那么您需要更新变量,以便它可以接受列表:
variable "tg" {
description = "List of TG to attach to the ASG."
type = list(string)
}
然后,在模块调用中,以下内容将发生变化:
module "asg" {
source = "./modules/asg"
project = "${var.project}"
app-sg = "${module.vpc.app-sg}"
sn1 = "${module.vpc.sn1}"
sn2 = "${module.vpc.sn2}"
tg = "${module.alb[*].tg}"
}
与对地形语法的一般注释相同:
module "asg" {
source = "./modules/asg"
project = var.project
app-sg = module.vpc.app-sg
sn1 = module.vpc.sn1
sn2 = module.vpc.sn2
tg = module.alb[*].tg
}
您正在尝试将输出的字符串插值版本作为变量传递,该变量将作为模块.asg中的列表读取。所以输出很好,但是你需要改变它被传递到module.asg中使用的方式。主要问题是您没有引用module.alb
的特定实例(用count
实现),并且您正在为target_group_arns
传递单个字符串,其中期望列表。
main中的模块实现。Tf应该看起来像:
module "asg" {
source = "./modules/asg"
project = var.project
app-sg = module.vpc.app-sg
sn1 = module.vpc.sn1
sn2 = module.vpc.sn2
tg = module.alb[*].tg
}
modules/asg/asgmod中的资源。Tf应该看起来像:
resource "aws_autoscaling_group" "asg" {
launch_configuration = aws_launch_configuration.launch-conf.name
vpc_zone_identifier = [var.sn1, var.sn2]
target_group_arns = var.tg # or [var.tf] if you pass single
health_check_type = "ELB"
min_size = 2
max_size = 4
tag {
key = "name"
value = "${var.project}-dev-asg"
propagate_at_launch = true
}
}
注意[var.tg]
,因为aws_autoscaling_group target_group_arns需要一个列表。因此,如果您选择module.alb[0].tg
,例如[var.tg]
将需要,但如果您使用module.alb[*].tg
,那么var.tg
将工作。
此外,我认为在模块/alb/output .tf中:
会更清楚一点output "target_group_arn" {
value = aws_lb_target_group.kiwi-dev-tg.arn
}
然后在modules/asg/var .tf中:
variable "target_group_arns" {
type = list(string)
default = []
}
在模块/asg/asgmod.tf:
resource "aws_autoscaling_group" "asg" {
launch_configuration = aws_launch_configuration.launch-conf.name
vpc_zone_identifier = [var.sn1, var.sn2]
target_group_arns = var.target_group_arns
health_check_type = "ELB"
min_size = 2
max_size = 4
tag {
key = "name"
value = "${var.project}-dev-asg"
propagate_at_launch = true
}
}
最后,在main.tf:
module "asg" {
source = "./modules/asg"
project = var.project
app-sg = module.vpc.app-sg
sn1 = module.vpc.sn1
sn2 = module.vpc.sn2
target_group_arns = module.alb[*].target_group_arn
}
这对我来说似乎更清楚了。