title: How to create multiple volumes and attach to each instance via Terraform
body: <p>The below code works when <code>ec2_instance_count = 1</code></p>
  
  <p>However, when <code>ec2_instance_count = 2</code> the following errors appears.</p>
  
  <pre><code>Error: Error running plan: 1 error(s) occurred:
  
  * module.build_ec2_app.aws_ebs_volume.ebs_volume: 1 error(s) occurred:
  
  * module.build_ec2_app.aws_ebs_volume.ebs_volume: Resource 'aws_instance.ec2' not found for variable 'aws_instance.ec2.availability_zone'
  </code></pre>
  
  <p>I believe it has something to do with the if statement <code>var.ec2_create_volume == "true" ? 1 : 0</code></p>
  
  <p>But even if I change <code>"true" ? 1 : 0</code> to <code>"true" ? 2 : 0</code> on both resources I still get an error when <code>ec2_instance_count = 2</code></p>
  
  <pre><code># Create EC2 instances
  resource "aws_instance" "ec2" {
    count                   = "${var.ec2_instance_count}"
    ami                     = "${var.ec2_instance_ami_id}"
    instance_type           = "${var.ec2_instance_type}"
    subnet_id               = "${var.ec2_instance_subnet_id}"
    key_name                = "${var.ec2_instance_key_name}"
    vpc_security_group_ids  = ["${aws_security_group.create_security_group.id}"]
  
    tags {
      Name                  = "${var.ec2_app_name}0${count.index + 1}.${var.ec2_tl_domain_name}"
      service               = "${var.ec2_service}"
      environment           = "${var.ec2_environment}"
    }
  }
  
  # Create EBS volume
  resource "aws_ebs_volume" "ebs_volume" {
    count                 = "${var.ec2_create_volume == "true" ? 1 : 0}"
    availability_zone     = "${aws_instance.ec2.availability_zone}"
    size                  = "${var.ec2_ebs_volume_size}"
  }
  
  # Attach EBS Volume
  resource "aws_volume_attachment" "volume_attachment" {
    count                 = "${var.ec2_create_volume == "true" ? 1 : 0}"
    device_name           = "${var.ec2_device_name}"
    volume_id             = "${aws_ebs_volume.ebs_volume.id}"
    instance_id           = "${aws_instance.ec2.id}"
  }
  </code></pre>