TF Ver11 to 13 upgrade - Syntax Error

Seeking some support on Terraform script upgrade. ver 11 → 13

Existing Block: assigning value to target_id issue.

resource “aws_lb_target_group_attachment” “wordpress_target_attachments” {
target_group_arn = “${aws_lb_target_group.wordpress.arn}”

target_id = “${element(module.ec2-wp.instance_id, count.index)}”
port = 80
count = 1
}

Error:

on main.tf line 223, in resource “aws_lb_target_group_attachment” “wordpress_target_attachments”:
223: target_id = “${element(module.ec2-wp.instance_id, count.index)}”
|----------------
| count.index is 0
| module.ec2-wp.instance_id is tuple with 1 element

Inappropriate value for attribute “target_id”: string required.

I believe this is the ID it’s trying to extract:

module.ec2-wp.aws_instance.ec2[0]: Refreshing state… [id=i-08287c418b529135a]

I’ve tried several variation without success.

These versions of Terraform don’t exist, so I guess you actually mean 0.11 and 0.13.

Upgrading from 0.11 to 0.13 is not supported.

It is mandatory to upgrade from 0.11 to 0.12, and only after a successful apply with 0.12, proceed to 0.13 - and then only after a successful apply with 0.13, proceed to 1.x.

The upgrades between 0.x versions are a big deal and it is very necessary to read and follow the version-specific upgrade documentation.

This upgrade documentation has actually now gotten quite hard to find, as 0.x versions are so obsolete these days, so it is no longer visible in the latest version of the documentation.

You can find the point where you need to start reading at Upgrading to Terraform 0.12 | Terraform | HashiCorp Developer

Good luck, and please make it a priority to upgrade to modern Terraform - knowledge of upgrade procedures for such old versions is rapidly fading away from most people’s memories - your options for getting help will reduce over time.

As @maxb notes, it’s important to upgrade these older versions one by one. This is particularly true for v0.12 and v0.13 because they both included some significant changes to Terraform’s designe.

In addition to the note about it getting harder to find help over time, another emerging hazard is that the upgrade instructions were written for the world that existed at the time of release, but like most software Terraform depends on various other external systems, such as Terraform providers and third-party network services, and those continue to evolve over time in ways that are adding extra complications. For example, by now there are some providers whose latest releases are not compatible with Terraform v0.12 or v0.13, so the advice in the documentation of just updating everything to latest is not sufficient. (terraform init is typically capable of detecting this and describing the problem, though.)

In this particular case the symptoms are familiar to me so I have a guess about what’s happening… I bet this module’s output "instance_id" block is defined something like this:

output "instance_id" {
  value = ["${aws_instance.example.*.id}"]
}

The above technically describes a list of lists rather than just a list, but Terraform v0.11 included a backward-compatibility concession to older versions that didn’t handle this properly which was removed in Terraform v0.12, as described in Referring to List Variables.

The automatic upgrade process described in the documentation should hopefully be able to fix this situation automatically, but note that it’s the child module that’s causing the problem here despite the fact that the error is being reported in the caller, and so you’ll need to complete the upgrade process for the module that the module "ec2-wp" block is referring to before this will behave correctly.

I would expect the upgrade tool to rewrite the above example to be like this instead:

output "instance_id" {
  value = aws_instance.example.*.id
}

However, I don’t recommend making that change manually instead of using the upgrade tool, because once your module includes any amount of syntax that Terraform v0.11 would consider invalid the upgrade tool (which uses Terraform v0.11’s configuration parser) will no longer be able to parse it. Better to start by running the upgrade tool as described in the documentation, and then see what other manual changes you might want or need to make based on its result.