Blog is migrated to Medium

Blog is migrated to Medium. We moved Speedle blog to Medium for some reasons. I just copied all posts to the platform. Sorry for the inconvenience..

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Deploy Django Channels 2.x on AWS Elastic Beanstalk

If you have been frantically scouring the web for explanations and solutions on any of these subjects:

Then I hope this can be the guide for you.

However, if you are looking for a beginner-friendly tutorial on how to set it all up, this might be a bit difficult to chew through.

I’ve just spent close to a week to get all these services to play well together after constantly jumping that entire time between a few dozen browser tabs with outdated tutorials and incomplete StackOverflow answers. So, I’m striking the iron while it’s hot (because by next week I’ll have forgotten all these details) and I’m writing a comprehensive tutorial so others don’t have to go through the same experience.

You can skip whichever section you already got figured out as they are loosely coupled. At the end of each one, I’ll write a list of common issues, what they mean, likely solutions, and places of interest to gather information for diagnostics.

However, before you proceed, you have to decide on how you are going to route your WebSocket traffic. Here are your 3 choices:

I got my deployment running with all three configurations and chose Option 2, merely through my limited empirical evidence. Option 1 had some delays that I couldn’t explain; and as for Option 3, I wasn’t quite comfortable letting Daphne handle all my traffic. No offense, Daphne!

As you may know, you can configure everything in Elastic Beanstalk through either their online interface or through config files. I will cover how to deploy with the latter, as this is more common practice for production environments. You can just script your deployments and not have to go manually tweak settings every time you upload a new version.

3.1. During the upcoming eb create -v command, you will be asked to define the application, environment, and CNAME for your Elastic Beanstalk. This CNAME will determine the URL that you will use to access the Load Balancer that connects to the EC2 instances. eb deploy -v is what you will use to deploy new versions.

4.1. If you are doing Option 1 then add the following:

What this is doing is telling Elastic Beanstalk to allow the Application Version Elastic Load Balancer (hence elbv2) to listen on port 80 and if a request comes in to a path that matches the following pattern: hostname:80/ws/* then forward it to port 5000 in the EC2 instance it is managing.

Note: It doesn’t have to be port 5000 the one to handle your WebSockets and your path doesn’t have to be /ws/. Also, your process names and rule names don’t have to be http, websocket, and ws, respectively.

4.2. If you are doing Option 2 or Option 3 then add the following:

This is telling Elastic Beanstalk to allow the Application Load Balancer to listen and forward any request on port 80 and port 5000.

5. Add another config file:

6. Finish your deployment and deploy new versions:

AWS Console -> Elastic Beanstalk -> Application -> Environment -> Request Logs
AWS Console -> Elastic Beanstalk -> Application -> Environment -> Configuration -> Load Balancer

2. Create a Redis instance through AWS ElastiCache.

3. Add the following to your Django config file:

If you are doing Option 2 or Option 3, you have to make Apache act as a reverse proxy for either just WebSocket connections or for both WebSocket and HTTP connections.

Note: You don’t have to use Apache. There are a lot of resources that explain how to do this in NGINX if you prefer that option.

This is adding a new Apache config file with the ProxyPass and ProxyPassReverse directives to proxy any connection that matches the following path hostname:80/ws/* to ws://127.0.0.1:5000/ws/* and upgrade those connections from HTTP to WebSocket.

As you may have noticed, this is exactly what the Load Balancer in Option 1 was doing but now we are doing it at the webserver level.

Note: It doesn’t have to be port 5000 the one to handle your WebSockets and your path doesn’t have to be /ws/.

Crack open your browser console and you should see the following if your proxy is upgrading the connection:

AWS Console -> EC2 -> Load Balancers-> Load Balancer Instance-> Edit Attributes

Another potential culprit for delays is having DEBUG=True in your Django config or having django-debug-toolbar enabled.

2. Add the following file to .ebextensions:

This will add the Daphne and worker logs into the managed Elastic Beanstalk logs. It will eliminate having to SSH just to view what’s going on inside your EC2 instance.

I hope this post is helpful and complete and will save some from that exhaustion of jumping through endless StackOverflow posts, blogs, documentation pages, etc, and jury-rigging it until it works.

Example of “jury-rigged”

If you have any comments, questions, or corrections, please let me know! Also if I come across new information I’ll make updates to keep this as clean as possible for as long as possible.

Add a comment

Related posts:

Resolving our platform stability issues

A couple of years back we started to face some serious scaling issues. As the platform had grown and we had onboarded more and more customers, system response times began to suffer due to server…

Deepfake Software Startups That are Commercializing the Technology

Deepfake software startups are scrambling to find ways we can use the tech to revolutionize industries including gaming, entertainment, sales and marketing

How Important Is a Mobile App for My Company?

Mobile devices have a huge impact on the business world. With 81% of Americans owning a smartphone, there is a high probability that your users will expect to use your product from a mobile device…