Output is different between v0.11 and v0.12, and v0.12 will report error if the resource was created with v0.11

I have created the resource with v0.11.14

main.tf

resource "aws_instance" "this" {
  count = "${var.count}"
  .......
}

output.tf

output "subnet_id" {
  description = "List of IDs of VPC subnets of instances"
  value       = ["${aws_instance.this.*.subnet_id}"]
}

when I upgrade v0.12.6, and output.tf is the following content, it will report error.

main.tf

resource "aws_instance" "this" {
  for_each = {
    1: ""
  }
  .......
}

output.tf

output "subnet_id" {
  description = "List of IDs of VPC subnets of instances"
  value = {
    for instance in aws_instance.this:
    instance.id => instance.subnet_id
  }
}

result:

Error: Attempt to get attribute from null value

  on ../../modules/aws-ec2-instance/outputs.tf line 99, in output "subnet_id":
  99:     instance.id => instance.subnet_id

This value is null, so it does not have any attributes.


Error: Missing map element

  on ../../modules/aws-ec2-instance/outputs.tf line 99, in output "subnet_id":
  99:     instance.id => instance.subnet_id

This map does not have an element with the key "id".


Error: Unsupported attribute

  on ../../modules/aws-ec2-instance/outputs.tf line 99, in output "subnet_id":
  99:     instance.id => instance.subnet_id

This value does not have any attributes.

But if for_each is { 1: "", 2: "" } (for_each length > 1), it is normal !

Why ? How to solve? This is a bug ?

Looking forward to your reply !

Thanks.

Hi @lyc0221,

I think you’ve found the same bug described in GitHub issue #22407, so I added a comment over there linking to your question. Thanks!

In the mean time, if you are continuing to use numbers as your keys anyway, there isn’t any advantage to using for_each over count, and so I’d suggest to keep using count until this issue is fixed.

Hi @apparentlymart ,

Because I don’t want to switch to for_each when count=2, I’ll start with for_each.

Ok, I’ll keep using count until this issue is fixed.

Thanks for you reply!