I am creating a terraform configuration to allow user to input the number of AWS EBS volumes they want to attach to the EC2 instance.
variable "number_of_ebs" {}
resource "aws_volume_attachment" "ebs_att" {
count = "${var.number_of_ebs}"
device_name= "/dev/sdh"
volume_id = "${element(aws_ebs_volume.newVolume.*.id, count.index)}"
instance_id = "${aws_instance.web.id}"
}
resource "aws_instance" "web" {
ami = "ami-14c5486b"
instance_type = "t2.micro"
availability_zone = "us-east-1a"
vpc_security_group_ids=["${aws_security_group.instance.id}"]
key_name="KeyPairVirginia"
tags {
Name = "HelloWorld"
}
}
resource "aws_ebs_volume" "newVolume" {
count = "${var.number_of_ebs}"
name = "${format("vol-%02d", count.index + 1)}"
availability_zone = "us-east-1a"
size = 4
type="standard"
tags {
Name = "HelloWorld"
}
}
It surely is giving error. I am unaware of how to dynamically assign different name to each volume that is created and get volume_id to the attach to the instance.
Below is the error that I get.
var.number_of_ebs
Enter a value: 2
Error: aws_ebs_volume.newVolume[0]: : invalid or unknown key: name
Error: aws_ebs_volume.newVolume[1]: : invalid or unknown key: name