Search code examples
amazon-web-servicesaws-cliamazon-elasticache

Fetch AWS ReplicationGroup DNS using aws-cli


We use cloudformation to deploy our service instances in aws. Part of our stack is currently an elasticache redis cluster, but we're trying to transition to a redis ReplicationGroup for a little extra redundancy.

We use ansible playbooks to automate the deployment of our service, including fetching things like Redis dns entries by fetching via aws-cli.

The problem is, aws-cli has the ability to look up a ReplicationGroup by id (aws-cli docs) but CloudFormation doesn't have the ability to actually set the id, instead the id is set to a random unique value:

Elasticache replication group id in CloudFormation template

So long story short, is there a single command I could use to query for my ReplicationGroup to get its primary DNS record?


Solution

  • After much digging and discussion with our devops team I found a way to do this that'll work with a single aws-cli command.

    First, add an Outputs block to your redis configuration, on the same level as (outside of) the "Resources" block:

    "Outputs" : {
            "RedisReplicationGroupDnsName" : {
                "Description" : "DNS entry for the redis replication group",
                "Value" : { "Fn::GetAtt" : [ "<your replication group's json key>", "PrimaryEndPoint.Address" ] }
            }
        },
    

    Next, you'll need to set up an IAM policy that allows your instance to describe its own stack.

    Finally, using aws-cli you can get the primary dns endpoint for the replication group with this command:

    aws cloudformation describe-stacks --region {{ ansible_ec2_placement_region }} --stack-name <your stack name> --query 'Stacks[0].Outputs[?OutputKey==`RedisReplicationGroupDnsName`]|[0].OutputValue' --output text