This document describes how to quickly deploy a local Django project to the cloud through a HTTP-triggered Function.
Explanation
This document mainly describes how to deploy in the console. You can also complete the deployment on the command line. For more information, please see Deploying Framework on Command Line.
Template deployment: Quick deployment of Django project
1. Log in to the Serverless console and click on Function Service in the left navigation bar.
2. Select the region and namespace where to create a function at the top of the page and click Create to enter the function creation process.
3. Choose to create a new function using Template Creation. In the search box, enter Django, select the Django Framework Template, and click Next. As shown in the figure below:
4. On the Create page, you can view and modify the specific configuration information of the template project.
5. Click Complete. After creating the HTTP-triggered Function, you can view its basic information on the Function Management page.
6. Click on Trigger Management in the left sidebar, check the access path URL, and visit your deployed Django project. As shown in the figure below:
7. Click the access path URL to access the Django project.
Custom deployment: Quick migration of local project to cloud
Local development
1. Run the following command to confirm that Django has been installed in your local environment.
python -m pip install Django
2. Create the Hello World sample project locally.
django-admin startproject helloworld && cd helloworld
The directory structure is as follows:
$ tree
. manage.py Manager
|--***
||-- __init__.py Package
||-- settings.py Settings file
||-- urls.py Route
| `-- wsgi.py Deployment
3. Run the python manage.py runserver command locally to start the bootstrap file. Below is the sample code:
$ python manage.py runserver
July 27, 2021 - 11:52:20
Django version 3.2.5, using settings 'helloworld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
4. Open a browser and visit http://127.0.0.1:8000 to access the Django sample project locally. As shown in the figure below:
Deployment in cloud
You need to make simple modifications to the locally created project, so that the project can be quickly deployed through an HTTP-triggered function. The project modification steps for Django are as follows:
1. Install dependencies
1.1 As the Django dependency library is not provided in the standard cloud environment of SCF, you must install the dependencies and upload them together with the project code. Please create the requirements.txt file first with the following content:
Django==3.1.3
1.2 Run the following installation command:
pip install -r requirements.txt -t .
Explanation
As the initialized default project imports the db.sqlite3 library, please install this dependency synchronously or configure comments for the DATABASES field in the setting.py file of the project.
2. Add scf_bootstrap startup file
In the HTTP-triggered function, the listening port must be set to 9000. Therefore, you need to modify the listening address port. Create the scf_bootstrap startup file in the root directory of the project and add the following content to it (this file is used to configure environment variables, specify service start commands, and other custom operations to ensure that your service can be started normally through this file):
3. After the file is created, you need to run the following command to modify the executable permission of the file. By default, the permission 777 or 755 is required for the service to start normally. Below is the sample code:
chmod777 scf_bootstrap
Note
In the SCF environment, only files in the /tmp directory are readable/writable. We recommend you select /tmp when outputting files. If you select other directories, write will fail due to the lack of permissions.
If you want to output environment variables in logs, you need to add the -u parameter before the bootstrap command, such as python -u app.py.
4. After the local configuration is completed, run the following command to start the service (take running the command in the scf_bootstrap directory as an example) and make sure that your service can be normally started locally.
Note
When testing locally, be sure to change the Python path to the local path.
./scf_bootstrap
5. Log in to the Serverless console and click on Function Service in the left navigation bar.
6. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
7. Choose to start from scratch to create a new function, and configure the relevant options according to the page prompts.
Function Type: Select "HTTP-triggered Function".
Function name: Enter the name of your function.
Region: enter your function deployment region, such as Chengdu.
Runtime Environment: Select "Python3.6".
Submission Method: Select "Upload Local Folder" to upload your local project.
Function Code: select the specific local folder where the function code is.
8. Click Complete.
Development management
Upon completion of the deployment, you can quickly access and test your web service through the SCF console. You can also experience various unique features of cloud functions, such as layer binding, log management, and enjoy the advantages of a Serverless architecture, such as low cost and elastic scaling, as shown below: