How to setup HTTP to HTTPS redirection for a django application on AWS Elastic Beanstalk and have health check working?

Baptiste Florentin
3 min readSep 26, 2019
Photo by Ty Lorenz on Unsplash

I only found a couple of outdated posts to setup redirection on aws eb, so I thought I would write a short tutorial on how I made it work.

Setup HTTPS

First thing, you need to redeem certificates for your domain name on aws acm, you will find a lot of tutorials online, here is one that I recommend :

Then when your certificates are ready to be used you can head over to :

https://eu-west-3.console.aws.amazon.com/ec2

Go to Load BalancingLoad BalancersListeners Add listener

Chose HTTPS, port 443

On Add action chose Forward to and in the drop-down list chose your AWS eb instance.

And on Default SSL Certificate chose your certificate on the right drop-down menu.

Now if you type https:// in front of your website’s url you should be able to access it via https.

How we will proceed for redirection

Now is time to set up the HTTP to HTTPS redirection. Imagine your site is called mysite.com , we want that whenever a user types mysite.com or http://mysite.com it redirects him automatically to https://mysite.com automatically.

We could do a simple redirection of all requests to https but the load balancer regularly makes health checks in order to evaluate the instance’s health. These health checks should be made through HTTP.

Setting up health check

First, add django-health-check to your requirements.txt file

then install it via $ pip install -r requirements.txt

Then change the website’s settings in settings.py

INSTALLED_APPS += (  'health_check',  'health_check.db',  'health_check.cache',  'health_check.storage',)

And then add to your urls.py

from django.conf.urls import url, includeurlpatterns += [
url(r'^health/?', include('health_check.urls')),
]

Check that http://mysite.com/health works well.

HTTP to HTTPS redirection

Then go to https://eu-west-3.console.aws.amazon.com/ec2 and click on Instances > click on your instance and in Description find Public DNS (IPv4) and copy this value.

Then in settings.py add the URL previously copied in ALLOWED_HOST.

Now we will redirect HTTP to HTTPS for all paths but /health/ so the load balancer can make HTTP health checks on /health/.

To do this go to

https://eu-west-3.console.aws.amazon.com/ec2 Load Balancing → Load Balancers → Select your load balancer → In the bottom of the page click on Listener → And for the HTTP : 80 row click on View/edit rules in the Rules column

Now you can add redirection rules. Click on the plus icon at the top and add a rule. Now on Add condition chose Path then type /health/ and then click on Add action and chose Forward to and add your HTTP instance.

Now chose the pencil at the top and edit the last rule at the bottom, chose Redirect to HTTPS and just add the port 443

Now if you type http://mysite.com you should be redirected to https://mysite.com but not if you type http://mysite.com/health/

Now just head back to AWS Elastic Beanstalk and in your application click on ConfigurationLoad BalancerProcesses → click on the default process ActionsEdit → And modify the path to /health/

Conclusion

You should now be up and running, check if the redirection works and if the health of your instance turns to healthy.

If you have any problems following this tutorial, please let me know. I could have forgotten something in which case, I will thank you for bringing it up, help you out and bring modifications to this post.

Hope you found it helpful!

--

--