Deploying Django on AWS Lambda with Zappa: A Serverless Computing Approach

Serverless Computing

Serverless computing is a cloud-computing execution model where the cloud provider runs the server, and dynamically manages the allocation of machine resources, and usage charges rather than a pre-purchased unit of capacity. It’s a great way to build and run applications and services without thinking about servers.

Advantages:

  • Cost-efficient: Pay only for the actual compute time used.
  • Scalability: Automatically scales up or down based on demand.
  • Simplified operations: Eliminates the need for server provisioning, maintenance, and management.
  • High Availability and Fault Tolerance: It offers built-in redundancy and fault tolerance mechanisms.

Disadvantages:

  • Understanding Cold Start Latency: Cold Start Latency initial invocation of functions can experience latency known as cold start latency.
  • Cost Considerations: This can become more expensive than traditional servers if the traffic is too high.
  • Visibility Challenges: Limited visibility into the underlying infrastructure and execution environment can make monitoring, debugging, and performance tuning more challenging.

Zappa

Zappa is an open-source tool that simplifies the process of deploying Python applications to AWS Lambda. Zappa handles all the packaging, deployment, and scaling of your application, making it easy to deploy web applications that can handle high levels of traffic without managing any servers.

Let us go through the step-by-step process of deploying a Django application on AWS Lambda using Zappa.

Prerequisites

  1. Python virtual environment with Zappa installed.
  2. A Django application ready for deployment including a dependency requirements file.
  3. An AWS account with access to AWS Lambda, API Gateway, S3, and IAM.
  4. The AWS CLI is installed and configured with your AWS credentials.

Step 1: Create an IAM User

Create an IAM user on your AWS account.

Create-IAM-User

Create and attach a policy with the following permissions to the IAM user.

{
"Version": "2024-01-24",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iam:AttachRolePolicy",
"iam:GetRole",
"iam:CreateRole",
"iam:PassRole",
"iam:PutRolePolicy"
],
"Resource": [
"arn:aws:iam::<ACCOUNT_ID>:role/*-ZappaLambdaExecutionRole"
]
},
{
"Effect": "Allow",
"Action": [
"lambda:CreateFunction",
"lambda:ListVersionsByFunction",
"logs:DescribeLogStreams",
"events:PutRule",
"lambda:GetFunctionConfiguration",
"cloudformation:DescribeStackResource",
"apigateway:DELETE",
"apigateway:UpdateRestApiPolicy",
"events:ListRuleNamesByTarget",
"apigateway:PATCH",
"events:ListRules",
"cloudformation:UpdateStack",
"lambda:DeleteFunction",
"events:RemoveTargets",
"logs:FilterLogEvents",
"apigateway:GET",
"lambda:GetAlias",
"events:ListTargetsByRule",
"cloudformation:ListStackResources",
"events:DescribeRule",
"logs:DeleteLogGroup",
"apigateway:PUT",
"lambda:InvokeFunction",
"lambda:GetFunction",
"lambda:UpdateFunctionConfiguration",
"cloudformation:DescribeStacks",
"lambda:UpdateFunctionCode",
"events:DeleteRule",
"events:PutTargets",
"lambda:AddPermission",
"cloudformation:CreateStack",
"cloudformation:DeleteStack",
"apigateway:POST",
"lambda:RemovePermission",
"lambda:GetPolicy"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucketMultipartUploads",
"s3:CreateBucket",
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::zappa-*"
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:AbortMultipartUpload",
"s3:DeleteObject",
"s3:ListMultipartUploadParts"
],
"Resource": "arn:aws:s3:::zappa-*/*"
}
]
}
  • Generate and collect the access and secret keys for the IAM user.
  • Set up a profile in the AWS CLI on the development machine using the access and secret keys of the IAM user.
$ aws configure
AWS Access Key ID [None]: <access_key>
AWS Secret Access Key [None]: <secret_key>
Default region name [None]: <default_region>
Default output format [None]: json
  • Creating a separate IAM user for the deployment ensures a secure setup with the minimum required permissions for the deployment operations.

