DEV Community

yuki0417
yuki0417

Posted on

1 1

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!!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay