Terraform v. 0.9.11I have two directories one for EBS and other for EC2. I run terraform apply for EBS and then I go and give apply for EC2.
I have
main.tf and
outputs.tf in EBS as follows:
main.tfresource "aws_ebs_volume" "myapp-ebs" {
availability_zone = "${element(var.availability_zones, 0)}"
size = 100
type = "gp2"
count = 2
tags {
Name = "${var.environment}-elasticsearch01"
}
}
outputs.tfoutput "app_ebs_1" {
value = "${aws_ebs_volume.myapp-ebs.0.id}"
}
output "app_ebs_2" {
value = "${aws_ebs_volume.myapp-ebs.1.id}"
}
This works perfectly well and gives me volume ID of two EBS created.
This is my
main.tf file for EC2 instance.
main.tf
/* Fetch Volume IDs from EBS module for use in here */
data "terraform_remote_state" "ebs_data" {
backend = "local"
config {
path = "${path.module}/../ebs/terraform.tfstate"
}
}
resource "aws_instance" "app-box" {
ami = "${lookup(var.ami_linux, var.region)}"
instance_type = "${element(var.instance_types, 5)}"
subnet_id = "${element(var.subnet_ids, 0)}"
security_groups = ["${aws_security_group.kafka-security-group.id}"]
count = 2
key_name = "${var.key_name}"
private_ip = "${element(var.kafka-ips, count.index)}"
iam_instance_profile = "${data.terraform_remote_state.iam_data.iam_profile_out}"
user_data = "${file("bootstrap_app.sh")}
tags {
Name = "${var.environment}-app0${count.index + 1}"
}
}
/* Attaching EBS Volumes to App instances */
resource "aws_volume_attachment" "a-app_ebs1" {
device_name = "/dev/xvdf"
volume_id = "${data.terraform_remote_state.ebs_data.app_ebs_1}"
instance_id = "${aws_instance.app-box.0.id}"
force_detach = true
}
resource "aws_volume_attachment" "a-app_ebs2" {
device_name = "/dev/xvdf"
volume_id = "${data.terraform_remote_state.ebs_data.app_ebs_2}"
instance_id = "${aws_instance.app-box.1.id}"
force_detach = true
}
Now the problem here is, 2 EC2 instances get created but the Volume attachment fails for the second instance. Plan and apply both fails with the following error:
* aws_volume_attachment.a-app_ebs2: Resource 'aws_instance.app-box' not found for variable 'aws_instance.app-box.1.id'Not sure what mistake I am making here. Any improvements I can do to avoid this issue? Help much appreciated. I have to push this urgently for everyday use.