<?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: Mahir Mahbub</title>
    <description>The latest articles on DEV Community by Mahir Mahbub (@sandboa).</description>
    <link>https://dev.to/sandboa</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%2F937030%2Fcc6a5a08-ef14-402d-be6b-2980c4c3e2fd.jpeg</url>
      <title>DEV Community: Mahir Mahbub</title>
      <link>https://dev.to/sandboa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sandboa"/>
    <language>en</language>
    <item>
      <title>Deploying Django + Celery + Amazon SQS to AWS Elastic Beanstalk with Amazon Linux 2</title>
      <dc:creator>Mahir Mahbub</dc:creator>
      <pubDate>Tue, 25 Oct 2022 20:11:00 +0000</pubDate>
      <link>https://dev.to/sandboa/deploying-django-celery-amazon-sqs-to-aws-elastic-beanstalk-with-amazon-linux-2-442m</link>
      <guid>https://dev.to/sandboa/deploying-django-celery-amazon-sqs-to-aws-elastic-beanstalk-with-amazon-linux-2-442m</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;In this tutorial, we will learn about the configuration for deploying Django+Celery+Amazon SQS to Amazon Elastic Beanstalk with Amazon Linux 2.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I will assume here that you already familiar with Django/DRF and have running Django/DRF application. I will also assume that you know how to create a Beanstalk application with Amazon Linux 2 image.&lt;/p&gt;

&lt;p&gt;Let's drive into the configuration.&lt;/p&gt;

&lt;p&gt;The first step into getting your application to run in Beanstalk is to create the &lt;strong&gt;".ebextensions"&lt;/strong&gt; folder in the root of the source. &lt;br&gt;
Now create a file named &lt;strong&gt;"02_package.config"&lt;/strong&gt; add the code into that file showed in snippet below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;packages:
  yum:
    libcurl-devel: []
    openssl-static.x86_64: []
    python3-devel: []
    gcc: []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the openssl-static.x86_64, libcurl-devel, gcc are required for installing pycurl which will be used to connect with Amazon SQS.&lt;/p&gt;

&lt;p&gt;To run DB migration and install python required packages, add the snippet below to a file named &lt;strong&gt;"04_db_migrate.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;container_commands:
  01_install_requirements:
    command: "source /var/app/venv/*/bin/activate &amp;amp;&amp;amp; pip3 install -r requirements.txt"
    leader_only: true
  02_migrate:
    command: "ls /var/app/venv/*/bin &amp;amp;&amp;amp; source /var/app/venv/*/bin/activate &amp;amp;&amp;amp; python3 manage.py migrate"
    leader_only: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then It is needed to configure the Beanstalk to find/work with the Django settings and run the WSGI server. So create a file name &lt;strong&gt;"05_django.config"&lt;/strong&gt; and add the code snippet to the file.&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: config.wsgi:application

  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: config.settings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now It is needed to define a pre-deployment action through the hook which used by many Amazon services for controlled action execution. the file named &lt;strong&gt;"03_pycurl_reinstall.sh"&lt;/strong&gt; is needed to be created under the folder path &lt;strong&gt;".platform/hook/predeploy"&lt;/strong&gt;. It will reinstall the pycurl after installing the required yum packages mentioned above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source ${PYTHONPATH}/activate &amp;amp;&amp;amp; ${PYTHONPATH}/pip3 install pycurl --global-option="--with-openssl" --upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now It is needed to create a file named &lt;strong&gt;"01_create_celery_service.sh"&lt;/strong&gt; in &lt;strong&gt;".platform/hook/postdeploy"&lt;/strong&gt; folder and add the code snippet below. The Celery will be configured with this service file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env bash

echo "[Unit]
Name=Celery
Description=Celery service for __
After=network.target
StartLimitInterval=0
[Service]
Type=simple
Restart=always
RestartSec=30
User=root
WorkingDirectory=/var/app/current
ExecStart=$PYTHONPATH/celery -A config worker --loglevel=INFO 
ExecReload=$PYTHONPATH/celery -A config worker --loglevel=INFO 
EnvironmentFile=/opt/elasticbeanstalk/deployment/env
[Install]
WantedBy=multi-user.target
" | tee /etc/systemd/system/celery.service

# Start celery service
systemctl start celery.service

# Enable celery service to load on system start
systemctl enable celery.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, the AWS compulsory config along with Amazon SQS configuration is needed to be added to the settings.py file under the config folder of the Django app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# AWS config
AWS_ACCESS_KEY_ID = config("AWS_ACCESS_KEY_ID", "")
AWS_SECRET_ACCESS_KEY = config("AWS_SECRET_ACCESS_KEY", "")

# CELERY SETTINGS
BROKER_URL = f"sqs://{AWS_ACCESS_KEY_ID}:{AWS_SECRET_ACCESS_KEY}@"

BROKER_TRANSPORT_OPTIONS = {
    "region": &amp;lt;region name&amp;gt;, # example ap-southeast-2
    "polling_interval": 60,
    "visibility_timeout": 3600,
    "queue_name_prefix": &amp;lt;app-name&amp;gt;,
}
CELERY_DEFAULT_QUEUE = "sqs"
CELERY_ACCEPT_CONTENT = ["application/json"]
CELERY_TASK_SERIALIZER = "json"
CELERY_RESULT_SERIALIZER = "json"
CELERY_BROKER_TRANSPORT_OPTIONS = BROKER_TRANSPORT_OPTIONS
CELERY_TASK_DEFAULT_QUEUE = "sqs"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Predefined queue do not work with pooling so It has been avoided. Now zip the source code and upload to Elastic Beanstalk. Alternatively you can use AWS console or command line interface to deploy the application. Now we have successfully configured the application with celery and SQS in Elastic Beanstalk with Amazon Linux 2. You can check the log file to check the status of the celery. Also a new queue will be created in AWS SQS console.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>beanstalk</category>
      <category>sqs</category>
      <category>celery</category>
    </item>
  </channel>
</rss>
