Deploying Web Function On Command Line

Last updated: 2023-09-27 17:57:02

Operational Overview

HTTP-triggered Function is a new function capability in SCF. Compared with event function that has limits on the event format, HTTP-triggered Function focuses on optimization of web service scenarios and can directly send HTTP requests to URLs to trigger function execution. For more information, please see Function Overview.
The Serverless Cloud Framework (SCF) now supports the deployment of HTTP-Triggered Functions. With the SCF component, you can quickly create and deploy HTTP-triggered Functions.

Procedure

1. Execute the following command to initialize the Serverless HTTP-triggered Function template.
scf init scf-nodejs
2. Enter the demo project and view the directory structure as shown below:
. http-demo
├── serverless.yml # Configuration file
├── package.json # Dependency file
├── scf_bootstrap # Project bootstrap file
└── index.js # Service function
Here, scf_bootstrap is the project bootstrap file. For the specific writing rules, please see Bootstrap File Description.
3. Open serverless.yml to inspect the configuration details. You merely need to introduce a new type parameter within the yml file, designate the function type, and you can accomplish the deployment of a HTTP-Triggered Function.
Note
For HTTP-triggered Functions, there is no need to specify the entry function.
If the type parameter is not entered, the function will be an event function by default.
If there is no scf_bootstrap bootstrap file in your local code, you can specify the entry function by setting the entryFile parameter in the yml file. The component will generate a default scf_bootstrap bootstrap file based on the runtime language and complete the deployment. After deployment, you need to modify the content of the scf_bootstrap file in the Cloud Function Console according to the actual situation of your project.
The sample yml is as follows:
component: scf
name: http
inputs:
src:
src: ./
exclude:
- .env
# Specify the SCF type as Web type
type: web
name: web-function
region: ap-guangzhou
runtime: Nodejs12.16
# For Node.js, automatic dependency installation can be enabled.
installDependency: true
events:
- apigw:
parameters:
protocols:
- http
- https
environment: release
endpoints:
- path: /
method: ANY

4. Execute the scf deploy command in the root directory to complete the service deployment. The example is as follows:
$ scf deploy
serverless-cloud-framework 
Action: "deploy" - Stage: "dev" - App: "http" - Name: "http"
type: web
functionName: web-function
description: This is a function in http application
namespace: default
runtime: Nodejs12.16
handler:
memorySize: 128
lastVersion: $LATEST
traffic: 1
triggers:
-
NeedCreate: true
created: true
serviceId: service-xxxxxx
serviceName: serverless
subDomain: service-xxxxxx.cd.apigw.tencentcs.com
protocols: http&https
environment: release
apiList:
-
path: /
method: ANY
apiName: index
created: true
authType: NONE
businessType: NORMAL
isBase64Encoded: false
apiId: api-xxxxxx
internalDomain:
url: https://service-xxxx.cd.apigw.tencentcs.com/release/
18s › http › executed successfully

Relevant Commands

Viewing access log

Similar to event-based functions, you can directly view the latest 10 log entries of the deployed function using the scf log command. The example is as follows:
$ scf log
serverless-cloud-framework 
Action: "log" - Stage: "dev" - App: "http" - Name: "http"
-
requestId: xxxxx
retryNum: 0
startTime: 1624262955432
memoryUsage: 0.00
duration: 0
message:
"""
-
requestId: xxxxx
retryNum: 0
startTime: 1624262955432
memoryUsage: 0.00
duration: 0
message:
"""

Testing service

Solution 1: Directly open the output path URL in the browser. If it can be accessed normally, it indicates that the function has been created successfully. As shown in the figure below:

Solution 2: You can use other HTTP testing tools, such as CURL, POSTMAN, etc., to test the HTTP-triggered Function you have successfully created. The following example tests using the CURL tool:
curl https://service-xxx.cd.apigw.tencentcs.com/release/

Deleting service

Run the following command to remove your deployed cloud resources.
scf remove

Web framework migration

Serverless Cloud Framework also offers a dedicated HTTP component for Web framework deployment, enabling quick implementation of features such as Web framework deployment, layer creation, static resource separation, and CDN acceleration. For usage, please refer to Deploying Frameworks via Command Line.