Deploying Django on AWS Elastic Beanstalk

Deploying Django on AWS Elastic Beanstalk

Introduction: In this tutorial, we will walk through the process of deploying a Django application on AWS Elastic Beanstalk, a fully managed service that makes it easy to deploy and scale web applications. By the end of this guide, you'll have your Django application up and running on Elastic Beanstalk, ready to scale and handle real-world traffic.

Prerequisites:

  • Python installed on your machine
  • Pip and Virtualenv installed
  • AWS Elastic Beanstalk CLI (EB CLI) installed
  • A basic understanding of Django and virtual environments

Step 1: Set up your virtual environment

C:\> virtualenv %HOMEPATH%\eb-virt
C:\>%HOMEPATH%\eb-virt\Scripts\activate
(eb-virt) C:\>        

Step 2: Install Django and Create a Project

(eb-virt)~$ pip install django==2.2
(eb-virt)~$ django-admin startproject ebdjango
(eb-virt) ~$ cd ebdjango
(eb-virt) ~/ebdjango$ python manage.py runserver        
Note : After deploying you can replace the existing ebdjango with your django project to deploy :)

Step 3: Configure for Elastic Beanstalk

3.1 Activate virtual environment and save requirements:

(eb-virt) ~/ebdjango$ pip freeze > requirements.txt        

3.2 Create .ebextensions/django.config:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application        

3.3 Deactivate virtual environment:

(eb-virt) ~/ebdjango$ deactivate        

Step 4: Deploy with EB CLI 4.1 Initialize EB CLI repository:

Note: Be careful here; for this step, I have provided a solution below in case you encounter an error.

~/ebdjango$ eb init -p python-3.7 django-tutorial        

4.2 (Optional) Configure SSH key pair:

~/ebdjango$ eb init        

4.3 Create environment and deploy:

~/ebdjango$ eb create django-env        
S3 BUCKET
EC2

Step 5: Update Django Settings and Redeploy

5.1 Find environment domain name:

~/ebdjango$ eb status        

5.2 Update settings.py with the domain:

ALLOWED_HOSTS = ['eb-django-app-dev.elasticbeanstalk.com']        

5.3 Deploy changes:

Ps : the fun part xD :P

~/ebdjango$ eb deploy        

Conclusion: Congratulations! You have successfully deployed your Django application on AWS Elastic Beanstalk. Your application is now accessible via the provided domain name. Feel free to explore Elastic Beanstalk's scalability features and adapt your Django application for production use.

I faced a configuration error and an unauthorized access issue with the following error message:

'ERROR: ServiceError - '--version/20190924/us-west-2/elasticbeanstalk/aws4_request' is not a valid key=value pair (missing equal-sign) in the Authorization header: 'AWS4-HMAC-SHA256 Credential=eb --version/20190924/us-west-2/elasticbeanstalk/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=afc27125738fef1062fc8565e130ced6e0f7b2c343c2e28456d7693c8f396c92.'         

I solved it by following the steps outlined in a similar Stack Overflow question linked in my original post. Here's what I did: I navigated to my root directory using the command 'cd', and then used 'ls -a' to reveal hidden files. I noticed a directory named '.aws', so I changed into that directory using 'cd .aws' and opened the 'config' file with the command 'open config'. The contents of the 'config' file were as follows:

aws_access_key_id = eb --version 
aws_secret_access_key = ENTER_SECRET_HERE 

[default] output = json 
region = 3        

I made the necessary corrections in the file."

Note: The placeholder 'ENTER_SECRET_HERE' suggests that the actual secret access key should be entered in that position. Additionally, you may want to double-check if the 'aws_access_key_id' is correctly formatted as it seems to include unexpected values such as 'eb --version'.


https://stackoverflow.com/questions/58080154/aws-eb-init-not-a-valid-key-value-pair-missing-equal-sign



Securing Your Django Application: Enforcing HTTPS with AWS Elastic Beanstalk, ACM SSL Certificates, and Amazon Route 53

Introduction: This concise guide outlines the necessary steps to secure your Django application by enforcing HTTPS through the use of AWS Elastic Beanstalk, ACM SSL Certificates, and Amazon Route 53. By following these straightforward instructions, you'll ensure a secure and encrypted connection, providing an enhanced level of security for your web application.

