<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arpit-Gulgulia</title>
    <description>The latest articles on DEV Community by Arpit-Gulgulia (@arpitgulgulia).</description>
    <link>https://dev.to/arpitgulgulia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F389158%2F2bb2ab98-2be0-40d5-adab-06a00c76f43b.png</url>
      <title>DEV Community: Arpit-Gulgulia</title>
      <link>https://dev.to/arpitgulgulia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arpitgulgulia"/>
    <language>en</language>
    <item>
      <title>Step by step guide to deploy a Django application to Elastic Beanstalk - AWS Elastic Beanstalk</title>
      <dc:creator>Arpit-Gulgulia</dc:creator>
      <pubDate>Sat, 02 Dec 2023 19:19:17 +0000</pubDate>
      <link>https://dev.to/arpitgulgulia/step-by-step-guide-to-deploy-a-django-application-to-elastic-beanstalk-aws-elastic-beanstalk-19cb</link>
      <guid>https://dev.to/arpitgulgulia/step-by-step-guide-to-deploy-a-django-application-to-elastic-beanstalk-aws-elastic-beanstalk-19cb</guid>
      <description>&lt;h2&gt;
  
  
  Deploying a Django application to Elastic Beanstalk
&lt;/h2&gt;

&lt;p&gt;This article walks through the deployment of a simple Django application to an AWS Elastic Beanstalk environment running Python.&lt;/p&gt;

&lt;p&gt;In this article , you’ll do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a Python virtual environment and install Django&lt;/li&gt;
&lt;li&gt;Create a Django project&lt;/li&gt;
&lt;li&gt;Configure your Django application for Elastic Beanstalk&lt;/li&gt;
&lt;li&gt;Deploy your site with the EB CLI&lt;/li&gt;
&lt;li&gt;Update your application&lt;/li&gt;
&lt;li&gt;Clean up&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also watch the below video and follow along:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.youtube.com/watch?si=XIpFTSW-6BkyYCTK&amp;amp;v=eH0hTPufZcY&amp;amp;feature=youtu.be" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--pjGvHpAH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ytimg.com/vi/eH0hTPufZcY/maxresdefault.jpg%3Fsqp%3D-oaymwEmCIAKENAF8quKqQMa8AEB-AH-CYAC0AWKAgwIABABGFUgXChlMA8%3D%26rs%3DAOn4CLBNRhtAJr10YFpEFAbbT90oHRcT1w" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.youtube.com/watch?si=XIpFTSW-6BkyYCTK&amp;amp;v=eH0hTPufZcY&amp;amp;feature=youtu.be" rel="noopener noreferrer" class="c-link"&gt;
          Step by step guide to deploy a Django application to Elastic Beanstalk -  AWS Elastic Beanstalk - YouTube
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          This video walks you through the deployment of a Django website to an AWS Elastic Beanstalk environment running Python. This tutorial shows you how to host a...
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--XfMXSFy2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://www.youtube.com/s/desktop/2a835f78/img/favicon.ico" width="16" height="16"&gt;
        youtube.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow this tutorial, you should have all of the Common Prerequisites for Python installed, including the following packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python 3.7 or later&lt;/li&gt;
&lt;li&gt;pip&lt;/li&gt;
&lt;li&gt;virtualenv&lt;/li&gt;
&lt;li&gt;awsebcli&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have already installed all these packages in my local dev environment (Ubuntu).&lt;/p&gt;

