简体   繁体   English

将标签应用于为每个 Terraform 创建的实例

[英]Applying tags to instances created with for each Terraform

I have multiple EC2 instances created using for each.我为每个创建了多个 EC2 实例。 Each instance is being deployed into a different subnet.每个实例都部署到不同的子网中。 I am getting an error when trying to apply tags to each instance being deployed.尝试将标签应用于正在部署的每个实例时出现错误。 Any advice would be helpful.任何意见将是有益的。 Below is the code for my tags and instances:以下是我的标签和实例的代码:

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错误

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.

I had this code working earlier, not sure what happened.我之前有这个代码工作,不知道发生了什么。 I made a change to the AMI it spins up, but I don't see why that could have an effect on tags.我对它启动的 AMI 进行了更改,但我不明白为什么这会对标签产生影响。 Any advice would be helpful.任何意见将是有益的。

UPDATE I have updated the resource with the following locals block and dynamic block within my "aws_instance" "private" code:更新我在“aws_instance”“私有”代码中使用以下本地块和动态块更新了资源:

locals {
  private_instance = [{
    name = "PrimaryEC2"
    },
    {
      name = "EC2Worker1"
    },
    {
      name = "EC2Worker2"
  }]
}


    dynamic "tags" {
    for_each = local.private_instance

    content {
      Name = tags.value.name
    }
  }

Error错误

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.

Any advice how to fix would help.任何如何解决的建议都会有所帮助。 Thanks!谢谢!

If you want to make your tags dynamic, you could create them as follows:如果你想让你的标签动态化,你可以按如下方式创建它们:

  tags = {
    Name = each.key == "0" ? "PrimaryEC2" : "EC2Worker${each.key}" 
  }

You would use it as follows (assuming everything else is OK):您可以按如下方式使用它(假设其他一切正常):

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 = each.key == "0" ? "PrimaryEC2" : "EC2Worker${each.key}" 
  }
}

The code uses conditional expression .该代码使用条件表达式 It works as follows.它的工作原理如下。

If each.key is equal to "0" (ie, first instance being created) then its tag will be "PrimaryEC2".如果 each.key 等于“0”(即,正在创建第一个实例),则其标签将为“PrimaryEC2”。 All remaining instances will be tagged: "EC2Worker1", "EC2Worker2", "EC2Worker3" and so on for as many subnets there are.所有剩余的实例都将被标记为:“EC2Worker1”、“EC2Worker2”、“EC2Worker3”等等,用于尽可能多的子网。

One possible cause of this errors is that the aws_subnet.private variable is longer then the list of ec2 tags which would result in an error when the index 3 is used on your ec2_tags list looking for the 4th (nonexistent element).此错误的一个可能原因是aws_subnet.private变量比 ec2 标签列表长,当您在 ec2_tags 列表中使用索引 3 查找第 4 个(不存在的元素)时,这将导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM