Terraform-provider-aws: Can't use cache_nodes output of aws_elasticache_cluster as a list

Created on 13 Jun 2017  ·  14Comments  ·  Source: hashicorp/terraform-provider-aws

_This issue was originally opened by @phoolish as hashicorp/terraform#15040. It was migrated here as part of the provider split. The original body of the issue is below._


This seems to be the return of hashicorp/terraform#8794

Terraform Version

0.9.6
0.9.2

Affected Resource(s)

Please list the resources as a list, for example:

  • aws_elasticache_cluster

Terraform Configuration Files

resource "aws_elasticache_cluster" "memcached" {
  cluster_id = "foo"
  engine = "memcached"

  num_cache_nodes = 3
  node_type = "cache.t2.micro"
}

resource "route_53_record" "memcached-site-com" {
  count = "${length(aws_elasticache_cluster.memcached.cache_nodes)}"
  zone_id = "${aws_route53_zone.site-com.zone_id}"
  name = "${format("node%d.mem.site.com", count.index)}"
  type = "CNAME"
  records = [
    "${element(aws_elasticache_cluster.memcached.cache_nodes.*.address, count.index)}",
  ]
  ttl = "300"
}

Debug Output

Panic Output

Expected Behavior

3 route53_records should be generated.

Actual Behavior

Error refreshing state: 1 error(s) occurred:

* aws_route53_record.mem-site-com: 3 error(s) occurred:

* aws_route53_record.mem-site-com[0]: At column 3, line 1: element: argument 1 should be type list, got type string in:

${element(aws_elasticache_cluster.mem-site-com.cache_nodes.*.address, count.index)}
* aws_route53_record.mem-site-com[1]: At column 3, line 1: element: argument 1 should be type list, got type string in:

${element(aws_elasticache_cluster.mem-site-com.cache_nodes.*.address, count.index)}

${element(aws_elasticache_cluster.mem-site-com.cache_nodes.*.address, 0)}
* aws_route53_record.mem-site-com[2]: At column 3, line 1: element: argument 1 should be type list, got type string in:

${element(aws_elasticache_cluster.mem-site-com.cache_nodes.*.address, count.index)}

Steps to Reproduce

  1. terraform apply

Important Factoids

References

  • GH-10657
bug servicelasticache terraform-0.12

Most helpful comment

Same problem with. Trying to register cluster nodes in consul catalog:
address = "${element(aws_elasticache_cluster.memcached.cache_nodes.*.address, count.index)}"

All 14 comments

I'm getting this as well with 0.9.8. Would be nice with a fix.

Also running into this on 0.9.8, exact same use case :sob:

Running into the same issue creating CloudWatch alarms with version 0.9.11.

variable "num_nodes" {
  defaul  = 2
}

resource "aws_elasticache_cluster" "memcached" {
  cluster_id = "foo"
  engine = "memcached"
  num_cache_nodes = "${var.num_nodes}"
  ...
}

resource "aws_cloudwatch_metric_alarm" "CPU" {
  count                     = "${var.num_nodes}"
  ...
  dimensions                {
      CachedClusterId = "${aws_elasticache_cluster.memcached.id}",
      CachedNodeId    = "${element(aws_elasticache_cluster.memcached.cache_nodes.*.id, count.index)}"
  }
}

I get the following errors applying it all together:
(fix for the errors below is to add a depends_on to the alarm resource)

Error applying plan:

6 error(s) occurred:

* module.memcached.aws_cloudwatch_metric_alarm.evictions[0]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_
nodes.*.id'
* module.memcached.aws_cloudwatch_metric_alarm.evictions[1]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_
nodes.*.id'
* module.memcached.aws_cloudwatch_metric_alarm.CPU[1]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes.
*.id'
* module.memcached.aws_cloudwatch_metric_alarm.swap[0]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes
.*.id'
* module.memcached.aws_cloudwatch_metric_alarm.CPU[0]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes.
*.id'
* module.memcached.aws_cloudwatch_metric_alarm.swap[1]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes
.*.id'

When I re-run terraform apply I get this error:


Error applying plan:

6 error(s) occurred:

* module.memcached.aws_cloudwatch_metric_alarm.evictions[0]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_
nodes.*.id'
* module.memcached.aws_cloudwatch_metric_alarm.evictions[1]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_
nodes.*.id'
* module.memcached.aws_cloudwatch_metric_alarm.CPU[1]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes.
*.id'
* module.memcached.aws_cloudwatch_metric_alarm.swap[0]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes
.*.id'
* module.memcached.aws_cloudwatch_metric_alarm.CPU[0]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes.
*.id'
* module.memcached.aws_cloudwatch_metric_alarm.swap[1]: Resource 'aws_elasticache_cluster.memcached' does not have attribute 'cache_nodes.*.id' for variable 'aws_elasticache_cluster.memcached.cache_nodes
.*.id'

