Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External volume get destroyed on instance increas #122

Closed
heino-vdbh opened this issue Aug 2, 2019 · 4 comments
Closed

External volume get destroyed on instance increas #122

heino-vdbh opened this issue Aug 2, 2019 · 4 comments

Comments

@heino-vdbh
Copy link

I use this module and attach a external volume as shown in the examples.
But when I increase the instance_count the volume get destroyed and recreated.

module "webserver" {
  source                                    = "terraform-aws-modules/ec2-instance/aws"
  version                                    = "~> 2.0"
  name                                       = "${var.ec2_name}"
  instance_count                       = "${var.instance_count}"
  ami                                          = data.aws_ami.centos7.id
  instance_type                         = "${var.instance_type}"
  key_name                                = "${var.key_name}"
  monitoring                              = "${var.monitoring}"
  vpc_security_group_ids.         = ["${module.webserver_sg.this_security_group_id}"]
  subnet_ids                              = data.terraform_remote_state.vpc.outputs.vpc_public_subnets
  associate_public_ip_address = "${var.associate_public_ip_address}"

  tags                                        = "${var.ec2_tags}"
}
resource "aws_volume_attachment" "webserver" {
  count                = var.instance_count
  device_name.    = "/dev/sdh"
  volume_id          = aws_ebs_volume.webserver[count.index].id
  instance_id        = module.webserver.id[count.index]
}

resource "aws_ebs_volume" "webserver" {
  count                      = var.instance_count
  availability_zone     = module.webserver.availability_zone[count.index]
  size                        = var.ebs_size
  encrypted              = true
  kms_key_id            = data.terraform_remote_state.vpc.outputs.kms_key_arn
  tags = {
    Name = var.instance_count > 1 ? format("%s-%d", var.ec2_name, count.index + 1) : var.ec2_name
  }
}

ERROR

  # aws_ebs_volume.webserver[0] must be replaced
-/+ resource "aws_ebs_volume" "webserver" {
      ~ arn               = "arn:aws:ec2:eu-central-1:123456789012:volume/vol-0ffb96d6967f0e8d0" -> (known after apply)
      ~ availability_zone = "eu-central-1a" -> (known after apply) # forces replacement
        encrypted         = true
      ~ id                = "vol-0ffb96d6967f0e8d0" -> (known after apply)
      ~ iops              = 150 -> (known after apply)
        kms_key_id        = "arn:aws:kms:eu-central-1:123456789012:key/ac63ab1f-9bf8-4d92-97a6-15e162aaeb72"
        size              = 50
      + snapshot_id       = (known after apply)
      ~ tags              = {
          ~ "Name" = "approval" -> "approval-1"
        }
      ~ type              = "gp2" -> (known after apply)
    }
@heino-vdbh
Copy link
Author

I have fixed the destroy of the volume by putting the AZs into a variable.

resource "aws_ebs_volume" "webserver" {
  count = var.instance_count


  availability_zone ="${element(var.azs, count.index)}"
  size              = var.ebs_size
  encrypted = true
  kms_key_id = data.terraform_remote_state.vpc.outputs.kms_key_arn

  tags = {
    Name = var.instance_count > 1 ? format("%s-%d", var.ec2_name, count.index + 1) : var.ec2_name
  }
}

@heino-vdbh
Copy link
Author

heino-vdbh commented Aug 4, 2019

But the aws_volume_attachment of the existing instance get still a forces replacement if i increase the instance_count

-/+ resource "aws_volume_attachment" "webserver" {
        device_name = "/dev/sdh"
      ~ id          = "vai-1312302379" -> (known after apply)
      ~ instance_id = "i-34542354525" -> (known after apply) # forces replacement
        volume_id   = "vol-234532525"
    }

@antonbabenko
Copy link
Member

@hniemann I think you can try to use for_each instead of count for this situation and loop through ids instead of count indexes.

Check this for more details - https://www.terraform.io/docs/configuration/resources.html#when-to-use-for_each-instead-of-count

This is not a bug of the module, so I am closing this issue.

@github-actions
Copy link

github-actions bot commented Nov 9, 2022

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants