Spin Up Redis with AWS EC2
By: Calvin Hsieh
Tags: Development, AWS, DevOps, Redis, Elasticache

Ever need to setup Redis yourself? Want to save cost by hosting Redis yourself instead of using ElastiCache? This post demonstrates how to setup Redis as daemon on AWS EC2 instance!

Before we get started, you should have basic knowledge on vim and launching EC2 instance via AWS console.

Launch EC2 Instance

Setup EC2 Instance

Pick Amazon Linux 2 AMI Pick desired instance type Configure instance with proper VPC, subnet, etc Finish up the rest of the steps to launch the instance Make sure you have SSH port 22 open to your ip in security group

Setup Redis

Connect to the instance that just created

ssh -i path_to_keypair ec2-user@ip_address

Install dependencies

sudo yum -y update

sudo yum -y install make gcc gcc-c++ kernel-devel tcl

Install Redis

wget http://download.redis.io/redis-stable.tar.gz

tar xvzf redis-stable.tar.gz

cd redis-stable

make

Test the build is working properly

make test

Install the binary to proper location

sudo make install

Run Redis as daemon

Create directory to store Redis config

sudo mkdir /etc/redis

sudo mkdir /var/redis

Copy the init script

sudo cp utils/redis_init_script /etc/init.d/redis_6379

Edit the init script:

Make sure to modify REDISPORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.

sudo vi /etc/init.d/redis_6379

Copy the template configuration file

sudo cp redis.conf /etc/redis/6379.conf

Create a directory for data and working directory

sudo mkdir /var/redis/6379

Edit the configuration file, making sure to perform the following changes:

sudo vi /etc/redis/6379.conf

Set daemonize to yes (by default it is set to no).

to yes (by default it is set to no). Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).

to (modify the port if needed). Change the port accordingly. In our example it is not needed as the default port is already 6379.

accordingly. In our example it is not needed as the default port is already 6379. Change the bind to 0.0.0.0.

to 0.0.0.0. Set your preferred loglevel .

. Set the logfile to /var/log/redis_6379.log

to Set the dir to /var/redis/6379 (very important step!)

Finally, add the new Redis init script to all the default runlevels for CentOS using the following command:

sudo chkconfig --add redis_6379

You are done! Now you can try running your instance with:

sudo /etc/init.d/redis_6379 start

Make sure that everything is working as expected:

Try pinging your instance with redis-cli.

Do a test save with redis-cli save and check that the dump file is correctly stored into /var/redis/6379/ (you should find a file called dump.rdb).

and check that the dump file is correctly stored into /var/redis/6379/ (you should find a file called dump.rdb). Check that your Redis instance is correctly logging in the log file.

If it’s a new machine where you can try it without problems make sure that after a reboot everything is still working.

Note: In the above instructions we skipped many Redis configuration parameters that you would like to change, for instance in order to use AOF persistence instead of RDB persistence, or to setup replication, and so forth. Make sure to read the example redis.conf file (that is heavily commented) and the other documentation you can find in this web site for more information.

Reference