简体   繁体   English

将 EBS 卷附加到每个 terraform 的 EC2 实例

[英]Attach EBS volumes to EC2 instances for each terraform

I have instances created in a private subnet using terraform.我有使用 terraform 在私有子网中创建的实例。 Each instance in its own AZ.每个实例在其自己的 AZ 中。 The instances were created using for each.实例是使用 for each 创建的。 I am now attaching ebs volumes to each of the instances and am running into an error specifying the instances created with for each.我现在将 ebs 卷附加到每个实例,并且在指定为每个实例创建的实例时遇到错误。 Below is the code and variables for the resources and the error.下面是资源和错误的代码和变量。 Any advice would be appreciated.任何意见,将不胜感激。

   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
}

Variables变量

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"
    }
  }
}

Error错误

Error: Unsupported attribute

  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

This object does not have an attribute named "tag".

Got my own answer.得到了我自己的答案。 I had to specify:我必须指定:

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

in aws_volume_attachment resource在 aws_volume_attachment 资源中

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

相关问题 为 EC2 实例配置实例配置文件以附加/分离/等待 EBS 卷并使用 S3? - Configure instance profile for EC2 instances to attach/detach/wait for EBS volumes and work with S3? Terraform EC2实例+ EBS问题 - Terraform ec2 instances + EBS issues Terraform-创建EBS快照,然后将快照转换为EBS并附加到EC2 - Terraform - Create Snapshot of EBS and then convert Snapshot to EBS and attach to an EC2 如何创建3个EBS卷并通过Terraform附加到每个实例 - How to create 3 EBS volumes and attach to each instance via Terraform 将多个 ebs 卷附加到每个 ec2 实例 - attaching multiple ebs volumes to each ec2 instance 附加现有 EBS 卷以在 Terraform 中启动配置 - Attach existing EBS volumes to launch configuration in Terraform EBS 卷和 EC2 实例映射 - EBS volumes and EC2 instance mapping 如何将现有 EC2 实例附加到 terraform 中的 Auto Scaling 组? - How to attach existing EC2 instances to auto scaling group in terraform? 是否可以将新的EC2实例附加到当前已附加到可访问性检查失败的另一个EC2实例的EBS卷上? - Can a new EC2 instance attach to EBS volumes that are currently attached to another EC2 instance that's failed in reachability check? 如何使用 Terraform 创建不同大小的 EBS 卷并附加到单个 EC2 实例? - How to create the different size of EBS volume and attach to single EC2 instance using Terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM