The SLS framework deployment scheme has been upgraded. You can use an SCF HTTP-triggered function to quickly deploy your Django service to the cloud.
Note:
What are the differences between SLS console deployment and direct function deployment?
Both SLS console deployment and function deployment can be based on HTTP-triggered functions, and quick deployment is usually used for web frameworks.
If you only need to develop code logic and do not need to create additional resources, you can perform quick deployment through the SCF console.
If you need to create more capabilities or resources, such as automatic creation of layer hosting dependencies, quick implementation of static resource isolation, and support for direct code repository pulling, in addition to code deployment, you can use the SLS console to create web applications.
Template Deployment - Deploying Django Sample Code
2. Choose Create Application and select Web Application > Django Framework.
3. Click Next and complete basic configuration.
4. Select Sample Code as the upload mode and click Complete. The application deployment starts.
5. After the application deployment is completed, you can view the basic information of the sample application on the application details page. In addition, you can use access the deployed Django project at the access path URL generated by API Gateway.
Custom Deployment - Quickly Deploying Web Application
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. Visit http://127.0.0.1:8000
in a browser, and you can access the sample Django project locally as shown 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 the dependency package
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:
Run the following installation command:
pip install -r requirements.txt -t .
Note:
As the db.sqlite3
library is referenced in the initialized default project, you need to install this dependency at the same time or comment out the DATABASES
field in the setting.py
file of the project.
2. (Optional) Configure the scf_bootstrap
file.
Note:
You can also complete the configuration in the console.
The listening port in the HTTP-triggered function must be 9000, so you need to change the listening address and port. To do so, you need to create the scf_bootstrap
file in the root directory of the project and add the following content to the file to configure environment variables, specify service start commands, and so on to make sure that your service can be started normally through the file:
#!/bin/bash
/var/lang/python3/bin/python3 manage.py runserver 9000
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:
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
.
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:
Be sure to change the python
path to the local path during local testing.
3. Console upload
Log in to the SLS console, select Web Application > Django Framework, and select Local Upload or Code Repository Pull as the upload mode. You can configure the scf_bootstrap
file in the console. When the configuration is completed, the console automatically generates the scf_bootstrap
file and packages it and the project code for deployment.
Note:
The actual scf_bootstrap
file in your project prevails. If the scf_bootstrap
file already exists in your project, its content will not be overwritten.
When the configuration is completed, click Complete to deploy your Django project.
Advanced configuration management
In Advanced Configuration, you can perform more application management operations, such as creating layers, binding custom domains, and configuring environment variables.
Was this page helpful?