How to set up Redis on AWS EC2?

Easy way to start developing with Redis.

How to set up Redis on AWS EC2?

Recently for one of the projects I needed to set up Redis, I thought of ElastiCache but I required it for the development purpose and didn't want to spend much on it so I decided to spin up a Redis on AWS EC2 instance.

Today I had like to share a quick and simple way to set up Redis on a AWS EC2 instance, how to add a password to Redis, and also how you can connect to it. Although this is a secure setup still I don't recommend it for production use, for Production use I will share the details in a later blog post.

Before starting this blog I assume that you have a basic knowledge of AWS and are able to spin up a EC2 instance on your own and also able to connect it through SSH.

Launch EC2 instance.

We are going to use Amazon Linux 2 AMI for this tutorial but you can also use Ubuntu.

If you want to know how to launch AWS EC2 instance and connect to it through SSH please watch this video.

Make sure that required ports are open and accessible through the internet.

Download and Install Redis.

Run the following commands step-by-step to download and install Redis on the EC2 instance.

Connect to your instance through SSH

ssh -i /path/key-pair-name.pem instance-user-name@instance-public-dns-name

Install dependencies

sudo yum -y update
sudo yum groupinstall "Development Tools"

Install Redis

cd /usr/local/src 
sudo wget http://download.redis.io/redis-stable.tar.gz
sudo tar xvzf redis-stable.tar.gz
sudo rm -f redis-stable.tar.gz

Recompile Redis

Now we have downloaded and extracted the Redis, we need to recompile it.

cd redis-stable
sudo make distclean
sudo make

Run Tests

This is not required but recommended to check that Redis is correctly installed. It will take 10 mins to run all the tests.

sudo yum install -y tcl
sudo make test

Make directories and copy files into proper places

sudo mkdir -p /etc/redis /var/lib/redis /var/redis/6379
sudo cp src/redis-server src/redis-cli /usr/local/bin
sudo cp redis.conf /etc/redis/6379.conf

Edit Config file

sudo nano /etc/redis/6379.conf

Set values as below in config file

  • Set bind to 127.0.0.1
  • daemonize to yes
  • logfile to /var/log/redis_6379.log
  • dir to /var/redis/6379

Download and install init script

sudo wget https://raw.githubusercontent.com/saxenap/install-redis-amazon-linux-centos/master/redis-server
sudo mv redis-server /etc/init.d
sudo chmod 755 /etc/init.d/redis-server

Edit the Redis init script as below

sudo nano /etc/init.d/redis-server
  • REDIS_CONF_FILE="/etc/redis/6379.conf"

Finally, add new Redis init script to all the default runlevels for CentOS and start the Redis server.

sudo chkconfig –add redis-server
sudo chkconfig –level 345 redis-server on
sudo service redis-server start

Also to ensure background saves and fix low-memory issues you need to create a new file systctl.conf in /etc.

sudo nano /etc/systctl.conf

add the below lines to the file and save it.

vm.overcommit_memory = 1
systctl vm.overcommit_memory=1

Congratulations! You are done, make sure to test the server by pinging your instance with redis-cli. It should give you a response PONG.

Set password for Redis Server

We need to set the password using redis-cli. From Redis 6 ACL is introduced so we can also set username and also define access for users but we will talk about ACL in detail in another article. Let's see how we can set the password for the default user.

1) Connect to the Redis command-line interface:

redis-cli

2) Set password

Change mypassword to your choice of password.

127.0.0.1:6379> CONFIG SET requirepass "mypassword"

3) Check if the password is set.

127.0.0.1:6379> AUTH "mypassword"

If Password Matches then Server Response will be

OK

Else if Incorrect

(error) ERR invalid password

And you are done with the Redis setup on AWS EC2 instance.

Connect to the Redis server using NodeJS.

As I already told you that the default PORT of the Redis server is 6379 so your connection URL becomes "redis://your_instance_public_ip_address:6379".

so by using redis client you can connect to the Redis server in following way.

const express = require("express");
const redis = require("redis");

const app = express();

const url = "redis://your_instance_public_ip_address:6379";

const redisClient = redis.createClient({
  url,
  password: "mypassword",
});

const connectRedis = async () => {
  await redisClient.connect();

  await redisClient.set("foo", "bar");
  let data = await redisClient.get("foo");
  console.log("DATA ", data);

  app.listen(5000, () => {
    console.log("Server is running on port 5000");
  });
};

connectRedis();

References: