5
votes

How can you make your local Django development server think it's running inside your AWS network using SSH tunneling?

My scenario, I'm running a local Django server i.e. python manage.py runserver and Redis as cache backend (Elasticache). When my app runs in the AWS environment it has access to Elasticache, but, Locally it won't (and that's a good thing). If for some reason I want to test my local environment with Elasticache I need to somehow use SSH tunneling to make AWS think it's running inside the VPC network.

I've tried to get this working by using below. I've confirmed I can connect locally using SSH tunneling with Redis Desktop Manager so 100% I know AWS supports this, my problem is now doing the same thing with Django.

This is what I've tried:

> python manage.py runserver 8000
> ssh -i mykey.pem [email protected] -L 6379:localhost:6379

I get the message "Error 60 connecting to" message when I visit http://127.0.0.1:8000/.

What I'm I doing wrong here?

Notes:

  • [email protected] is not the Redis server, just another EC2 instance on AWS that has access Elasticache that I want to use as a tunnel.
  • The mykey.pem has access and the correct permission.
  • The ec2 instance has all the correct permissions and ports for access.
  • Tested SSH tunneling with Redis Desktop Manager and this works for that software.
  • Elasticache and the EC2 instances are all in the same region and can connect to each other.
1
Why are you trying to connect to a production Redis instance? Can't you just run a local instance on your machine instead? As for your question, you probably don't want to use 6379:localhost:6379, but instead something like 6379:redis-servers.ip.address:6379, but that's just a guess. Use ssh -vv to debug tunneling problems, it will print out a bunch of stuff to stderr. - koniiiik
I believe memcached on AWS would only work within the AWS network. I tried without luck to connect from my local enviroment. BTW Dont forget to open the Elasticache ports in your EC2 security groups. Also I do run my Django apps through AWS Elastc Beanstalk. - WayBehind
@koniiiik You misunderstand, I'm not trying to connect directly Redis but want to use SSH tunneling (to any EC2 instance) to make it seem like my local environment is running inside the VPC. - Prometheus
@WayBehind I've confimed I can indeed connect locally using SSH tunneling with Redis Desktop Manager so I know this works. The issue is how to do the same thing when runnign Django server. - Prometheus
Could you perhaps post the settings you use in Redis Desktop Manager to set up the SSH tunnel, and what settings you enter into the “Connection” tab? I still think your issue is that you are making the SSH server on the remote end forward connections to localhost, which would mean the same machine you are SSH-connecting to, which, as you said, is not the machine running Redis. Also, please post the settings you use in your Django application to connect to Redis. - koniiiik

1 Answers

3
votes
ssh -i mykey.pem [email protected] -L 6379:localhost:6379

This will forward requests from your local machine (on :6379) to localhost:6379 on the EC2 instance. This is not what you want (unless you have redis running locally on the EC2 instance)

You should use the Elasticache IP instead

ssh -i mykey.pem [email protected] -L 6379:<elasticache-ip>:6379