DEV Community

Cover image for AWS Aurora DSQL for Django Developers: A Step-by-Step Guide

AWS Aurora DSQL for Django Developers: A Step-by-Step Guide

Amazon Aurora is a cloud-native relational database service that provides high performance and scalability. With the introduction of Aurora DSQL (Distributed SQL), developers can now leverage distributed database capabilities to enhance reliability and performance. This guide will walk you through setting up Aurora DSQL for a Django project in four steps:

Prerequisites

Before proceeding, ensure you have the following:

  • An AWS account with necessary IAM permissions.
  • A Django project.
  • AWS CLI installed and configured.
  • boto3 installed in your Django environment (pip install boto3).

  • Ensure you have the right dependancies in the requirements.txt

Django==4.0
django-admin-cli==0.1.1
djangorestframework==3.15.1
psycopg2==2.9.9
sqlparse==0.5.0
python-dotenv==0.19.0

aurora_dsql_django
boto3>=1.35.74
botocore>=1.35.74
Enter fullscreen mode Exit fullscreen mode

Step 1.Provision an Aurora DSQL Cluster

  • Navigate to the Aurora DSQL Console.
  • Create Cluster
  • Security Group for network access. Inbound - Best practice: Restrict inbound access to your VPC CIDR (if the application runs inside the same VPC) or your server’s specific IP and avoid using 0.0.0.0/0 unless for testing purposes Outbound - Is set to Allow all traffic from 0.0.0.0/0 by default

Step 2: Connect Django to Aurora DSQL

Change Database settings in your django app's settings.py

DATABASES = {
    'default': {
        'HOST': 'uiabtxahshv6at5pcfidcxfnbq.dsql.us-east-1.on.aws',
        'USER': 'postgres',
        'NAME': 'postgres',
        'ENGINE': 'aurora_dsql_django',
        'OPTIONS': {
            'sslmode': 'require',
            'region': 'us-east-1',
            'region': 'us-east-2',
            'expires_in': 60
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Migrate the Database

Run the following commands to apply migrations:

python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode

Image description

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Image description

Remember to clear any previous migrations, using:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
Enter fullscreen mode Exit fullscreen mode
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
Enter fullscreen mode Exit fullscreen mode

Image description

Step 4: Monitor and Scale

Use Amazon CloudWatch and Performance Insights to monitor query performance and optimize configurations.

Aurora DSQL enhances Django applications by providing high availability, scalability, and better performance. By following this guide, you can integrate Aurora DSQL into your Django project efficiently. Explore additional optimizations and best practices to get the most out of your Aurora database setup.

Follow me for more demos and networking. Kevin Kiruri LinkedIn

Top comments (0)