0.12 splat error "An attribute name is required after a dot"

Hi! Please advise on this error.
I want to output all instance ids, that are declared in current state:

output "instance_ids" {
  value = [aws_instance.[*].id]
}

terraform apply gives this error:

Error: Invalid attribute name

  on configuration.tf line 104, in output "instance_ids":
 104:   value = [aws_instance.[*].id]

An attribute name is required after a dot.

I’m not 100% sure since I can’t test this right now, but does

output "instance_ids" {
  value = [aws_instance[*].id]
}

work? Try removing the dot after instance?

@fortman nope…

Error: Invalid reference

  on configuration.tf line 104, in output "instance_ids":
 104:   value = [aws_instance[*].id]

A reference to a resource type must be followed by at least one attribute
access, specifying the resource name.

Hi @dev-e!

There are two problems here. One of them was correctly identified by @fortman: the [*] splat syntax follows the precedent of the index syntax (like [0]) and doesn’t use a leading dot marker.

The second problem is that you cannot dynamically select instances like that. A minimal reference to a resource must always include a resource name, like aws_instance.foo. You can then apply any arbitrary traversal operators to that, such as using the splat operator to map over all of the instances of a particular resource:

  value = [aws_instance.example[*].id]

If you have a set of resources that are related such that you will use them together then you can either represent them all as a single resource using count (assuming they are essentially just multiple copies of the same concept) or you must construct manually the list of specific resources you want to include:

  value = concat(
    aws_instance.foo[*].id,
    aws_instance.bar[*].id,
    aws_instance.baz[*].id,
  )

Terraform uses these references to decide the order of operations in the execution plan, which is why it requires you to select specific resources statically by name rather than making dynamic selections by expressions. This is one of the implementation/design tradeoffs of Terraform’s declarative model.

Thanks a lot for your answers!