Skip to main content

AWS ElastiCache for Redis

Connecting to Amazon's ElastiCache service is very similar to the public Redis example.

In order for the Redis KV capability provider to maintain connections during scaling/failover events it needs to be configured with the write endpoint that ElastiCache provides for the whole cluster.

We'll create an ElastiCache cluster and EC2 instance, verify connectivity, and use the resulting write endpoint as our Redis KV configuration value.

AWS ElastiCache+EC2 setup


The AWS CLI and jq need to be installed. Also, this example uses the default VPC/subnets here by not specifying any. If your AWS account is setup differently, you will need to provide more fields for the AWS CLI commands. Creating the ElastiCache cluster and EC2 Instance via the AWS console is also an option.

#!/usr/bin/env bash

# jq will fail if our output isn't json

# here we set some variables so we can easily change and reuse them

# create a replication group (this will automatically create an elasticache cluster)
aws elasticache create-replication-group \
  --cache-node-type $NODETYPE \
  --replication-group-id $CACHENAME \
  --replication-group-description "cosmonic Redis KV provider example" \
  --num-cache-clusters $CACHECOUNT \
  --engine redis

# more variables for convenience
EC2AMI=${EC2AMI:-ami-06a7641d5bd7bdc65} # debian 11

# create a keypair or use an existing one
aws ec2 create-key-pair --key-name $KEYNAME | jq .KeyMaterial > $KEYNAME.rsa

# create an ec2 instance with ssh configured
aws ec2 run-instances \
    --instance-type $EC2NODETYPE \
    --image-id $EC2AMI \
    --key-name $KEYNAME \
    --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$KEYNAME}]"
    --count 1

# get the primary endpoint for elasticache
# we will refer to this as ELASTICACHE_ENDPOINT below
ELASTICACHE_ENDPOINT=$(aws elasticache describe-replication-groups | jq -r '.ReplicationGroups[] | select(.ReplicationGroupId=="patrick") | .NodeGroups[] | .PrimaryEndpoint.Address')

Connecting to Cosmonic

Now let's hop onto the instance to check ElastiCache connectivity, install cosmo, and start our super constellation node.

# start a session on your ec2 instance
# your instance must be accessible from your IP
# you might need to edit security groups in your aws account
PUBLICIP=$(aws ec2 describe-instances | jq -r '.Reservations[] | select(.Instances[0].Tags[0].Value=="cosmonic-example") | .Instances[0].PublicIpAddress')
ssh -i $KEYNAME.rsa admin@$PUBLICIP

# on your instance:
apt-get update && apt-get install -y redis-tools

# make sure you can connect to your elasticache cluster (you should get PONG back)

# install cosmo
curl -Ss | bash
export PATH=$PATH:~/.cosmo/bin

# this will give you a link to open in your browser to adopt the node
cosmo login

# start your super constellation node
cosmo up

The rest is the same as the External Redis example and we can follow from starting the provider.

Make sure to use the value we defined for ELASTICACHE_ENDPOINT in the Link Definition section