Deploying a Serverless Django Application Using Zappa

Deploying a Serverless Django Application Using Zappa

Introduction to Serverless Architecture

Serverless architecture allows applications to run on cloud platforms without the need for server management. This approach lets developers focus solely on writing code while the cloud provider handles responsibilities like scaling, provisioning, upgrades, and security patches.

Why Choose Zappa?

Zappa is designed to build and deploy serverless Python applications on AWS Lambda and API Gateway. It excels in deploying:

  • Serverless Python microservices with frameworks like Flask.
  • Large web applications and Content Management Systems with Django.

Prerequisites

Before starting, ensure you have:

  • An active AWS account
  • AWS CLI installed
  • Python 3.12.7 installed
  • Access to AWS services, including:

Architecture Overview

Serverless deployment architecture connects your Django application through API Gateway and Lambda to AWS services like RDS for database functionality, with S3 and CloudFront handling frontend delivery.

Database Configuration

Modify your Django settings.py file to handle PostgreSQL RDS in AWS:

DATABASES = {

    'default': {

        'ENGINE': 'django_db_cascade.backends.postgresql_psycopg2',

        'NAME': 'your-database-name',

        'USER': 'your-master-username',

        'PASSWORD': 'your-master-password',

        'HOST': 'cluster-endpoint-url',

        'PORT': 5432, # default for PostgreSQL

    }

}        

Setting Up the Environment:

1. Configuring AWS:

Before using AWS, we must configure an AWS environment on our local machine to ensure Zappa functions correctly.

?????aws configure        

2. Create a Python virtual environment using the following command

????????python3 -m venv myenv        

3. Activate the virtual environment using the command

???????source myenv/bin/activate        

4. In our virtual environment, we need to install the requirement.txt, which is necessary for the project

? ? Pip3 install -r requirement.txt        

5 . Configuring Zappa in Django Application:

After setting up AWS credentials, the next step is configuring the Zappa package in your Django application. To do this, navigate to your Django app directory and run the following commands

  • Installing? Zappa?

???????????pip install zappa        

  • After installing, we need to configure Zappa using ? ?

            ? zappa init        

  • We need to provide the environment as dev or production or staging
  • We need to give the s3 bucket for Zappa to store the package temporarily ?

  • We need to provide the project settings we are leaving as default

  • It will ask us to deploy this application globally, we are not deploying globally?

  • It will show us the zappa_settings.json, it looks like below.

  • Press yes to deploy, then the application init will be successfully done.

  • To deploy the application, we need to use the following command?

? ? ? ? ? ? ? ? Zappa deploy dev        

  • If we need to update the changes, we need to use the following

? ? ? ? ? ? ? ? Zappa update dev        

Note: Once you have deployed the Zappa, it will be like this?

  • Once the Zappa deployment is completed, log in to the AWS account and check if lambda and API gateway are created or not
  • The below picture shows the API gateway deployed

  • This shows the lambda function deployed

  • ?We need to create the methods in the API gateway to handle the GET and POST requests for the database

  • Let's create a GET method. Click on the create method which is shown in the above picture

  • We need to define the integration type method for AWS Lambda

  • After selecting the integration type, give the lambda function that you have defined and click on create method

Note: after creating every method, we need to deploy the API as shown in the below figure

  • Before deploying the API, we need to define the stage and then deploy

  • Similarly, create PUT and POST method

Note: We need to deploy all the methods.

  • After creating all the methods, and deploying them, we need to enable the CORS, which is in the right corner of the image shown below

  • Now, let's host the front end in the S3 bucket. Go to our frontend project directory, and we need to add the API Gateway endpoint, google_map_apikey and google_map_id into the .env and .env.production files to interact with the backend.
  • To get the API gateway endpoint, go to API gateway and the dashboard

the

  • picture below shows the files that have been added

  • To host on S3, we need to build using the npm build the ui directory, and after building, we will be getting the dist directory we need to upload into the s3 bucket

  • We need to interact with the S3 bucket with CloudFront, so CloudFront will forward all the incoming requests.? Now, let's create a CloudFront distribution


  • In the origin path, we need to provide the integration as s3

  • We need to define origin access as origin access control

  • We need to give the HTTP methods below

  • We need to disable the application firewall

  • In the default root, please give index.html

  • Then create distribution

Note: We need to update the s3 bucket policy, so copy the policy in the below picture and paste that into the S3 bucket

  • Now paste the s3 bucket policy in the s3 so go to s3 bucket and then permission

  • Now edit the bucket policy and then save

  • After deploying the CloudFront, if we want to check the workflow of the CloudFront, copy the endpoint of CloudFront

  • Hit the endpoint in any browser, we will get the UI

6. Integrating The Database With Your Application:?

  • Every application needs a database, and for ours, we’ll use AWS Aurora Serverless v2—a PostgreSQL-compatible, serverless database solution provided by AWS.
  • Run the following command to add the user to the database from your project directory ?

zappa manage dev migrate ? ? ?         

  • Now load the data into the database using the following command ? ? ? ?

zappa manage dev loaddata accounts.json        

  • After successfully loading the data into the database, we can log in to login into ui

I hope this article has provided you with valuable insights into the topic at hand. Whether you're a beginner or an experienced professional, there's always something new to learn and discover in the world. By sharing my knowledge and experience with you, I hope to have sparked your curiosity and inspired you to explore further. Thank you for taking the time to read this article, and I look forward to hearing your thoughts and feedback.


Written By,

DevOps Team


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

Buildbot Technologies Private Limited的更多文章