π AWS ElastiCache (Redis) Setup Guide with EC2 Client
π Overview
This guide walks you through:
- Creating a Redis cluster using AWS ElastiCache
- Configuring networking (VPC, Subnets, Security Groups)
- Launching an EC2 instance
- Installing Redis CLI
- Connecting EC2 β ElastiCache Redis
ποΈ Architecture
EC2 (Client)
β
Security Group (Port 6379)
β
ElastiCache Redis Cluster (Primary + Replica)
βοΈ PHASE 1: Create ElastiCache Redis Cluster
πΉ Step 1: Go to ElastiCache
- Open AWS Console
- Search for ElastiCache
- Click Create Redis Cluster
πΉ Step 2: Choose Configuration
- Cluster Mode β Disabled (Node-based)
- Cluster Type β Cluster Cache
- Engine β Redis
- Version β Latest stable
πΉ Step 3: Cluster Settings
- Cluster Name β
my-redis-cluster - Node Type β
cache.t3.micro(free-tier friendly) - Number of Replicas β
1 - Multi-AZ β β Disabled (for learning; enable in production)
πΉ Step 4: Connectivity Settings
- VPC β Default VPC
- Subnet Group β Create new
Create Subnet Group:
- Select 2 subnets only
- Choose different Availability Zones (AZs)
πΉ Step 5: Availability Zone Placement
- Choose: Specify Availability Zones
- Select 2 AZs manually
πΉ Step 6: Security & Encryption
- Encryption in Transit β β Disabled (for simplicity)
- Encryption at Rest β Optional
- AUTH β Disabled (enable in production)
πΉ Step 7: Backup & Logging
- Backup β β Disabled
- Log Delivery β β Enabled (recommended)
πΉ Step 8: Create Cluster
- Click Create
- Wait ~5β10 minutes
π PHASE 2: Configure Security Group
πΉ Step 1: Create Security Group
Go to EC2 β Security Groups β Create
- Name β
redis-sg
Inbound Rules:
| Type | Port | Source |
|---|---|---|
| Custom TCP | 6379 | EC2 Security Group |
π Important:
Allow only EC2 SG, NOT 0.0.0.0/0
πΉ Step 2: Attach SG to Redis
- Go to ElastiCache cluster
- Modify β Attach
redis-sg
π₯οΈ PHASE 3: Launch EC2 Instance
πΉ Step 1: Create EC2
-
AMI β Amazon Linux 2023
-
Instance Type β t2.micro / t3.micro
-
VPC β Same as Redis
-
Security Group:
- Allow SSH (22) from your IP
πΉ Step 2: Connect to EC2
ssh -i your-key.pem ec2-user@your-public-ip
π§° PHASE 4: Install Redis CLI on EC2
πΉ Step 1: Check OS
cat /etc/os-release
πΉ Step 2: Update System
sudo dnf update -y
πΉ Step 3: Install Dependencies
sudo dnf install -y gcc gcc-c++ make jemalloc-devel openssl-devel tcl tcl-devel wget tar
πΉ Step 4: Download Redis
cd /tmp
wget http://download.redis.io/redis-stable.tar.gz
πΉ Step 5: Extract
tar xvzf redis-stable.tar.gz
cd redis-stable
πΉ Step 6: Build Redis (with TLS)
make BUILD_TLS=yes
πΉ Step 7: Install
sudo make install
πΉ Step 8: Verify Installation
redis-cli --version
π PHASE 5: Connect EC2 to Redis
πΉ Step 1: Get Primary Endpoint
- Go to ElastiCache
- Copy Primary Endpoint
Example:
my-redis.xxxxxx.ng.0001.use1.cache.amazonaws.com:6379
πΉ Step 2: DNS Check
nslookup your-endpoint-without-port
πΉ Step 3: Connect to Redis
Without TLS:
redis-cli -h your-endpoint -p 6379
With TLS:
redis-cli -h your-endpoint --tls
πΉ Step 4: Test Connection
set name "AWS Redis"
get name
π PHASE 6: Connect Application (Node.js)
Install Redis Client
npm install redis
Example Code
import { createClient } from "redis";
const client = createClient({
socket: {
host: "your-endpoint",
port: 6379,
},
});
await client.connect();
await client.set("key", "value");
const data = await client.get("key");
console.log(data);
Check:
β Same VPC β Correct Security Group (Port 6379) β EC2 allowed in Redis SG β Endpoint copied correctly (no port in DNS step)
π― Final Flow
EC2 β Redis CLI β ElastiCache Primary Endpoint β Redis Cluster