Correct way to reference dns_a records in 0.12

I have a module that im getting 3 IP address’s from :

resource dns_a_record_set "company_db" {

addresses = ["${module.company_db.instances_ips}"]

name = "company-db"

ttl = 300

zone = "${var.domain}"

}

Ive tried the straight call : module.company_db.instances_ips . This fails with module.company_db.instances_ips is tuple with 1 element

Inappropriate value for attribute "addresses" : element 0: string required.

Then trying with addresses : flatten([ “module.company_db.instances_ips” ]) works but it wants to remove all 3 IP’s and insert only ‘module.company_db.instances_ips’

How can I do this, preferably keeping the code functional with TF 0.11?

Assuming Terraform 0.12 or later and that the instances_ips output is a list(string) you would just use addresses = module.company_db.instances_ips. The use of interpolation (using ${} within a quoted string) is deprecated from 0.12 onwards unless needed due to string concatenation.

You mentioned keeping this compatible with Terraform v0.11. That’s not always possible because v0.12 fixed a number of bugs in v0.11 which, prior to v0.12, required odd workarounds.

This is possibly one of those situations: setting an argument that expects a list to be a list of lists instead was a somewhat-common workaround for a bug in the provider SDK where it would fail if given a list whose length or contents aren’t known until apply time, incorrectly reporting “a list is required”. Making it a list of lists ended up being an accidental workaround for that by relying on an old backward-compatibility behavior introduced in Terraform v0.7, to support configurations that predated Terraform having a first-class list type, but we removed that legacy behavior in Terraform v0.12 along with adding a workaround for the aforementioned SDK bug the workaround was for.

With all of that said, you could try writing the following to perhaps get a cross-compatible module:

addresses = "${module.company_db.instances_ips}"

More modern versions of Terraform will produce a warning with the above to point out that it’s deprecated syntax, but the current version of Terraform as I write this (v0.14) will still accept this as valid in spite of the warning.

The trick will be whether Terraform v0.11 (or, more accurately, the Terraform SDK without the workaround that Terraform v0.12 introduced) can accept it. This will depend on whether this instances_ips value is known at plan time or not. Given the name of it I’m betting it’s a list of IP addresses decided by the remote system, in which case it probably wouldn’t be known at plan time unless those instances happen to already be created. If they aren’t already present, the DNS provider might incorrectly report “a list is required” for that addresses argument.

If you only need v0.11 compatibility briefly during a transition, the above might be acceptable with an additional workaround: as long as the instances whose IP addresses you are using already exist when you run terraform plan this will likely work without any special tricks. But if you do end up needing to replace any of those instances or create new ones while you’re still on v0.11 you may see the error and need to work around it by running Terraform once with the -target option to tell Terraform to create the instances first, without also trying to create the record sets:

terraform apply -target=module.company_db

I hope this helps! The v0.11 knowledge is getting harder to recall now because it’s been some years so I might’ve got some details wrong here but hopefully it’s close enough to get something working. If you run into trouble then feel free to ask follow-up questions; I can’t promise I’ll be able to answer them promptly myself, but someone else in the forum might know the answer.

I think the strong recommendation would be to move away from 0.11 as soon as you can. 0.12 was released nearly two years ago now and since then many community modules and providers have been updated to require more recent versions - you might easily find that features you need are only available with a newer provider version which no longer works with Terraform 0.11.

1 Like

Unfortuantely, “${module.company_db.instances_ips}” gives the same tuple error.

`output "instances_ips" {`

`value = ["${local.instance_ips}"]`

`}`

And that leads to

instance_ips = "${aws_instance.instance.*.private_ip}"

So you’d need to use value = module.company_db.instances_ips which is Terraform 0.12+ only.