cloudposse / cloudposse/terraform-aws-ec2-instance-group

Setting ebs_volume_count to more than 1 results in terraform error

Open
#48 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
HCL
Stars
19
Forks
35
PR merge metrics
No merged PRs in 30d

Description

Found a bug? Maybe our [Slack Community](https://slack.cloudposse.com) 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

Contributor guide

Open the contributing guide

Research direction

Start at modules/cp-ec2-group/main.tf line 163 and reproduce the reported error with instance_count = 3 and ebs_volume_count = 2. Trace how count.index maps EBS attachments to aws_instance.default IDs, then verify that the configuration plans successfully and attaches the requested volumes to the intended instances.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, terraform
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.