Deploying Django on AWS Elastic Beanstalk
ARYAN KYATHAM
Impact Kid | 8x Hackathon Winner | Software Engineer | Startups & AI | Student @FRCRCE |
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:
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
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'.
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:
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.
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
6x Hackathon Winner | Student @FRCRCE | Building @Xircls | AR Developer
1 年Very Useful!!
AI/Ml Developer | Backend developer
1 年Glad to be of help!!