Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use cache_nodes output of aws_elasticache_cluster as a list #826

Closed
hashibot opened this issue Jun 13, 2017 · 14 comments
Closed

Can't use cache_nodes output of aws_elasticache_cluster as a list #826

hashibot opened this issue Jun 13, 2017 · 14 comments
Labels
bug Addresses a defect in current functionality. service/elasticache Issues and PRs that pertain to the elasticache service.

Comments

@hashibot
Copy link

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

@hashibot hashibot added the bug Addresses a defect in current functionality. label Jun 13, 2017
@Mattias-
Copy link

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

@crielly
Copy link

crielly commented Jun 27, 2017

Also running into this on 0.9.8, exact same use case 😭

@imauriciovargas
Copy link

imauriciovargas commented Aug 2, 2017

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

@ccgagnon
Copy link

ccgagnon commented Oct 3, 2017

I got the same issue with 0.10.6.

@ssmereka
Copy link

ssmereka commented Nov 2, 2017

Same issue in 0.10.8

@nshenry03
Copy link

nshenry03 commented Nov 28, 2017

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

@bflad bflad added the service/elasticache Issues and PRs that pertain to the elasticache service. label Jan 19, 2018
@dermyrddin
Copy link

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

@ajbrown
Copy link

ajbrown commented Aug 9, 2018

Any workarounds for this?

@imauriciovargas
Copy link

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

@jdavisyei
Copy link

jdavisyei commented Oct 22, 2018

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.

@TechDawg270
Copy link

TechDawg270 commented Jan 10, 2019

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

@bflad
Copy link
Contributor

bflad commented Jan 10, 2019

The issue found by @TechDawg270 appears to be similar to hashicorp/terraform#18129 which is fixed in Terraform 0.12. 👍

@bflad
Copy link
Contributor

bflad commented Jul 24, 2019

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! 🚀

@bflad bflad closed this as completed Jul 24, 2019
@ghost
Copy link

ghost commented Nov 2, 2019

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!

@ghost ghost locked and limited conversation to collaborators Nov 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/elasticache Issues and PRs that pertain to the elasticache service.
Projects
None yet
Development

No branches or pull requests