Skip to content

instance count and volume attachement #49

New issue

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

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

Already on GitHub? # to your account

Closed
NFhbar opened this issue Sep 20, 2018 · 3 comments
Closed

instance count and volume attachement #49

NFhbar opened this issue Sep 20, 2018 · 3 comments

Comments

@NFhbar
Copy link

NFhbar commented Sep 20, 2018

Based on the attach volume example, how can you attach aws volumes to all instances that are created when instance_count is greater than 1?

  module "ec2" {
  source = "../../"

  instance_count = "${var.instances}" .  --> this is greater than 1

  name                        = "example-with-ebs"
  ami                         = "${data.aws_ami.amazon_linux.id}"
  instance_type               = "m4.large"
  subnet_id                   = "${element(data.aws_subnet_ids.all.ids, 0)}"
  vpc_security_group_ids      = ["${module.security_group.this_security_group_id}"]
  associate_public_ip_address = true
}

resource "aws_volume_attachment" "this_ec2" {

  device_name = "/dev/sdh"
  volume_id   = "${aws_ebs_volume.this.id}"

  # ================ ISSUE HERE
  count = "${length(module.ec2.id)}"
  instance_id =  "${element(module.ec2.id, count.index)}" 
  # ================ 
}

resource "aws_ebs_volume" "this" {
  availability_zone = "${module.ec2.availability_zone[0]}"
  size              = 1
}
@vukomir
Copy link

vukomir commented Dec 6, 2018

Hi @NFhbar

you can use the same variable when you specify the count number

module "ec2" {
  source                      = "../modules/ec2"
  instance_count              = "${var.fe_ec2_count}"
  name                        = "app"
  ami                         = "${var.fe_ec2_ami}"
  instance_type               = "t2.micro"
  subnet_id                   = ["${data.terraform_remote_state.vpc.private_subnets_id}"]
  vpc_security_group_ids      = ["${data.terraform_remote_state.vpc.sg_fe_id}"]
}

resource "aws_volume_attachment" "this_ec2" {
  count                       = ${var.fe_ec2_count}"
  device_name                 = "/dev/sdh"
  volume_id                   = "${element(aws_ebs_volume.this.*.id, count.index)}"
  instance_id                 = "${element(module.ec2_fe.id, count.index)}"
}

resource "aws_ebs_volume" "this" {
  count                       = ${var.fe_ec2_count}"
  availability_zone           = "${element(var.availability_zones, count.index)}"
  type                        = "gp2"
  size                        = 1
  
  tags                        = "${var.default_tags}"
  depends_on                  = ["module.ec2_fe"]
}

@antonbabenko
Copy link
Member

Closing as resolved. See examples for more details.

@github-actions
Copy link

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 10, 2022
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants