【发布时间】:2020-12-01 13:18:18
【问题描述】:
我为每个实例创建了多个 EC2 实例。每个实例都部署到不同的子网中。尝试将标签应用于正在部署的每个实例时出现错误。任何意见将是有益的。以下是我的标签和实例的代码:
resource "aws_instance" "private" {
for_each = aws_subnet.private
ami = var.ec2_amis[var.region]
instance_type = var.tableau_instance
key_name = aws_key_pair.tableau.key_name
subnet_id = each.value.id
tags = {
Name = var.ec2_tags[each.key]
}
}
variable "ec2_tags" {
type = list(string)
default = [
"PrimaryEC2",
"EC2Worker1",
"EC2Worker2"
]
}
错误
Error: Invalid index
on vpc.tf line 21, in resource "aws_instance" "private":
21: Name = var.ec2_tags[each.key]
|----------------
| each.key is "3"
| var.ec2_tags is list of string with 3 elements
The given key does not identify an element in this collection value.
我之前有这段代码工作过,不知道发生了什么。我对它启动的 AMI 进行了更改,但我不明白为什么这会对标签产生影响。任何建议都会有所帮助。
更新 我在“aws_instance”“私有”代码中使用以下本地块和动态块更新了资源:
locals {
private_instance = [{
name = "PrimaryEC2"
},
{
name = "EC2Worker1"
},
{
name = "EC2Worker2"
}]
}
dynamic "tags" {
for_each = local.private_instance
content {
Name = tags.value.name
}
}
错误
Error: Unsupported block type
on vpc.tf line 28, in resource "aws_instance" "private":
28: dynamic "tags" {
Blocks of type "tags" are not expected here.
任何解决方法的建议都会有所帮助。谢谢!
【问题讨论】:
标签: amazon-web-services amazon-ec2 foreach