Prerequisites:

  • A Django application deployed on AWS Elastic Beanstalk
  • Access to the AWS Management Console
  • An ACM (AWS Certificate Manager) SSL certificate created for your domain
  • Domain registered on GoDaddy (or any other domain registrar)

Steps to Enforce HTTPS:

Step 1: Create ACM SSL Certificate

1.1 Open the AWS Management Console and navigate to AWS Certificate Manager (ACM). 1.2 Request a new SSL certificate for your domain (e.g., https://www.dhirubhai.net/redir/invalid-link-page?url=example%2ecom). 1.3 Complete the ACM certificate validation process.

Step 2: Create a Hosted Zone on Route 53

2.1 Access Amazon Route 53 in the AWS Management Console. 2.2 Create a new hosted zone for your domain. 2.3 Note down the nameservers provided by Route 53.

Step 3: Update GoDaddy Nameservers

3.1 Log in to your GoDaddy account.

3.2 Navigate to the domain management section.

3.3 Replace the existing nameservers with the ones provided by AWS Route 53.

ns-1234.awsdns-12.org
ns-5678.awsdns-34.co.uk
ns-9012.awsdns-56.net
ns-3456.awsdns-78.com        

Step 4: Configure Route 53 for ACM Certificate

4.1 Return to Amazon Route 53 in the AWS Management Console. 4.2 Update DNS records by adding CNAMEs with values provided during ACM certificate validation.

Step 5: Update Elastic Beanstalk Environment

5.1 Open the AWS Elastic Beanstalk console. 5.2 Navigate to your environment and click on "Configuration." 5.3 Add a new listener for HTTPS (port 443). 5.4 Set the SSL certificate to the one you created in ACM. 5.5 Save the configuration changes.

Listener Port - 443

Step 6: Deploy Your Updated Application

7.1 Commit and push your code changes. 7.2 Deploy your updated Django application to Elastic Beanstalk.

Conclusion: Following these steps ensures your Django application is configured for HTTPS, creating a secure and encrypted environment for users. By incorporating AWS Elastic Beanstalk, ACM SSL Certificates, and Amazon Route 53, you've taken a comprehensive approach to enhance the overall security of your web application.


After this your https:// not secure website will convert into https:// secure
Sharvin Gavad

6x Hackathon Winner | Student @FRCRCE | Building @Xircls | AR Developer

1 年

Very Useful!!

Elvis Dodti

AI/Ml Developer | Backend developer

1 年

Glad to be of help!!

要查看或添加评论,请登录

ARYAN KYATHAM的更多文章

  • Tech toolkit for winning hackathons

    Tech toolkit for winning hackathons

    This toolkit consists of features/tech we integrated which helped me win 8+ hackathons.?? Ranging from AI, openCV…

    24 条评论
  • Deploying a Flask App to GCP using Docker and CI/CD

    Deploying a Flask App to GCP using Docker and CI/CD

    Step 1: Create requirements.txt First, list the dependencies for your Flask application in a file: Step 2: Create…

    15 条评论
  • How to Dockerize Your Django Application

    How to Dockerize Your Django Application

    1. Install Docker Desktop: Download Docker Desktop from docker.

    3 条评论
  • AWS - Networking, Compute, Storage

    AWS - Networking, Compute, Storage

    Full article Here : Link, ? the repo :) Earlier, people used to set up rack servers manually on-premises, which was…

    3 条评论
  • Python fundamentals 1 shot

    Python fundamentals 1 shot

    Full Blog : Click Here ?? Python is a versatile programming language that supports both Object-Oriented Programming…

    7 条评论
  • How to win hackathons ?

    How to win hackathons ?

    Build awesome team Your vibe should match not only the skills. Don’t team up with people with whom you ain’t…

    10 条评论
  • AI Powered Developer Guru

    AI Powered Developer Guru

    In software development, a significant portion of a developer's time—up to 60%—is spent not in writing code, but in…

  • Deploy dJango app on AWS using EC2, RDS, S3 !

    Deploy dJango app on AWS using EC2, RDS, S3 !

    ?? Launch Django Project on AWS with EC2, S3, RDS, Nginx, Gunicorn! ?? What is EC2 Instance ? : EC2 allows us to…

    1 条评论
  • Revolutionizing Billboard Analytics with Artificial Intelligence & Data Science.

    Revolutionizing Billboard Analytics with Artificial Intelligence & Data Science.

    What is Billboard Analytics ? Billboard analytics is the process of analyzing data related to outdoor advertising, such…

    3 条评论