我对 Terraform 很陌生,我正在使用这个函数来重用相同的子网列表来启动比我拥有的子网更多的实例。 (它只会循环(如果我提供自己的子网映射,但远程状态的数据是元组并且出现此错误,则此方法非常有用:
Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument. data.terraform_remote_state.vpc.outputs.private_subnets is tuple with 4 elements
我也尝试了tomap(函数,但这失败了:
Invalid value for "v" parameter: cannot convert tuple to map of any single
type.
这是我的代码:
count = var.instance_count
ami = var.ami
instance_type = "t2.medium"
subnet_id = lookup(data.terraform_remote_state.vpc.outputs.private_subnets, count.index%length(data.terraform_remote_state.vpc.outputs.private_subnets))
vpc_security_group_ids = ["${data.terraform_remote_state.foo_sg.outputs.foo_sg_id}"]
key_name = var.key_name
iam_instance_profile = var.iam_instance_profile
user_data = <<-EOF
#!/bin/bash
hostnamectl set-hostname --static "${var.app_name}-${count.index + 1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name}"
echo "127.0.0.1 localhost.localdomain localhost4 localhost4.localdomain4 ${var.app_name}-${count.index + 1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name} localhost" > hosts
echo "::1 localhost localhost.localdomain localhost6 localhost6.localdomain6" >> hosts
EOF
tags = {
Name = "${var.app_name}-${count.index +1}.${data.terraform_remote_state.vpc.outputs.private_zone_domain_name}"
}
就像我说的,我的目标是在我拥有的远程状态下重用 4 个子网,所以如果我想要 6 个实例,它将遍历我拥有的 4 个实例,第 5 个和第 6 个实例将位于元组的子网 1 和 2 上。任何建议将不胜感激!
你有 3 种形式来制作循环。
1º 使用dynamic block
...例:
resource "aws_security_group" "ecc_default" {
count = length(var.clusters)
vpc_id = var.vpc_id
name = "${terraform.workspace}.ecc-${var.clusters[count.index].engine}.sg"
description = "${terraform.workspace}.ecc-${var.clusters[count.index].engine}.sg"
ingress {
from_port = var.clusters[count.index].port
to_port = var.clusters[count.index].port
protocol = "tcp"
cidr_blocks = [var.vpc_cidr]
}
dynamic "ingress" {
iterator = item
for_each = var.enable_vpn ? var.vpn_cidr : []
content {
from_port = var.clusters[count.index].port
to_port = var.clusters[count.index].port
protocol = "tcp"
cidr_blocks = [item.value["cidr"]]
description = item.value["description"]
}
}
...
...
}
2º 使用for_each
...(点击这里回答(
3º 使用算术迭代...(点击这里查看答案( 示例:
resource "aws_security_group_rule" "ecc_internal" {
count = length(var.ingress_security_groups) > 0 ? length(var.ingress_security_groups) * length(var.clusters) : 0
security_group_id = aws_security_group.ecc_default[count.index % length(var.clusters)].id
type = "ingress"
from_port = var.clusters[count.index % length(var.clusters)].port
to_port = var.clusters[count.index % length(var.clusters)].port
protocol = "tcp"
source_security_group_id = var.ingress_security_groups[floor(count.index / length(var.clusters))]
}
并且,对于转换时的错误:尝试将coalescelist
与假替换一起使用。喜欢这个:
resource "aws_lb_listener_certificate" "default" {
count = (coalescelist(data.aws_alb_listener.https, [{ certificate_arn = "" }])[0].certificate_arn != var.certificate_arn) ? local.enabled : 0
listener_arn = coalescelist(data.aws_alb_listener.https, [{ arn = "" }])[count.index].arn
certificate_arn = var.certificate_arn
}
将元组([N1, N2, N3]
(转换为地图({N1 = "", N2 = ""}
(是绝对不允许的。但是,您可以使用假替代品来完成这项工作。tomap
需要一个参数对象。map
,需要多个参数才能得到结果。尝试操作您的列表,在地图中转换,使用另一种形式(如果您只想转换,而不是解决问题(:
environment_vars = jsonencode([
for key in sort(keys(data.external.environment_vars.result)) : {
name = key
value = lookup(data.external.environment_vars.result, key)
}])
我推荐给你:Terraform提示和技巧:循环,if语句和陷阱