【问题标题】:Attach EBS volumes to EC2 instances for each terraform将 EBS 卷附加到每个 terraform 的 EC2 实例
【发布时间】:2021-09-02 23:56:04
【问题描述】:

我有使用 terraform 在私有子网中创建的实例。每个实例在其自己的 AZ 中。实例是使用 for each 创建的。我现在将 ebs 卷附加到每个实例,并且在指定为每个实例创建的实例时遇到错误。下面是资源和错误的代码和变量。

   resource "aws_instance" "private" {
      for_each      = var.priv_subnet
      ami           = var.ec2_amis[var.region]
      instance_type = each.value.instance_type
      key_name      = aws_key_pair.main.key_name
      subnet_id     = aws_subnet.private[each.key].id
      vpc_security_group_ids = [
        aws_security_group.main_sg.id,
        aws_security_group.instance_sg.id
      ]
    
      tags = {
        Name = each.value.tag
      }
    }
    
    resource "aws_ebs_volume" "partition" {
      for_each          = var.volumes
      availability_zone = each.value.availability_zone
      size              = each.value.size
    
      tags = {
        Name = each.key
      }
    }

resource "aws_volume_attachment" "ebs_att" {
  for_each    = aws_ebs_volume.partition
  device_name = contains(["Primary", "Worker1", "Worker2"], each.key) ? "/dev/sdf" : "/dev/sdg"
  volume_id   = each.value.id
  instance_id = aws_instance.private.id
}

变量

variable "volumes" {
  type = map(object({
    size              = string
    availability_zone = string
  }))
  default = {
    "Primary" = {
      size              = "200"
      availability_zone = "us-west-2a"
    }
    "PrimarySecondary" = {
      size              = "100"
      availability_zone = "us-west-2a"
    }
    "Worker1" = {
      size              = "200"
      availability_zone = "us-west-2b"
    }
    "Worker1Secondary" = {
      size              = "100"
      availability_zone = "us-west-2b"
    }
    "Worker2" = {
      size              = "200"
      availability_zone = "us-west-2c"
    }
    "Worker2Secondary" = {
      size              = "100"
      availability_zone = "us-west-2c"
    }
  }
}

variable "priv_subnet" {
  type = map(object({
    instance_type = string
    subnet        = string
    tag           = string
  }))
  default = {
    "us-west-2a" = {
      instance_type = "m4.2xlarge"
      subnet        = 4
      tag           = "Primary"
    }
    "us-west-2b" = {
      instance_type = "m4.4xlarge"
      subnet        = 5
      tag           = "Worker1"
    }
    "us-west-2c" = {
      instance_type = "m4.4xlarge"
      subnet        = 6
      tag           = "Worker2"
    }
  }
}

错误

错误:不支持的属性

 on vpc.tf line 51, in resource "aws_volume_attachment" "ebs_att":
 51:   instance_id = aws_instance.private[each.value.tag].id
   |----------------
   | each.value is object with 12 attributes

此对象没有名为“tag”的属性。

【问题讨论】:

    标签: amazon-web-services foreach terraform amazon-ebs


    【解决方案1】:

    我必须指定:

    instance_id = aws_instance.private[each.value.availability_zone].id
    

    在 aws_volume_attachment 资源中

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2013-06-24
      • 2018-07-28
      • 2014-12-09
      相关资源
      最近更新 更多