Skip to content

Commit c44b7b0

Browse files
committed
add blog post
1 parent efef6e5 commit c44b7b0

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: "Deploying a Django App with Real-time Moderation Using Defang"
3+
author: Defang Team
4+
tags: [deployment, django, docker, compose]
5+
---
6+
7+
# Deploying a Django App with Real-time Moderation Using Defang
8+
9+
In this guide, we'll walk through the easiest and fastest way to deploy a full-featured Django application—including real-time chat and background task processing—to the cloud using Defang. You'll see firsthand how simple Defang makes it to deploy apps that require multiple services like web servers, background workers, Redis, and Postgres.
10+
11+
## Overview of Our Django Application
12+
13+
We're deploying a real-time chat application that includes automatic moderation powered by a background worker using the Natural Language Toolkit (NLTK). The application structure includes:
14+
15+
- **Web Service**: Django app with chat functionality using Django Channels for real-time interactions.
16+
- **Worker Service**: Background tasks processing messages for profanity and sentiment analysis.
17+
- **Postgres Database**: Managed database instance for persistent storage.
18+
- **Redis Broker**: Managed Redis instance serving as the broker for Celery tasks and Django Channels.
19+
20+
## Running Locally
21+
22+
To run the app locally, we leverage Docker Compose, splitting configurations into two YAML files:
23+
24+
- `compose.yaml`: Production configuration.
25+
- `compose.dev.yaml`: Development overrides extending production.
26+
27+
You can quickly spin up the application locally with:
28+
29+
```bash
30+
docker compose --env-file .env.dev -f compose.dev.yaml up --build
31+
```
32+
33+
This replicates your production environment locally, passing environment variables in the same way as we will with Defang's [secure configuration system](https://docs.defang.io/docs/concepts/configuration).
34+
35+
## Application Features
36+
37+
### Real-time Chat
38+
Using Django Channels and Redis, users can engage in real-time conversations within chat rooms.
39+
40+
### Background Moderation Tasks
41+
The worker service runs independently, handling moderation tasks asynchronously. It uses NLTK to:
42+
- Check for profanity.
43+
- Perform sentiment analysis.
44+
- Automatically flag negative or inappropriate messages.
45+
46+
This decouples resource-intensive tasks from the main API server, ensuring optimal application responsiveness. The demo isn't doing anything very complicated, but you could easily run machine learning models [with access to GPUs](https://docs.defang.io/docs/tutorials/deploy-with-gpu) with Defang if you needed to.
47+
48+
## Deploying with Defang
49+
50+
Deploying multi-service applications to cloud providers traditionally involves complex infrastructure setup, including configuring ECS clusters, security groups, networking, and more. Defang simplifies this significantly.
51+
52+
### Deploying to Defang Playground
53+
54+
The Defang Playground lets you quickly preview your deployed app in a managed environment.
55+
56+
#### Secure Configuration
57+
58+
Before deploying, securely set encrypted sensitive values:
59+
60+
```bash
61+
defang config set DJANGO_SECRET_KEY
62+
defang config set POSTGRES_PASSWORD
63+
```
64+
65+
Then run the deployment command:
66+
67+
```bash
68+
defang compose up
69+
```
70+
71+
Defang automatically:
72+
- Builds Docker containers.
73+
- Sets up required services.
74+
- Manages networking and provisioning.
75+
76+
Once deployed, your app is accessible via a public URL provided by Defang, which you can find in the CLI output or in our portal at [https://portal.defang.io](https://portal.defang.io)
77+
78+
### Deploying to Your Own Cloud
79+
80+
To deploy directly into your AWS account (or other [supported providers](https://docs.defang.io/docs/category/providers)):
81+
82+
1. Set your cloud provider:
83+
> In my case, I use an AWS Profile, but you should be able to use [any methods supported by the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
84+
85+
```bash
86+
export DEFANG_PROVIDER=AWS
87+
export AWS_PROFILE=your-profile-name
88+
```
89+
#### Secure Configuration
90+
91+
Before deploying, securely set encrypted sensitive values in your cloud account:
92+
93+
```bash
94+
defang config set DJANGO_SECRET_KEY
95+
defang config set POSTGRES_PASSWORD
96+
```
97+
98+
2. Deploy:
99+
100+
```bash
101+
defang compose up
102+
```
103+
104+
Defang handles provisioning managed services (RDS for Postgres, ElastiCache for Redis), container builds, and networking setup. Note: Initial provisioning for managed data stores might take a few minutes.
105+
106+
## Cloud Deployment Results
107+
108+
Post-deployment, your Django app infrastructure includes (among other things):
109+
- **Managed Postgres**: AWS RDS instance.
110+
- **Managed Redis**: AWS ElastiCache instance.
111+
- **Containers**: ECS services with load balancers and DNS configured.
112+
113+
## Why Use Defang?
114+
115+
Defang simplifies complex cloud deployments by:
116+
- Automatically provisioning managed cloud resources.
117+
- Securely handling sensitive configurations.
118+
- Providing seamless container orchestration without manual infrastructure setup.
119+
120+
## Try It Yourself
121+
122+
Explore deploying your Django applications effortlessly with Defang. The full source code for this example is available on [GitHub](https://github.com/DefangLabs/django-chat-demo). Feel free to give it a try, and let us know how it goes!
123+
124+
Happy deploying!

0 commit comments

Comments
 (0)