Terraform: Terraform still trying to resolve interpolation in resource with count of zero

Created on 16 May 2017  ยท  5Comments  ยท  Source: hashicorp/terraform

I am trying to create PTR records for a server deployment. The server(s) below need to be deployed after a dependent set of servers are applied so we currently do this by running one apply deploying those server modules then a second deploy where we change these resources counts from 0 to however many we're looking to deploy. I've added a new resource to create a PTR record for these servers and even with the count set to 0 Terraform attempts to resolve the interpolation. It doesn't do this for the A record resource just the PTR record resouce.

Here is the code, I even hard coded the count to 0 to see if there was an issue with the variable. The list is expected to be empty while the count is at 0. I expect that Terraform wouldn't try to resolve the interpolation.

resource "aws_route53_record" "ds_sync_A_records" {
// same number of records as instances
provider = "aws.dns"
count = 0
// count = "${var.ping_sync_cluster_count}"
zone_id = "${data.aws_route53_zone.zone_company_io.zone_id}"
name = "ping-sync-0${count.index}.${var.domain_name}"
type = "A"
ttl = "10"
// matches up record N to instance N
records = ["${element(module.ping_sync_hot_server.private_server_ips, 
count.index)}"]
}

resource "aws_route53_record" "ds_sync_PTR_records" {
// same number of records as instances
provider = "aws.dns"
count = 0
// count = "${var.ping_sync_cluster_count}"
zone_id = "${data.aws_route53_zone.zone_company_io.zone_id}"
name = "${format("%s.%s.%s.$s.in-appr.arpa", element(split(".", 
element(module.ping_sync_hot_server.private_server_ips, count.index)), 3), 
element(split(".", element(module.ping_sync_hot_server.private_server_ips, 
count.index)), 2), element(split(".", 
element(module.ping_sync_hot_server.private_server_ips, count.index)), 1), 
element(split(".", element(module.ping_sync_hot_server.private_server_ips, 
count.index)), 0) )}"
type = "PTR"
ttl = "10"
// matches up record N to instance N
records = ["${element(module.ping_sync_hot_server.private_server_ips, 
count.index)}"]

Error message on apply:

Error running plan: 3 error(s) occurred:

* element: element() may not be used with an empty list in:

${element(module.ping_sync_hot_server.private_server_ips, count.index)}
* element: element() may not be used with an empty list in:

${element(module.ping_sync_hot_server.private_server_ips, count.index)}
* element: element() may not be used with an empty list in:

${element(module.ping_sync_hot_server.private_server_ips, count.index)}
bug core

Most helpful comment

What was the solution?

All 5 comments

What was the solution?

I also have this issue.
Very inconvenient

My solution was to create separate modules and doesn't call module if list with configuration has 0 length.

This is a bug added to core and is closed with any fix or reference to a PR or other issue. Can we have some details on this?

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings