amazon-web-services - 传递 ec2 实例 id 时 terraform 中的属性值类型错误
问题描述
我正在使用以下代码创建 3 个 ec2 实例并将它们注册到负载均衡器,
变量
instance_type = "t3.large"
root_block_volume_type = "standard"
root_block_volume_size = "50"
instance_count = "3"
ec2 创建
resource "aws_instance" "ec2" {
count = "${var.instance_count}"
ami = "${var.ami_id}"
instance_type = "${var.instance_type}"
key_name = "${var.key_pair_name}"
subnet_id = "${var.private_subnet_id}"
iam_instance_profile = "${aws_iam_instance_profile.iam_instance_profile.name}"
/*
* CAUTION: changing value of below fields will cause the EC2 instance to be terminated and
* re-created. Think before running the "apply" command.
*/
associate_public_ip_address = false
tags = {
Environment = "${var.env}"
Project = "${var.project}"
Provisioner="different-box"
Name = "${local.name}-1"
}
root_block_device {
volume_type = "${var.root_block_volume_type}"
volume_size = "${var.root_block_volume_size}"
}
}
resource "aws_network_interface_sg_attachment" "sg_attachment" {
count = "${var.instance_count}"
security_group_id = "${aws_security_group.ec2_sg.id}"
network_interface_id = "${aws_instance.ec2[count.index].primary_network_interface_id}"
}
向负载均衡器注册
resource "aws_alb_target_group_attachment" "alb_target_group_attachment" {
count = length("${var.ec2_instance_ids}")
target_group_arn = "${aws_alb_target_group.alb_target_group.arn}"
target_id = "${var.ec2_instance_ids[count.index]}"
port = "${var.alb_target_group_port}"
}
但是,当我如下传递 ec2 实例 ID 时,
module "alb_engine-ui" {
source = "./modules/load-balancer"
env = "${lower(var.env)}"
project = "engine-ui"
vpc_id = "${data.aws_vpc.main.id}"
public_subnet1_id = "${var.public_subnet1_id}"
public_subnet2_id = "${var.public_subnet2_id}"
health_check_target_group_path = "/"
certificate_arn = "${var.certificate_arn}"
alb_target_group_port = "2016"
ec2_instance_ids = ["${aws_instance.ec2[*].id}"]
}
variable "ec2_instance_ids" {
description = "the ec2 instance ids to be used for alb target group"
type = "list"
}
我收到以下错误,
Error: Incorrect attribute value type
on provisioners/different-box/modules/load-balancer/resources.tf line 109, in resource "aws_alb_target_group_attachment" "alb_target_group_attachment":
109: target_id = "${var.ec2_instance_ids[count.index]}"
Inappropriate value for attribute "target_id": string required.
有没有办法避免这个错误并将 ec2 ids 传递给列表?
解决方案
"${aws_instance.ec2[*].id}"
已经是一个 ID 列表,所以如果你把它放在方括号中,它就会成为一个列表列表。
相反,删除方括号会将其保留为字符串列表,因此当您索引它时,您会得到一个target_id
参数想要的单个字符串。
推荐阅读
- cassandra - 关于cassandra分页机制和集群使用规范的问题
- php - PHP/AJAX 为什么我的 getAttribute 只针对 foreach 代码中的第一行
- kubernetes - 3 节点设置中的 Kubernetes 安装
- php - SQL 错误:1064 您的 SQL 语法有错误
- vba - 尝试保存时 VBA 宏抛出错误(但仅在一台计算机上)
- angular - 如何在 Angular 中返回 RxJS Observable 的已完成状态
- android - 二进制 XML 文件行:膨胀类时出错
- python - NLTK 中的类似函数总是返回“No Matches”结果
- postgresql - 如何在 Postgres 中对时间段进行逻辑运算?
- wordpress - 带有嵌套中继器的 Wordpress Elementor 自定义小部件