In addition to the console, you can also rapidly deploy a Web framework via the command line. This document will specifically guide you on how to locally deploy a Web application using the HTTP component of the Serverless Cloud Framework.
Preparations
You have installed the Serverless Cloud Framework, activated the service, and completed the permission configuration for the Serverless Cloud Framework.
Supported Framework
Supported Framework | Documentation |
Express | |
Koa | |
Egg | |
Next.js | |
Nuxt.js | |
Nest.js | |
Flask | |
Django | |
Laravel |
Instructions
1. Developing Applications Locally
Based on your actual business scenario, complete the development locally. For more details, please refer to the Supported Frameworks development documentation.
2. Configuring the YML File
In the project root directory, create a new
serverless.yml file and write the configuration according to the following example. For comprehensive configuration, please refer to the Configuration Document.# serverless.ymlcomponent: http # Component name, which is requiredname: webDemo # Component instance name, which is required.inputs:region: ap-guangzhou # The region where the cloud function is locatedsrc: # Deploy the code files under src, package them into a zip file, and upload to the bucket.src: ./ # The local directory of files that need to be packagedexclude: # Files or directories to be excluded- .env- 'node_modules/**'faas: # Function configuration relatedframework: express # Select framework, using express as an example hereruntime: Nodejs12.16name: webDemo # Cloud function nametimeout: 10 # Timeout period, in secondsmemorySize: 512 # Memory size, default is 512 MBlayers:- name: layerName # Layer nameversion: 1 # Versionapigw: # The HTTP component will automatically create an API Gateway service by defaultisDisabled: false # Whether to disable the automatic creation of API Gateway featureid: service-xxx # API Gateway service ID, if not filled, a new gateway will be automatically createdname: serverless # API Gateway service IDapi: # Configuration related to the created APIcors: true # Allow cross-origin resource sharing (CORS)timeout: 15 # API timeout durationname: apiName # API namequalifier: $DEFAULT # Version associated with the APIprotocols:- http- httpsenvironment: test
3. Upon completion, execute
scf deploy in the root directory for deployment. The component will automatically generate the scf_bootstrap startup file for deployment based on the selected framework type.Note
As the bootstrap file logic is strongly related to your business logic, the generated default bootstrap file may cause the framework start to fail. We recommend you manually configure the bootstrap file according to your actual business needs. For more information, please see the deployment guide document of each framework.
Example scf_bootstrap
express:
#!/usr/bin/env bash/var/lang/node12/bin/node app.js
koa
#!/usr/bin/env bash/var/lang/node12/bin/node app.js
egg
#!/var/lang/node12/bin/node/*** Node path in Docker: /var/lang/node12/bin/node* Since the serverless function only has read and write permissions for /tmp, it is necessary to modify two environment variables during startup.*NODE_LOG_DIRchanges the default node write path ofegg-scripts(~/logs) to/tmp*EGG_APP_CONFIGchanges the default directory of the Egg application to/tmp*/process.env.EGG_SERVER_ENV = 'prod';process.env.NODE_ENV = 'production';process.env.NODE_LOG_DIR = '/tmp';process.env.EGG_APP_CONFIG = '{"rundir":"/tmp","logger":{"dir":"/tmp"}}';const { Application } = require('egg');// If you deploynode_modulesthrough layers, you need to modifyeggPathObject.defineProperty(Application.prototype, Symbol.for('egg#eggPath'), {value: '/opt',});const app = new Application({mode: 'single',env: 'prod',});app.listen(9000, '0.0.0.0', () => {console.log('Server start on http://0.0.0.0:9000');});
nextjs
#!/var/lang/node12/bin/node/*# Since the HTTP direct function runs based on a Docker image, it must listen to the address 0.0.0.0 and the port must be set to 9000.*/const { nextStart } = require('next/dist/cli/next-start');nextStart(['--port', '9000', '--hostname', '0.0.0.0']);
nuxtjs
#!/var/lang/node12/bin/node/*# Since the HTTP direct function runs based on a Docker image, it must listen to the address 0.0.0.0 and the port must be set to 9000.*/require('@nuxt/cli').run(['start', '--port', '9000', '--hostname', '0.0.0.0']).catch((error) => {require('consola').fatal(error);require('exit')(2);});
nestjs
#!/bin/bash# SERVERLESS=1 /var/lang/node12/bin/npm run start -- -e /var/lang/node12/bin/nodeSERVERLESS=1 /var/lang/node12/bin/node ./dist/main.js
flask
#!/bin/bash# Since the HTTP direct function runs based on a Docker image, it must listen to the address 0.0.0.0 and the port must be set to 9000./var/lang/python3/bin/python3 app.py
django
#!/bin/bash# Since the HTTP direct function runs based on a Docker image, it must listen to the address 0.0.0.0 and the port must be set to 9000./var/lang/python3/bin/python3 manage.py runserver 0.0.0.0:9000
laravel
#!/bin/bash######################################## Inject Environment Variables in the Serverless Environment######################################## Injecting the SERVERLESS Identifierexport SERVERLESS=1# Modify the template compilation cache path, as only the /tmp directory is readable and writable in the cloud function.export VIEW_COMPILED_PATH=/tmp/storage/framework/views# Modify the session to be stored in memory (array type)export SESSION_DRIVER=array# Log Output to stderrexport LOG_CHANNEL=stderr# Modifying the Application Storage Pathexport APP_STORAGE=/tmp/storage# Initialize Template Cache Directorymkdir -p /tmp/storage/framework/views# Since the HTTP direct function runs based on a Docker image, it must listen to the address 0.0.0.0 and the port must be set to 9000.# Cloud executable file path /var/lang/php7/bin/php/var/lang/php7/bin/php artisan serve --host 0.0.0.0 --port 9000