The SLS framework deployment scheme has been upgraded. You can use an SCF HTTP-triggered function to quickly deploy your Egg 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.
This document introduces the SLS console deployment scheme. You can also complete the deployment in CLI by referring to Deploying Web Function on Command Line.
The Node.js runtime environment has been installed locally.
mkdir egg-example && cd egg-example
npm init egg --type=simple
npm i
npm run dev
open http://localhost:7001
Next, perform the following steps to make simple modifications to the locally created project, so that it can be quickly deployed through an HTTP-triggered function. The steps of project transformation for the Egg framework are as follows:
0.0.0.0:9000
./tmp
directory is readable/writable in the SCF environment.scf_bootstrap
file.1. (Optional) Configure the scf_bootstrap
file.
Note:You can also complete the configuration in the console.
Create the scf_bootstrap
file in the root directory of the project and add the following content to it (the file is used to configure environment variables and start the service. Here is only a sample. Please adjust the configuration according to your actual business scenario):
#!/var/lang/node12/bin/node
'use strict';
/**
* Node path in docker: /var/lang/node12/bin/node
* As only `/tmp` is readable/writable in SCF, two environment variables need to be modified at startup
* `NODE_LOG_DIR` changes the default node write path of `egg-scripts` (~/logs) to `/tmp`
* `EGG_APP_CONFIG` changes 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 deploy `node_modules` through layers, you need to modify `eggPath`
Object.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');
});
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:
chmod 777 scf_bootstrap
2. Console upload
Log in to the SLS console, select Web Application > Egg 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 thescf_bootstrap
file already exists in your project, its content will not be overwritten.
When the configuration is completed, click Complete to deploy your Egg project.
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?