Home > Software engineering >  How can I get cache_nodes address from ElastiCache in Terraform?
How can I get cache_nodes address from ElastiCache in Terraform?

Time:07-01

In Terraform, I am creating a single ElastiCache cluster:

resource "aws_elasticache_cluster" "api" {
  cluster_id           = var.cluster_id
  engine               = var.engine
  node_type            = var.node_type
  num_cache_nodes      = 1
  parameter_group_name = "default.redis3.2"
  engine_version       = "3.2.4"
  port                 = 6379
  subnet_group_name    = var.subnet_group_name
}

I am wondering how I can access the address attribute exported by cache_nodes

Is either of the following sufficient?

output "redis_host1" {
  value = aws_elasticache_cluster.api.cache_nodes["address"]
}

output "redis_host2" {
  value = aws_elasticache_cluster.api.cache_nodes.address
}

Just looking for some clarification, please.

CodePudding user response:

cache_nodes is a list. So you have to specify index of the node first, e.g.:

output "redis_host1" {
  value = aws_elasticache_cluster.api.cache_nodes[0].address
}

output "redis_host2" {
  value = aws_elasticache_cluster.api.cache_nodes[1].address
}

or just return addresses as list:

output "redis_hosts" {
  value = aws_elasticache_cluster.api.cache_nodes[*].address
}
  • Related