DEV Community šŸ‘©ā€šŸ’»šŸ‘Øā€šŸ’»

yuki0417
yuki0417

Posted on

Easy way to connect to Amazon ElastiCache (redis) with password from Django app

Requirements

  • ElastiCache (redis) instance

  • your Django app

  • git

  • pip package manager

install redis

install redis by below command

pip install redis
Enter fullscreen mode Exit fullscreen mode

Copy the part of django-redis-sessions in your project directory

  • Clone to any directory

Clone git repository wherever you want to put.

git clone https://github.com/martinrusev/django-redis-sessions#django-redis-sessions
Enter fullscreen mode Exit fullscreen mode
  • Copy redis_sessions to your project directory

Copy redis_sessions directory in django-redis-sessions to your project directory.

cp -r redis_sessions your_directory
Enter fullscreen mode Exit fullscreen mode
  • Edit session.py and your settings.py

Open below files with editor, and add the codes.

# session.py

    def get(self):
        ...
        elif self.connection_type == 'redis_host':
        ...
            db=settings.SESSION_REDIS_DB,
            # add below code
            SSL=True,


# settings.py

# add below code
SESSION_ENGINE = 'your_directory.redis_sessions.session'

SESSION_REDIS = {
    'host': 'your_aws_elasticache_endpoint_name',
    'port': your_port,
    'db': 0,
    'password': your_password,
    'prefix': 'session',
    'socket_timeout': 3,
    'retry_on_timeout': False
}
Enter fullscreen mode Exit fullscreen mode

ā€» In your password setting, using encryption middleware such as django-environ, python-decouple is better.

The reason why we edit session.py is to use SSL mode. By doing that, we can connect to ElastiCache by SSL tunnel mode.

Finish! Check Connection

Confirm by running the server, testing, or connecting to ElastiCache directly, etc.

I hope I can help you to deploy your Django app to AWS!!

Top comments (1)

Collapse
priyanshupanwar profile image
Priyanshu Panwar

Thanks a lot for this.
Can you please provide the git link of this project.
Would be really helpful.

šŸŒš Browsing with dark mode makes you a better developer.

It's a scientific fact.