Step 2: Prepare Your Django Application

  • Create a virtual environment within the Django project directory and activate it. Then, install Django and Zappa in this environment to prepare for deploying on AWS Lambda.
  • Make sure the Django application is ready for serverless deployment. This includes settings for static and media files, database configurations, and any middleware adjustments required for a serverless environment.
  • Run zappa init to generate a “zappa_settings.json” file.
$ zappa init
  • This file contains the environment configurations for your Zappa deployments. The configuration parameters include:

▪️ AWS region: The AWS region where the project resources will be deployed.
▪️ Django settings: Path for the Django settings file.
▪️ Profile name: AWS CLI profile name.
▪️ Project name: Django project name.
▪️ Runtime: Python runtime.
▪️ S3 bucket: Name of AWS S3 bucket which will be created by Zappa if not already present for storing the source code until deployment is complete.

{
"dev": {
"aws_region": "us-west-1",
"django_settings": "project_name.settings",
"profile_name": "default",
"project_name": "project_name",
"runtime": "python3.8",
"s3_bucket": "zappa-django"
}
}

Begin Your Serverless Journey Today and Transform Your Deployment Experience!

Step 3: Deploy the Application

  • The following command deploys our application to the stage specified in our Zappa configuration file.
$ zappa deploy dev
  • Zappa packages the application, uploads it to an S3 bucket, creates a Lambda function, and sets up an API Gateway to handle HTTP requests.
  • If any changes are made to the application source code, we can update our AWS Lambda deployment with Zappa by running.
$ zappa update dev
  • AWS provides monitoring tools like CloudWatch to track the performance of Lambda functions. Zappa also logs output to CloudWatch, allowing us to monitor the logs for errors or issues.
  • We can also automate the deployment process by configuring a CI/CD pipeline to run a deployment script (yaml) such as:
name: Deploy to AWS Lambda

on:
push:
branches:
- dev

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.8'
- name: Setting up AWS Credentials
run: |
pip install awscli
aws configure set region <aws_region>
aws configure set output json
aws configure set aws_access_key_id ${{secrets.AWS_ACCESS_KEY}}
aws configure set aws_secret_access_key ${{secrets.AWS_SECRET_KEY}}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install virtualenv
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
- name: Deploy to AWS Lambda
run: |
source venv/bin/activate
zappa update dev

Best Practices

  • Keep Your Functions Warm: AWS Lambda can introduce latency if your function hasn’t been called recently, as it needs to start a new instance of your function. Zappa offers a keep_warm (true by default) option to periodically invoke your function, reducing latency.
  • Manage Dependencies Efficiently: Since AWS Lambda has a package size limit, it’s important to minimize the size of your deployment package. Only include necessary packages in your requirements.txt.
  • Security: Use AWS IAM roles and policies to give your Lambda function the minimum necessary permissions. Also, consider using environment variables or AWS Secrets Manager to manage sensitive information.
  • Database Management: Use AWS-managed services like RDS or Aurora Serverless for your database needs to leverage AWS’s scalability and management features alongside AWS Lambda.
  • Static and Media Files: Configure the Django application to serve static and media files from AWS S3.
  • Monitoring and Logging: Utilize AWS CloudWatch for monitoring and logging. Zappa automatically configures CloudWatch logging for your deployed applications on AWS Lambda.
coma

Conclusion

Deploying Django applications on AWS Lambda using Zappa offers a serverless solution that is scalable, cost-effective, and efficient. It allows developers to focus on building features rather than managing infrastructure, leveraging the benefits of serverless architecture. Zappa is an easy-to-use framework that reduces the deployment time and complexity to a great extent by automating most of the deployment tasks. With an automated CI/CD pipeline, it can further streamline the development and deployment process. Zappa can also be used with other Python-based frameworks such as Flask for serverless deployment.

Keep Reading

Keep Reading

Launch Faster with Low Cost: Master GTM with Pre-built Solutions in Our Webinar!

Register Today!
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?