Overview
Serverless Cloud Framework provides multiple basic resource components, which you can mix and use to quickly create and deploy resources in the cloud. This document describes how to use existing components to build your own multi-component serverless application template.
Prerequisites
Component Configuration Documentation
Directions
This document uses deploying a framework project based on Layer and Egg as an example to describe how to import multiple components into your project and quickly complete the deployment. The steps are as follows:
Step 1. Create a project
Create a project app-demo
and enter this directory:
$ mkdir app-demo && cd app-demo
Step 2. Build an Egg project
1. In the app-demo
directory, create a src
folder and create an Egg project in it:
$ mkdir src && cd src
$ npm init egg --type=simple
$ npm i
2. In the src
directory, write the configuration file serverless.yml
:
A sample .yml
file for the Egg component is provided below. For more information on all configuration items, please see Egg.js Component Configuration.
app: app-demo
stage: dev
component: egg
name: app-demo-egg
inputs:
src:
src: ./
exclude:
- .env
- node_modules
region: ap-guangzhou
functionName: eggDemo
runtime: Nodejs10.15
apigatewayConf:
protocols:
- http
- https
environment: release
Note:
The app
, stage
, and org
parameters must be the same for the resources created by each component under the same project.
The Egg component essentially creates an API Gateway trigger + SCF resource. Here, you can select different components according to your actual development needs, and the configuration methods are similar. For more information, please see Component Configuration Documentation. Step 3. Create a layer
Go back to the root directory of app-demo
, create a layer
folder, and create a layer configuration file serverless.yml
in it:
$ cd ..
$ mkdir layer && cd layer
$ touch serverless.yml
serverless.yml
can be configured according to the following template (for more information on the configuration, please see Layer Component Configuration):
app: app-demo
stage: dev
component: layer
name: app-demo-layer
inputs:
region: ap-guangzhou
src:
src: ../src/node_modules
targetDir: /node_modules
runtimes:
- Nodejs10.15
Note:
The app
, stage
, and org
parameters must be the same for the resources created by each component under the same project.
The Layer component also supports importing projects from COS buckets. For more information, please see Layer Component Configuration. When entering the bucket
parameter, be sure not to include -${appid}
, as the component will add it automatically. Step 4. Organize the resource relationship
In the same application, you can organize the creation order of resources according to their dependency relationship. Taking this project as an example, you need to create a layer first and then use the layer in the Egg.js project; therefore, you should ensure that the resource creation order is * layer > Egg.js application*. The specific steps are as follows:
Modify the .yml
configuration file of the Egg.js project, configure the layer configuration according to the following syntax, and import the deployment output of the Layer component as the deployment input of the Egg.js project to ensure that the Layer component is created before the Egg.js project:
In serverless.yml
, add layer configuration in the inputs
section:
inputs:
src:
src: ./
exclude:
- .env
- node_modules
region: ap-guangzhou
functionName: eggDemo
runtime: Nodejs10.15
layers:
- name: ${output:${stage}:${app}:app-demo-layer.name}
version: ${output:${stage}:${app}:app-demo-layer.version}
apigatewayConf:
protocols:
- http
- https
environment: release
At this point, the serverless application has been built, and the project directory structure is as follows:
./app-demo
├── layer
│ └── serverless.yml
├── src
│ ├── serverless.yml
│ ├── node_modules
│ ├── ...
│ └── app
└── .env
Step 5. Deploy the application
In the project root directory, run scf deploy
to complete layer creation and use the output of the Layer component as the input of the Egg.js component to cloudify the Egg.js framework.
$ scf deploy
serverless-cloud-framework
app-demo-layer:
region: ap-guangzhou
name: layer_component_xxx
bucket: scf-layer-ap-guangzhou-code
object: layer_component_xxx.zip
description: Layer created by serverless component
runtimes:
- Nodejs10.15
version: 3
vendorMessage: null
app-demo-egg:
region: ap-guangzhou
scf:
functionName: eggDemo
runtime: Nodejs10.15
namespace: default
lastVersion: $LATEST
traffic: 1
apigw:
serviceId: service-xxxx
subDomain: service-xxx.gz.apigw.tencentcs.com
environment: release
url: https://service-xxx.gz.apigw.tencentcs.com/release/
vendorMessage: null
76s › app-demo › "deploy" ran for 2 apps successfully.
You can click the URL output by apigw
to access the created application, run scf info
to view the status of the deployed instance, or run scf remove
to quickly remove the application.
Step 6. Publish the application template
After the serverless project template is built, Serverless Cloud Framework allows you to publish it in the Serverless Registry for use by your team and others.
1. Create a configuration file
In the root directory, create a serverless.template.yml
file, and the project directory structure is as follows:
./app-demo
├── layer
│ └── serverless.yml
├── src
│ ├── serverless.yml
│ ├── node_modules
│ ├── ...
│ └── app
├── .env
└── serverless.template.yml
name: app-demo
displayName: Egg.js project template created based on layer
author: Tencent Cloud, Inc.
org: Tencent Cloud, Inc.
type: template
description: Deploy an egg application with layer.
description-i18n:
zh-cn: Egg.js project template created based on layer
keywords: tencent, serverless, eggjs, layer
repo:
readme:
license: MIT
src:
src: ./
exclude:
- .env
- '**/node_modules'
- '**/package-lock.json'
After the serverless.template.yml
file is configured, you can use the scf publish
command to publish the project to the Registry as a template.
$ scf publish
serverless ⚡registry
Publishing "app-demo@0.0.0"...
Serverless › Successfully published app-demo
3. Reuse the template
After your template is published, others can quickly download it and reuse the project by running the scf init
command.
$ scf init app-demo --name example
$ cd example
$ npm install
Variable Import Description
serverless.yml
supports multiple ways to import variables:
Import basic Serverless parameters
In the inputs
field, you can directly import basic Serverless parameter configuration information through the ${org}
and ${app}
syntax.
Import environment variables In serverless.yml
, you can directly import the environment variable configuration (including the environment variable configuration in the .env
file and variable parameters manually configured in the environment) through the ${env}
syntax.
For example, you can import the environment variable REGION
through ${env:REGION}
.
Import the output results of other components
If you want to import the output information of other component instances into the current component configuration file, you can configure it by using the following syntax: ${output:[app]:[stage]:[instance name].[output]}
Sample .yml
file:
app: demo
component: scf
name: rest-api
stage: dev
inputs:
name: ${stage}-${app}-${name}
region: ${env:REGION}
vpcName: ${output:prod:my-app:vpc.name}
vpcName: ${output:${stage}:${app}:vpc.name}
Was this page helpful?