Skip to content

Setting ebs_volume_count to more than 1 results in terraform error #48

Open
@gsefanof

Description

@gsefanof

Found a bug? Maybe our Slack Community can help.

Describe the Bug

When set ebs_volume_count = 2 (more than 1), it will result in terraform error

╷
│ Error: Invalid index
│ 
│   on modules/cp-ec2-group/main.tf line 163, in resource "aws_volume_attachment" "default":
│  163:   instance_id = aws_instance.default.*.id[count.index]
│     ├────────────────
│     │ aws_instance.default is tuple with 1 element
│     │ count.index is 1
│ 
│ The given key does not identify an element in this collection value: the given index is greater than or equal to the length of the collection.

Expected Behavior

Create and attach two additional ebs volumes

Steps to Reproduce

for example

module "instance" {
  source = "./modules/cp-ec2-group"

  namespace                   = "eg"
  stage                       = "prod"
  name                        = "app"
  ami                         = "ami-a4dc46db"
  ami_owner                   = "099720109477"
  ssh_key_pair                = var.ssh_key_pair
  instance_type               = var.instance_type
  vpc_id                      = var.vpc_id
  security_groups             = var.security_groups
  subnet                      = var.subnet
  instance_count              = 3
  ebs_volume_count            = 2
}

Environment (please complete the following information):

  • OS: Linux, Mint 20.3, 5.4.0-139-generic
  • Terraform v1.3.7

Additional Context

Could be solved by replace string in main.tf, at line 162.
e.g.:
instance_id = element(aws_instance.default.*.id, floor(count.index / max(var.ebs_volume_count, 1)))

p.s.
there is a limitation, changing the number of EBS will lead to reattaching disks to different instances

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug🐛 An issue with the system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions