0

In AWS, I have an elasticache cluster with one redis node inside and a ECS cluster. I cannot figure out what whould be the better way to pass the redis endpoint url to my docker container.

Currently, in my task definition, I use an environment param named REDIS_ENDPOINT with my value. It worked well before.

But I'm converting our deployment process from all manual operations (with AWS UI) to Terraform. And now, the REDIS_ENDPOINT environment variable doesn't work so well anymore because the redis node will be created by terraform (dns will change potentially) and the env variable (part of the container definition) must be inside a static json file.

I read some documentation but I cannot find the proper way of doing that. Or may be I'm lost in AWS documentation.

NinjaFisherman
  • 296
  • 4
  • 15

2 Answers2

0

When you create an Elasticache cluster using Terraform you can access some output variables which includes the addresses of the nodes created.

So you have two options here: you could create a DNS record that CNAMEs to the outputted address which your application can use statically or you could use the outputted address in a templated file that your application can access.

I'd typically go with the first option so you might have something like this:

resource "aws_elasticache_cluster" "redis" {
    cluster_id = "redis-cluster"
    engine = "redis"
    node_type = "cache.t2.micro"
    port = 6379
    num_cache_nodes = 1
    parameter_group_name = "default.redis2.8"
}

resource "aws_route53_record" "redis" {
   zone_id = "${var.dns_zone_id}"
   name = "redis.example.com"
   type = "CNAME"
   ttl = "60"
   records = ["${aws_elasticache_cluster.redis.address}"]
}

Your application can then just use redis.example.com:6379 as the end point to use and this will always work even when you rebuild your Elasticache cluster.

ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
  • ╷ │ Error: Unsupported attribute │ │ on resources.tf line 24, in output "aws_elasticache_cluster_redis_address": │ 24: value = aws_elasticache_cluster.redis.address │ │ This object has no argument, nested block, or exported attribute named "address". ╵ – Elchin Valiyev Nov 23 '21 at 19:56
0

You can use an output {} variable, then in your ECS you can use remote state to retrieve it.

resource "aws_elasticache_cluster" "redis" {
    cluster_id = "redis-cluster"
    engine = "redis"
    node_type = "cache.t2.micro"
    port = 6379
    num_cache_nodes = 1
    parameter_group_name = "default.redis2.8"
}


output "redis_address" {
  value = "${aws_elasticache_cluster.redis.address}"
}

Then in your tf for ecs (assuming s3 remote state):

data "terraform_remote_state" "redis" {
    backend = "s3"
    config {
        bucket = "com-my-bucket"
        key = "redis/terraform.tfstate"
        region = "us-east-1"
    }
}

# address = "${data.terraform_remote_state.redis.redis_address}"
Marc Young
  • 3,854
  • 3
  • 18
  • 22