Reputation: 1879
I am getting below error while executing Terraform plan
. I am following modules concept here.
Below is my resource aws_efs_mount_target
and aws_subnet
blocks.
efs.tf:-
resource "aws_efs_file_system" "master_efs" {
creation_token = "master"
performance_mode = "generalPurpose"
kms_key_id = "${data.terraform_remote_state.kms_ebs.outputs.key_arn}"
encrypted = "true"
tags = {
Name = "master"
Environment = "${var.environment}"
Terraform = "true"
}
}
resource "aws_efs_mount_target" "master_mt" {
file_system_id = "${aws_efs_file_system.master_efs.id}"
count = "${length(var.availability_zones)}"
subnet_id = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
security_groups = [ "${aws_security_group.sg.id}" ]
}
data "aws_subnet" "app_subnet_0" {
vpc_id = "${data.aws_vpc.cng.id}"
filter {
name = "tag:Name"
values = ["${var.search_pattern_app}-0"]
}
}
Error: Invalid index
on ../../modules/efs.tf line 16, in resource "aws_efs_mount_target" "master_mt":
16: subnet_id = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
|----------------
| count.index is 2
| data.aws_subnet.app_subnet_0 is object with 15 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on ../../modules/efs.tf line 16, in resource "aws_efs_mount_target" "master_mt":
16: subnet_id = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
|----------------
| count.index is 1
| data.aws_subnet.app_subnet_0 is object with 15 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on ../../modules/efs.tf line 35, in resource "aws_efs_mount_target" "worker_mt":
35: subnet_id = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
|----------------
| count.index is 1
| data.aws_subnet.app_subnet_0 is object with 15 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on ../../modules/efs.tf line 35, in resource "aws_efs_mount_target" "worker_mt":
35: subnet_id = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
|----------------
| count.index is 2
| data.aws_subnet.app_subnet_0 is object with 15 attributes
The given key does not identify an element in this collection value.
Upvotes: 0
Views: 2338
Reputation: 56877
The aws_subnet
data source returns a single subnet, not a list and you don't appear to be looping over it with count
.
If you're expecting multiple subnets to be returned then you probably want the aws_subnet_ids
data source instead.
So if you want to create an EFS mount target in each subnet that matches a subnet filter you could use the following:
resource "aws_efs_file_system" "master_efs" {
creation_token = "master"
performance_mode = "generalPurpose"
kms_key_id = "${data.terraform_remote_state.kms_ebs.outputs.key_arn}"
encrypted = "true"
tags = {
Name = "master"
Environment = "${var.environment}"
Terraform = "true"
}
}
data "aws_subnet_ids" "app_subnet" {
vpc_id = "${data.aws_vpc.cng.id}"
tags = {
Name = "${var.search_pattern_app}"
}
}
resource "aws_efs_mount_target" "master_mt" {
file_system_id = "${aws_efs_file_system.master_efs.id}"
count = "${length(var.availability_zones)}"
subnet_id = "${data.aws_subnet.app_subnet.ids[count.index]}"
security_groups = ["${aws_security_group.sg.id}"]
}
This finds all the subnets with a name matching the search_pattern_app
variable and creates EFS mount targets in each of them.
Upvotes: 1
Reputation: 33
From what I'm understanding from your code you're are trying to create a mounting point for your EFS for each subnet in VPC (one per availability zone), and to do that you want to Terraform to automatically parse the availability zone and then assign the subnet id for the mount target ? Apparently the code you wrote is trying to match one of the subnet attribute with the value of the counter, if you just want to add aws_subnet.app_subnet_0.1 to aws_subnet.app_subnet_0.x maybe you can put it that way
aws_subnet.app_subnet_0.${count.index}.id
Upvotes: 1