Post-Deployment Steps

We’ve deployed the application in Lambda now, but still, there are a few more things to do, here we interact with production entities directly.

Configure the local system to interact with the production

To interact with the production deployment, we need to configure our local environment, like settings environment variable DJANGO_SETTINGS_MODULE  (todo.settings.prod), and also other required environment variables.

Create a file with the name env_vars.sh in the project home directory with the below content (update values appropriately)

export DJANGO_SETTINGS_MODULE=todo.settings.prod
export SECRET_KEY=mys3cr3tkey
export SQL_DB_NAME=my_db_name
export SQL_DB_USERNAME=my_db_username
export SQL_DB_PASSWORD=TLQahsnbKAF9sOmI
export SQL_DB_HOST=example.com
export SQL_DB_PORT=3306
export USE_S3_STATIC=True
export CUSTOM_AWS_ACCESS_KEY_ID=ABCDEDFGHIJKL
export CUSTOM_AWS_SECRET_ACCESS_KEY=abcdefghijkl
export AWS_STORAGE_BUCKET_NAME=my-storage-bucket

Then run the below command to set those environment variables, so that further commands will make use of these commands.

source env_vars.sh

Apply Migrations

Run the below command to apply migrations, these migrations will be applied in the DB server we configured in environment variables.

Apply Database Migrations 

Deploy Static files

Run the below command to push required static (HTML templates, CSS, js, images, ) files to the AWS S3 Bucket.

Collect required static files to S3

Access Application

You can now access your application by using the URL shown in the deployment.

Update Deployment

We can use the below command if we wanna update our existing deployment.

zappa update <stage>

Update Django Application Deployment

Delete Deployment

If you wanna delete your deployed application, run the below command, which will remove all the entities created for this deployment.

zappa undeploy <stage>

Undeploy Django Application

Conclusion: In this article, we showed you how to deploy a Django application using Zappa. Zappa makes it easy to deploy and manage your Django applications on AWS Lambda and API Gateway and provides a convenient way to keep your deployments up-to-date. Whether you’re just starting out with Django or looking to scale your existing application, deploying with Zappa is a great option.



How to Deploy Django Application in AWS Lambda?

Pre-requisite: AWS, Python

Django is a Python web framework that makes it easy to build web applications quickly and securely. It has a large and helpful community that provides support and contributes to its development.

AWS Lambda is a serverless computing platform that runs your code in Docker containers, provides us with the best possible speed, and there is a number of third-party apps, and tools that make Lambda usage much easier, in this tutorial we gonna use one such tool.

Django Application Deployment using AWS Lambda

Similar Reads

Why We Choose AWS Lambda

There are several reasons why we choose AWS Lambda over other choices like AWS EC2, Elastic Bean Stalk, etc for deploying our Django application:...

Zappa

Zappa is a serverless framework for deploying python applications using AWS Lambda and API Gateway. Zappa helps us to manage our Django applications easily and provides a convenient way to keep your deployments up-to-date. Zappa has a large and active community that provides support and contributes to its development, making it a great choice for deploying your Django applications. Prerequisites:...

Setting up a Django Project

1. Create and activate a Virtual environment for this Django project (ignore if already have  one)...

Initialize Zappa

1. In your Django project’s root directory, run the following command:...

Configure Database

For local development, we can use SQLite, but in general, it’s better to use MySQL server for better performance in large-scale applications, here in this case we used AWS RDS as a DB server....

Setup Static Files Hosting

...

Deploy Application

Static files are essential files that don’t need any dynamic changes, like static HTML, CSS, js, fonts, etc....

Update Environment Variables

We are all set for deployment, we configured our app with the required things, and now we can deploy our application to AWS Lambda....

Post-Deployment Steps

Make sure you set the proper environment variables required for deployment in set_env_vars.py, and run it after deploying the application to set env vars for your application. After updating the env vars in set_env_vars.py Run the below command to set/update env variables for our application....