DEV Community

Tapas SInghal for AWS Community Builders

Posted on

Project - Implement Blue/Green deployment of Python (Django) Application on AWS Elastic Beanstalk

buddydome

Image description

Result:- www.buddydome.cloud
Github Repo:- https://github.com/Shocker-lov-t/buddydome


Major AWS Services used :-

  • RDS
  • S3
  • ELASTICBEANSTALK
  • ROUTE53

Step-1: Create an AWS database instance of postgreSql database ( Compulsory in both )

    -Login to AWS console and head toward for RDS Service
    -Launch an Postgresql Database instance and note down the username,master username and password of the instance ( it will be shown one time only so make sure to note them down very carefully)
    -Wait for the database instance to be in running state ( Takes approximately 10 min)
    -After Launched, click on the instance and it will show the details of your database
    -Open Django project's Settings.py file, there you will see a section "DATABASE" IN LINE 85 
          >>> Change name to the username to saved earlier
          >>> Change Username to master username
          >>> Change Password to new Password
          >>> Change Host to new host which is shown exactly on aws console database instance page
    -SAVE CHANGES
Enter fullscreen mode Exit fullscreen mode

Step-2: (Follow only if you are working with BuddyDome with s3 only otherwise ignore this step) Setting up S3 bucket

Create an s3 bucket ( with unchecking the box of " Block all public access" )

Change the bucket policy as follows

//S3 Policy//

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::s3-bucket/*"
        }
    ]
} 
Enter fullscreen mode Exit fullscreen mode

Create an IAM user

        >>>In AWS Look for IAM service 
        >>>Create an IAM user with S3 Policy
        >>>Generate Access key and secret key and note them down
Enter fullscreen mode Exit fullscreen mode

Go to your Django project and first install two libraries using following command

         pip install boto3 django-storages
Enter fullscreen mode Exit fullscreen mode

Add Following in settings.py file

          import boto3
          from storages.backends.s3boto3 import S3Boto3Storage


          INSTALLED_APPS = [ 
                  'storages'
                  ]

          //Write Corresponding Values//
          AWS_ACCESS_KEY_ID =
          AWS_SECRET_ACCESS_KEY = 
          AWS_STORAGE_BUCKET_NAME = 
          AWS_S3_REGION_NAME='ap-south-1'

          AWS_QUERYSTRING_AUTH = False


          # Set the static files storage backend to S3
          STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"

          # Set the URL for static files
          STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/"

          AWS_LOCATION = 'media'

          DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

          # Set the URL for media files
          MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/{AWS_LOCATION}/"

          STATIC_ROOT = "https://s3.amazonaws.com/my-static-bucket/"
          MEDIA_ROOT = "https://s3.amazonaws.com/my-media-bucket/"
Enter fullscreen mode Exit fullscreen mode

Some Changes in all urls.py file

//urls.py//

          from Buddy import settings
          from django.conf.urls.static import static
Enter fullscreen mode Exit fullscreen mode

//In URL_patterns ending Square bracket Add below code as it is//

           +static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Enter fullscreen mode Exit fullscreen mode

Step-3: Deployment Using AWS Elastic Beanstalk( Compulsory in Both)

Install Elastic Beanstalk CLI using following:-

     pip install awsebcli
Enter fullscreen mode Exit fullscreen mode

Create a Config file to speicify settings file and .conf file to specify upload limit for your elasticbeasntalk environment in following format

    ~/BuddyDome/
    |-- .ebextensions
    |   `-- django.config
    |-- .platform
    |   --nginx
    |        --conf.d
    |                `--proxy.conf
Enter fullscreen mode Exit fullscreen mode

Write the following in django.config file

    container_commands:
      01_collectstatic:
        command: "source $PYTHONPATH/activate && python manage.py collectstatic --noinput"
    option_settings:
      aws:elasticbeanstalk:container:python:
        WSGIPath: Buddy.wsgi:application
      aws:elasticbeanstalk:environment:proxy:staticfiles:
        static: "static"
Enter fullscreen mode Exit fullscreen mode

Write the Following in proxy.conf

    client_max_body_size 400M;        
Enter fullscreen mode Exit fullscreen mode

In terminal initialize the application for elasticbeanstalk using awsebcli

Make sure you are in main directory where you can access manage.py file ( Use dir or ls command to verify)

    eb init -i

    Select a default region 
    (Default is 3): 6  

    application name: Buddydome

    Enter Application Name
    (default is "BuddyDome "): BuddyDome

    Select a platform
    (make a selection): 9       //Python

    Select a platform branch. 3
Enter fullscreen mode Exit fullscreen mode

In Terminal Create Elastic beanstalk environment:-

    eb create Buddydome-env
Enter fullscreen mode Exit fullscreen mode

use the following to get the status and to open the deployed application:-

    eb status
    eb open
Enter fullscreen mode Exit fullscreen mode

Use the following only in case you make some changes after environment creation


    eb deploy
Enter fullscreen mode Exit fullscreen mode




Step-4 : Setting Up DNS (Route53 and Hostinger)

  1. Go to Route53 and create a "Hosted Zone" with corresponding Hostinger domain name ( e.g. buddydome.cloud)
  2. Change Hostinger Nameservers to Route53 Generated Nameservers (changes might take upto 24hr)
  3. In Hosted Zone, create a "A" type Record
    • Leave Subdomain as blank
    • Toggle "Alias"
    • In Endpoint, choose Alias to "Elastic Beanstalk environment"
    • CHoose Corresponding Region, ap-south-1 "Asia Pacific (Mumbai)"
    • Choose Environment, select the corresponding elasticbeanstalk environment that directs to Buddydome Project
    • Click "Add another record"
    • > > Write "www" in subdomain
    • > > Choose Type as "CNAME"
    • > > In Value write original domain i.e. buddydome.cloud
    • Click "Create Records"

Note:- You can add more subdomains similarly by adding another record




With this your application has been successfully deployed and hosted with a Domain

Top comments (4)

Collapse
 
yogini16 profile image
yogini16

Thank you for sharing.
Blue green deployment approach allows for seamless updates and rollbacks, minimizing downtime and ensuring reliability in the software deployment process.

Collapse
 
its_tapas_ig profile image
Tapas SInghal

Precisely, and plus if something goes wrong you can easily go back and switch to an older version of the application.😎

Collapse
 
weatherinmay profile image
weatherInMay • Edited

I've shared this post of yours on 1024.

Collapse
 
its_tapas_ig profile image
Tapas SInghal

Thank you for the support.