&lt;h2&gt;
  
  
  Create and Run a sample Django application locally
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a virtual environment named eb-virt:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;virtualenv ~/eb-virt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Activate the virtual environment:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source ~/eb-virt/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll see (eb-virt) prepended to your command prompt, indicating that you're in a virtual environment.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;pip&lt;/code&gt;to install Django:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(eb-virt)~$ pip install django==2.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To verify that Django is installed, enter the following:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(eb-virt)~$ pip freeze
Django==2.2
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Use the &lt;code&gt;django-admin startproject&lt;/code&gt; command to create a Django project named &lt;code&gt;ebdjango&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(eb-virt)~$ django-admin startproject ebdjango
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Open the &lt;code&gt;settings.py&lt;/code&gt; file in the &lt;code&gt;ebdjango&lt;/code&gt; directory. Locate the &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt; setting, and then add your localhost(&lt;code&gt;127.0.0.1&lt;/code&gt;) domain name:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
ALLOWED_HOSTS = ['127.0.0.1']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run your Django site locally with &lt;code&gt;manage.py runserver&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(eb-virt) ~$ cd ebdjango
(eb-virt) ~/ebdjango$ python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In a web browser, open &lt;a href="http://127.0.0.1:8000/"&gt;http://127.0.0.1:8000/&lt;/a&gt; to view the site.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check the server log to see the output from your request. To stop the web server and return to your virtual environment, press &lt;code&gt;Ctrl+C&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Django version 2.2, using settings 'ebdjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[07/Sep/2018 20:14:09] "GET / HTTP/1.1" 200 16348
Ctrl+C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure and Deploy your Django application to Elastic Beanstalk with the EB CLI
&lt;/h2&gt;

&lt;p&gt;Now that you have a Django-powered site on your local machine, you can configure it for deployment with Elastic Beanstalk.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;pip freeze&lt;/code&gt;, and then save the output to a file named &lt;code&gt;requirements.txt&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(eb-virt) ~/ebdjango$ pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elastic Beanstalk uses &lt;code&gt;requirements.txt&lt;/code&gt; to determine which package to install on the EC2 instances that run your application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a directory named &lt;code&gt;.ebextensions&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(eb-virt) ~/ebdjango$ mkdir .ebextensions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;In the &lt;code&gt;.ebextensions&lt;/code&gt; directory, add a &lt;strong&gt;configuration file&lt;/strong&gt; named &lt;code&gt;django.config&lt;/code&gt; with the following text:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example: ~/ebdjango/.ebextensions/django.config&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now you've added everything you need to deploy your application on Elastic Beanstalk. Your project directory should now look like this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/ebdjango/
|-- .ebextensions
|   `-- django.config
|-- ebdjango
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- db.sqlite3
|-- manage.py
`-- requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initialize your EB CLI repository with the &lt;code&gt;eb init&lt;/code&gt; command:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/ebdjango$ eb init -p python-3.7 django-tutorial
Application django-tutorial has been created.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create an application named &lt;code&gt;django-tutorial&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an environment and deploy your application to it with &lt;code&gt;eb create&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/ebdjango$ eb create django-env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;If you see a "service role required" error message, run eb create interactively (without specifying an environment name) and the EB CLI creates the role for you.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/a/76620598/12326605"&gt;StakeOverflow - The instance profile aws-elasticbeanstalk-ec2-role associated with the environment does not exist.&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above command creates a load-balanced Elastic Beanstalk environment named &lt;code&gt;django-env&lt;/code&gt;. Creating an environment takes about 5 minutes. As Elastic Beanstalk creates the resources needed to run your application, it outputs informational messages that the EB CLI relays to your terminal.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the environment creation process completes, find the domain name of your new environment by running &lt;strong&gt;eb status&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/ebdjango$ eb status
Environment details for: django-env
  Application name: django-tutorial
  ...
  CNAME: eb-django-app-dev.elasticbeanstalk.com
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here note, Your environment's domain name is the value of the &lt;em&gt;CNAME&lt;/em&gt; property.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the &lt;code&gt;settings.py&lt;/code&gt; file in the &lt;code&gt;ebdjango&lt;/code&gt; directory. Locate the &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt; setting, and then add your application's domain name that you found in the previous step to the setting's value. If you can't find this setting in the file, add it to a new line:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
ALLOWED_HOSTS = ['eb-django-app-dev.elasticbeanstalk.com']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Save the file, and then deploy your application by running &lt;strong&gt;eb deploy&lt;/strong&gt;. When you run &lt;code&gt;eb deploy&lt;/code&gt;, the EB CLI bundles up the contents of your project directory and deploys it to your environment.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/ebdjango$ eb deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When the environment update process completes, open your website with &lt;strong&gt;eb open&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~/ebdjango$ eb open
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This opens a browser window using the domain name created for your application. You should see the same Django website that you created and tested locally.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>elasticbeanstalk</category>
      <category>django</category>
      <category>python</category>
    </item>
  </channel>
</rss>
