DEV Community

Arun Kumar for AWS Community Builders

Posted on

1

How to connect to ElastiCache Redis

Rationale

If ElastiCache/Redis deployments default encryption for both inflight and at-rest, then this could cause issues with connectivity for some clients, like redis-cli.

Solution Summary

[https://aws.amazon.com/premiumsupport/knowledge-center/elasticache-connect-redis-node/]

Two parts

  • Deploy EC2 for your app/branch and run stunnel to Redis (then use SSM to SSH into the server and run Redis commands from CLI)
  • Use SSM to port forward 2 x ports from your EC2 + stunnel setup to localhost, and connect with a desktop client.

Steps

  • Using an ec2 I have an ec2 keypair for (app server):
INSTANCE_NAME=demo-app
Enter fullscreen mode Exit fullscreen mode
  • Find the instance ID based on Tag Name
INSTANCE_ID=$(aws ec2 describe-instances \
 --filter “Name=tag:Name,Values=${INSTANCE_NAME}” \
 --query “Reservations[].Instances[?State.Name == ‘running’].InstanceId[]” \
 --output text)
Enter fullscreen mode Exit fullscreen mode
  • To connect to the EC2 to test connectivity
aws ssm start-session — target “${INSTANCE_ID}” 
# — — — — — — — — — — — -
# On the EC2
# — — — — — — — — — — — -
Enter fullscreen mode Exit fullscreen mode
  • Test EC2 connectivity to redis is OK
curl -v telnet://master.demo.cache.amazonaws.com:6379
Enter fullscreen mode Exit fullscreen mode
  • Setup stunnel as per -

[https://aws.amazon.com/premiumsupport/knowledge-center/elasticache-connect-redis-node/]

  • Install stunnel on ec2
sudo yum install -y stunnel
Enter fullscreen mode Exit fullscreen mode
cat /etc/stunnel/redis-cli.conf

fips = no
setuid = root
setgid = root
pid = /var/run/stunnel.pid
debug = 7
options = NO_SSLv2
options = NO_SSLv3
[redis-cli]
 client = yes
 accept = 127.0.0.1:6379
 connect = master.demo.cache.amazonaws.com:6379
[redis-cli-slave]
 client = yes
 accept = 127.0.0.1:6380
 connect = demo.app.cache.amazonaws.com:6379
Enter fullscreen mode Exit fullscreen mode
  • Run stunnel (as root)
sudo stunnel /etc/stunnel/redis-cli.conf
Enter fullscreen mode Exit fullscreen mode
  • Check if it’s up
netstat -tulnp | grep -i stunnel
exit
# — — — — — — — — — — — -
# Back on the laptop
# — — — — — — — — — — — -
Enter fullscreen mode Exit fullscreen mode
  • Create 2 port forwarding tunnels for stunnel redis
aws ssm start-session --target $INSTANCE_ID \
 --document-name AWS-StartPortForwardingSession \
 --parameters ‘{“portNumber”:[“6379”],”localPortNumber”:[“6379”]}’

aws ssm start-session — target $INSTANCE_ID \
 --document-name AWS-StartPortForwardingSession \
 --parameters ‘{“portNumber”:[“6380”],”localPortNumber”:[“6380”]}’
Enter fullscreen mode Exit fullscreen mode
  • Now test from laptop
redis-cli -h localhost -p 6379 -a eNdU35somebigpasswordXpvD ping
Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

Create a simple OTP system with AWS Serverless cover image

Create a simple OTP system with AWS Serverless

Implement a One Time Password (OTP) system with AWS Serverless services including Lambda, API Gateway, DynamoDB, Simple Email Service (SES), and Amplify Web Hosting using VueJS for the frontend.

Read full post

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay