In your Amazon Web Services (AWS) console, go to the ElasticCache page https://console.aws.amazon.com/elasticache and select Get Started > Redis. At a high level, there are two types of Redis you can create.
- serverless
- dedicated instances (this article)
When I was learning this for the first time, I went with Design your own cache and Easy create and Demo.
I gave my cluster a name, created a new subnet, and selected Create.
I may take a minute or two for the Redis Cluster to be ready. Once ready, you should have an endpoint such as my-redis-cluster.abc123.clustercfg.use1.cache.amazonaws.com:6379.
Since the endpoint has port 6379, on the system you'll be connecting to Redis from, you'll have to allow connections on port 6379. For example, if connecting from an EC2 instance, the aws ec2 authorize-security-group-ingress command can be used to allow inbound connections on port 6379 from the Security Group being used by the EC2 instance.
aws ec2 authorize-security-group-ingress --group-id sg-abdefg123456789ab --protocol tcp --port 6379 --cidr 0.0.0.0/0
From your EC2 instance, the openssl s_client connect command can be used to determine if you are able to connect to your Redis cluster on port 6379.
~]$ openssl s_client -connect my-redis-cluster.abc123.clustercfg.use1.cache.amazonaws.com:6379
CONNECTED(00000003)
The redis-cli can be used to see if you can connect to Redis. If you don't have the Redis CLI installed on your EC2 instance, check out my article Install redis-cli from source. If the connection is successful, you should get an interactive redis command prompt. I used the cluster info command and immediately got a response. All good!
~]$ redis-stable/src/redis-cli -c -h my-redis-cluster.abc123.clustercfg.use1.cache.amazonaws.com -p 6379
my-redis-cluster.abc123.clustercfg.use1.cache.amazonaws.com:6379>
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
total_cluster_links_buffer_limit_exceeded:0
my-redis-cluster.abc123.clustercfg.use1.cache.amazonaws.com:6379> quit
The following Python could then be used to test the connection to your Redis cluster. This is just the boilerplate code with no error handling.
#!/usr/bin/python3
import redis
client = redis.Redis.from_url('redis://your_redis_endpoint:6379')
client.ping()
And here is an example with error handling.
#!/usr/bin/python3
import redis
try:
client = redis.Redis.from_url("redis://my-redis-cluster.abc123.clustercfg.use1.cache.amazonaws.com:6379")
except Exception as exception:
print(f"got the following exception when trying redis.Redis.from_url - {exception}")
else:
print(f"redis.Redis.from_url appears to have been successful")
print(f"client = {client}")
try:
client.ping()
except Exception as exception:
print(f"got the following exception when trying client.ping() - {exception}")
else:
print(f"client.ping() appears to have been successful")
Did you find this article helpful?
If so, consider buying me a coffee over at