Update:
After more digging, I found my issue to be related to this bug

I got the same issue with 0.10.6.

Same issue in 0.10.8

output "prod-or00-elasticache-nodes" {
  value = "${join(",", aws_elasticache_cluster.prod-or00.cache_nodes.*.address)}"
}

Same error / problem...

Error: Error refreshing state: 1 error(s) occurred:

* output.prod-or00-elasticache-nodes: At column 3, line 1: join: argument 1 should be type list, got type string in:

${join(",", aws_elasticache_cluster.prod-or00.cache_nodes.*.address)}
nhenry@nhenry-desktop:~/Repositories/Terraform/prod-or00$ terraform -v
Terraform v0.11.0
+ provider.aws v1.1.0

Same problem with. Trying to register cluster nodes in consul catalog:
address = "${element(aws_elasticache_cluster.memcached.cache_nodes.*.address, count.index)}"

Any workarounds for this?

Yes. I found something today looking at a reference in the bug.

use lookup

variable "num_nodes" {
  defaul  = 2
}

resource "aws_elasticache_cluster" "memcached" {
  cluster_id = "foo"
  engine = "memcached"
  num_cache_nodes = "${var.num_nodes}"
  ...
}

resource "aws_cloudwatch_metric_alarm" "CPU" {
  count                     = "${var.num_nodes}"
  ...
  dimensions                {
      CachedClusterId = "${aws_elasticache_cluster.memcached.id}",
      CachedNodeId    = "${lookup(aws_elasticache_cluster.memcached.cache_nodes[count.index], "id")}"
  }
}

Tested in Terraform version 10.6

Is there a workaround that will let me dump the entire list of addresses into a comma separated list.

I tried using various forms of cache_nodes.*.address but passing that to join() returns a count of the number of nodes instead of the address as I would expect. Using count.index unfortunately doesn't help because I need all of them in one object.

I was experiencing this same issue when trying to create an elasticache cluster using this cloudposse wrapper module and Terraform v0.11.11. The result was a suppressed error message that results in:

* module.this.aws_route53_record.this_elasticache: Resource 'aws_elasticache_cluster.this' not found for variable 'aws_elasticache_cluster.this.cache_nodes.0.address'

Once I removed usage of that wrapper module it uncovered the actual error message and root cause which was the cluster I was attempting to create was >20 characters in length. Error message once I ran plan without the wrapper module:

Error: module.livequery.aws_elasticache_cluster.this: "cluster_id" ("this-cluster-name-is-too-damn-long") must contain from 1 to 20 alphanumeric characters or hyphens

I do not believe this is a bug with the aws provider and aws_elasticache_cluster resource and should be closed and referenced by hashicorp/terraform#18129

The issue found by @TechDawg270 appears to be similar to https://github.com/hashicorp/terraform/issues/18129 which is fixed in Terraform 0.12. 👍

Hi folks 👋 This issue is resolved in Terraform 0.12, which fully supports indexed splat (*) notation with list attribute values.

Given this configuration:

terraform {
  required_providers {
    aws = "2.20.0"
  }
  required_version = "0.12.5"
}

provider "aws" {
  region = "us-east-2"
}

resource "aws_elasticache_cluster" "test" {
  cluster_id      = "bflad-testing"
  engine          = "memcached"
  node_type       = "cache.t2.micro"
  num_cache_nodes = 3
}

output "test1" {
  value = aws_elasticache_cluster.test.cache_nodes[*].address
}

output "test2" {
  value = element(aws_elasticache_cluster.test.cache_nodes[*].address, 1)
}

output "test3" {
  value = join(",", formatlist("%s:%s", aws_elasticache_cluster.test.cache_nodes[*].address, aws_elasticache_cluster.test.cache_nodes[*].port))
}

We can get the various expected outputs that were problematic in Terraform 0.11 and earlier:

Outputs:

test1 = [
  "bflad-testing.asxu6y.0001.use2.cache.amazonaws.com",
  "bflad-testing.asxu6y.0002.use2.cache.amazonaws.com",
  "bflad-testing.asxu6y.0003.use2.cache.amazonaws.com",
]
test2 = bflad-testing.asxu6y.0002.use2.cache.amazonaws.com
test3 = bflad-testing.asxu6y.0001.use2.cache.amazonaws.com:11211,bflad-testing.asxu6y.0002.use2.cache.amazonaws.com:11211,bflad-testing.asxu6y.0003.use2.cache.amazonaws.com:11211

Enjoy! 🚀

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings