Connect Redis/Elasticache to application in Amazon Lightsail instance
Tags: aws, redis, vpc, cloud
Published: 2022-05-28 14:32:18

You can create Redis/Elasticache even though Amazon Lightsail doesn't offer it. You can create an Elasticache for Redis cluster in AWS and connect using VPC Peering. 

## Prerequisites 

1. An Amazon Lightsail instance. To create a new instance follow [**Deploy Laravel Application to Amazon Lightsail**](https://blog.harun.dev/deploy-laravel-application-to-amazon-lightsail) or [**Deploy WordPress App to Amazon Lightsail**](https://blog.harun.dev/deploy-wordpress-app-to-amazon-lightsail)
2. Be a little familiar with AWS Console.

## Create Elasticache for Redis

1. Go to [**Elasticache for Redis console**](https://us-east-1.console.aws.amazon.com/elasticache/home?/dashboard&region=us-east-1#/dashboard?getStarted=expand). Change the region that you want to launch it. I selected **us-east-1** as my Lightsail Instance(s) are there. 

2. Click the **Create Redis cluster** button from the center of the page (or wherever you see it)
![CleanShot 2022-05-28 at 18.24.04@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653740823924/ayTfyM9Sc.png)

3. You will land in this screen 👇
![CleanShot 2022-05-28 at 18.38.27@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653741612053/kUlxh7BaC.png)
  - A. Choose **Configure and create a new cluster**
  - B. Choose cluster mode **Disabled**
  - C. Give it a name
  - D. Uncheck **Multi-AZ** Enable
  - E. Uncheck **Auto-failover** Enable
  - F. Select a free-tier node type. Currently it's "cache.t3.micro" and "cache.t2.micro".
  - G. Do not need any replica for this demo. Make it **"0"**.
  - H. Give subnet groups a name
  - I. Select your VPC
  - J. And then clieck the **Next** button from the bottom.

3. You will be on the second page. Just uncheck **Enable automatic backup** and click the **Next** button from the bottom of the page. 
![CleanShot 2022-05-28 at 18.50.07@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653742318231/ahAMqpjdB.png)

4. Review everything on the 3rd and final page. And then click the **Create** button from the bottom of the page. 

5. It will take a few minutes to get created. Wait for status changes to **Available**.
![CleanShot 2022-05-28 at 19.03.41@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653743046013/ETHuHBu8T.png)

6. now, click on the Cluster name. And you will be on the details page. 
![CleanShot 2022-05-28 at 19.05.38@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653743211659/IfsLDKS_6.png)
Copy the **Primary endpoint** in your clipboard or somewhere safe. 

### Configure Security Group for Redis cluster

1. Go to the [**Security groups**](https://us-east-1.console.aws.amazon.com/vpc/home?region=us-east-1#securityGroups:) of VPS Console. Make sure you are in the same region as the Redis cluster. And then click the **Create security group** from the top-right corner. 
![CleanShot 2022-05-28 at 19.33.08@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653744863578/l7kjpTUOR.png)

2. You are in create Security Group page
![CleanShot 2022-05-28 at 19.41.30@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653745388466/tlEobrLnW.png)
  - A. Give it a name
  - B. Click on the **Add rule** button of the Inbound rules section.
  - C. Select 6379 as Custom TCP Port
  - D. Add your Amazon Lightsail VPC IP range. To generate it, check your instance's Private IP address. Mine is 172.26.3.157. Replace the last two sections with 0 and add /16. So, mine became 172.26.0.0/16. 
  - E. Optionally, you can add a description
  - F. Then hit the **Create security group** button from the bottom of the page.

3. Now go to the **Network and security** tab of your Redis cluster and click the **Modify** button from the **Security groups** section.
![CleanShot 2022-05-28 at 19.55.02@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653746258674/GFFV32uHF.png")

4. Click on the **Manage** button from the next screen
![CleanShot 2022-05-28 at 19.58.02@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653746332691/9RL95yIIC.png)

5. Select the correct security group from the pop and then click on the **Choose** button. 
![CleanShot 2022-05-28 at 19.59.19@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653746443378/yYbIwHuCh.png)

6. Click **Modify** button from the bottom of the page.
![CleanShot 2022-05-28 at 20.00.47@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653746505482/U9cm147ER.png)
It will take a few minutes to complete the modification. Wait until the status changes to **Available**.

## VPC Peering

The important part of this tutorial. We need to peer VPC between Lightsail and Redis cluster's VPC to connect it. 

1. Go to the homepage of the Amazon Lightsail console. Click the **Account** from the Account menu dropdown. 
![CleanShot 2022-05-28 at 19.10.54@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653743506780/Kkbk-kt5P.png)

2. Click on the **Advanced** tab from the Account settings page. You will see a list of your VPCs. Connect the one that resides in your Redis cluster. For me, it's **Virginia (us-east-1)
**. 
![CleanShot 2022-05-28 at 19.12.35@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653743731968/0hJH7OqKe.png)

## Connect to the Redis cluster from the Lightsail instance

### Using CLI 

1. Connect to the instance using SSH. [Follow this](https://blog.harun.dev/deploy-wordpress-app-to-amazon-lightsail#heading-connect-to-the-instance-using-ssh) to connect to SSH.

2. Install Redis CLI 
  - A. On Amazon Linux 2 
  ```bash
  sudo amazon-linux-extras install epel -y
  sudo yum install gcc jemalloc-devel openssl-devel tcl tcl-devel -y
  sudo wget http://download.redis.io/redis-stable.tar.gz
  sudo tar xvzf redis-stable.tar.gz
  cd redis-stable
  sudo make BUILD_TLS=yes
  ```
  - B. on Ubuntu
  ```bash
  sudo apt install redis-server
  ```

3. Now connect to the Redis cluster. The moment of truth.
```bash
redis-cli -h <redis_endpoint> -c -p 6379
```
Replace `<redis_endpoint>` with your Primary endpoint of Redis you copied in the clipboard. Remove port (":6379") from the endpoint URL. You will see this screen if successful 👇 
![CleanShot 2022-05-28 at 20.10.24@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1653747050113/8YFSWvlOm.png)
You can use the `ping`, `set`, and `get` commands to test it out. 

### Connect to Application

You will get packages for most of the Applications, languages, and Frameworks. Check docs. For Laravel, you just have to configure it in the `.env` file.  

## Conclusion

Hope you enjoyed this short article. You can subscribe to my newsletter, and follow me on [Twitter](https://twitter.com/HarunRRayhan), [Dev.to](https://dev.to/harunrrayhan), and [Hashnode](https://hashnode.com/@HarunRRayhan).