Aller au contenu principal
Version: 3.0.0

Tencent cloud SCF

Serverless Cloud Function (SCF) is a serverless execution environment provided by Tencent Cloud for enterprises and developers to help you run code without purchasing and managing servers. You only need to write the core code in the language supported by the platform and set the conditions under which the code runs to run the code flexibly and safely on the Tencent cloud infrastructure. Cloud functions are ideal computing platforms for real-time file processing and data processing.

The following are common methods for using and testing function triggers.

Trigger code

API Gateway is similar to HTTP functions in Tencent Cloud Function System, through which we publish functions as HTTP services.

import { Provide, Inject, ServerlessTrigger, ServerlessTriggerType } from '@midwayjs/core';
import { Context } from '@midwayjs/faas';

export class HelloTencentService {
ctx: Context;

@ServerlessTrigger(ServerlessTriggerType.API_GATEWAY, {
path: '/api_gateway_tencent',
method: 'post',
async handleAPIGatewayEvent(@Body() name) {
return 'hello ${name}';

After npm run deploy, you can access the link that is output from the console.

Local development

API Gateway types can be developed locally by using the npm run dev method similar to traditional applications. Other types of triggers cannot be developed locally by using dev. You can only run npm run test to run tests.

Local test

Same as the traditional application HTTP test, the function app is created by createFunctionApp and tested by createHttpRequest method.

import { Framework } from '@midwayjs/serverless-app';
import { createFunctionApp, createHttpRequest } from '@midwayjs/mock';

describe('test/hello_tencent.test.ts', () => {
let app: Application;
let instance: HelloTencentService;

beforeAll(async () => {
// create app
app = await createFunctionApp<Framework>();

afterAll(async () => {
await close(app);

it('should get result from http trigger', async () => {
const result = await createHttpRequest(app).post('api_gateway_tencent').send({
name: 'zhangting',

expect(result.text).toEqual('hello zhangting');

Published to Tencent Cloud SCF

Make sure it is tencent at the provider paragraph of f.yml in the project root directory.

name: midway-faas-examples

name: tencent

Configuration runtime

name: midway-faas-examples

name: tencent
runtime: nodejs12

Configuration function timeout

name: midway-faas-examples

name: tencent
Timeout: 60# Unit Seconds

Multiplex HTTP gateway

Tencent Cloud will create a new gateway binding every time HTTP type is deployed. For development, we can reuse the same id

name: midway-faas-examples

name: tencent
serviceId: ********

For more information, see DIP.

Run npm run deploy. The Deploy command is automatically packaged and released by calling the official deployment tool of Tencent Cloud.

The video flow is as follows:

Screen recording 2021-03-25 pm

Frequently Asked Questions

1. User authentication

When Tencent Cloud is deployed, if it is the first deployment, the console will display the corresponding two-dimensional code, and the authentication can be completed by scanning the code. The configuration will be reused by default for a single project.

The authentication file is stored in the .env file in the root directory of the deployment. If you want to modify the file, you can delete the file and scan the code again.

You can also modify the content in the following format:

TENCENT_APP_ID = xxxxxx# AppId of authorized account
TENCENT_SECRET_ID = xxxxxx# SecretId of authorized account
TENCENT_SECRET_KEY = xxxxxx# SecretKey of authorized account
TENCENT_TOKEN = xxxxx# temporary token

If you want to use a RAM user to publish a RAM user, you can view the RAM user permissions.

2. Release area switching

Tencent cloud configuration supports publishing to different regions.

service: fc-qinyue-test

name: tencent
runtime: nodejs10
region: ap-shanghai

Common region values are:

  • ap-shanghai Shanghai
  • ap-guangzhou Guangzhou
  • ap-beijing Beijing
  • ap-hongkong Hong Kong

For the complete list of regions, see Tencent Cloud documentation.

3. Reuse API Gateway

If the HTTP function is officially released, Tencent Cloud will automatically create a serviceId that identifies the Gateway every time it is released, and there will be many in the long run. In order to reuse each time, it is better to record the serviceId to reuse the following codes after the first release (or apply for a good gateway in advance).

service: fc-qinyue-test

name: tencent
runtime: nodejs10
serviceId: service-xxxxxx # <---- fill in the id here for reuse

Obtain the ID of the API Gateway

Way one, get from the platform.

Method 2, get after each release.

4. Bind domain name

After Tencent Cloud is released, it automatically gives a gateway address to access cloud functions, such as http:// At the same time, it automatically maps three sets of environment names and loads them on the path.

  • Test environment/test
  • Advance/prepub
  • Online/release

This address can be seen at both the trigger management of the function and the API gateway.

at the gateway.

If we want to remove this environment, we have to bind a custom domain name.

Click "New" at the custom domain name of API Gateway ".

Configure a custom path mapping, such as mapping/to the official publishing environment, so that the environment suffix is not required when accessing by a domain name.

5. Additional Billing

When using local tools, due to the SDK provided by Tencent Cloud, a COS Bucket may be created for the storage of code packages. As COS is used for payment, a certain fee will be incurred. Please pay attention to your COS situation in time to avoid deduction.

6. Delete Tencent Cloud Gateway Service

After trying out the Tencent cloud service for a period of time, many examples of non-reused gateways will appear because API gateways are not reused every time, as follows.

At this time, because the gateway has bound functions, the delete button is gray, and we need to manually delete the resources one by one.

Enter an API gateway instance first.

Enter the management API and delete the general API below.

Go to environmental management and take the environment offline.

Go back to the initial list and click Delete.