Referring to resources named with variables in Terraform
<p>I'm trying to create a module in Terraform that can be instantiated multiple times with different variable inputs. Within the module, how do I reference resources when their names depend on an input variable? I'm trying to do it via the bracket syntax (<code>"${aws_ecs_task_definition[var.name].arn}"</code>) but I just guessed at that.</p>

<p>(Caveat: I might be going about this in completely the wrong way)</p>

<p>Here's my module's (simplified) <code>main.tf</code> file:</p>

<pre><code>variable "name" {}

resource "aws_ecs_service" "${var.name}" {
    name = "${var.name}_service"
    cluster = ""
    task_definition = "${aws_ecs_task_definition[var.name].arn}"
    desired_count = 1
}

resource "aws_ecs_task_definition" "${var.name}" {
    family = "ecs-family-${var.name}"
    container_definitions = "${template_file[var.name].rendered}"
}

resource "template_file" "${var.name}_task" {
    template = "${file("task-definition.json")}"

    vars {
        name = "${var.name}"
    }
}
</code></pre>

<p>I'm getting the following error:</p>

<pre><code>Error loading Terraform: Error downloading modules: module foo: Error loading .terraform/modules/af13a92c4edda294822b341862422ba5/main.tf: Error reading config for aws_ecs_service[${var.name}]: parse error: syntax error
</code></pre>