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 网关
- Timer
- COS
- CMQ
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';
@Provide()
export class HelloTencentService {
@Inject()
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.
A timed task trigger is used to periodically execute a function. Tencent Cloud Timer Trigger currently only supports cron format.
Warm reminder, please close the trigger in time after testing the function and execute it automatically to avoid over-deduction.
import { Provide, Inject, ServerlessTrigger, ServerlessTriggerType } from '@midwayjs/core';
import { Context, SCF } from '@midwayjs/faas';
@Provide()
export class HelloTencentService {
@Inject()
ctx: Context;
@ServerlessTrigger(ServerlessTriggerType.TIMER, {
type: 'cron',
value: '*/60 * * * * * *', // trigger every 60s
})
async handleTimerEvent(event: SCF.TimerEvent) {
this.ctx.logger.info(event);
return 'hello world';
}
}
Note that Tencent Cloud is set to full Cron. For more information about the Cron format, see Development documentation.
Common format:
*/5 * * * * * indicates that it is triggered every 5 seconds.
0 0 2 1 * * * indicates that it is triggered at 2: 00 a.m. on the 1st of each month.
0 15 10 * * MON-FRI * means it will be triggered at 10:15 a.m. every day from Monday to Friday.
0 0 10,14,16 * * * * means to trigger at 10: 00 a.m., 2: 00 p.m. and 4: 00 p.m. every day
0 */30 9-17 * * * * means every half hour from 9: 00 a.m. to 5: 00 p.m.
0 0 12 * * WED * means to trigger at 12 noon every Wednesday
Timer configuration
Attribute name | Type | Description |
---|---|---|
type | cron | Required, trigger type, representing cron expression. |
value | string | Required, cron expression or every value. |
payload | string | Optional, a fixed passed value, rarely used |
Example:
cron expression
@ServerlessTrigger(ServerlessTriggerType.TIMER, {
type: 'cron',
value: '0 0 4 * * *', //triggered at 4:00 every day
})
Event structure
The structure returned by the Timer message is as follows and is described in the SCF.TimerEvent
type.
{
Message: '',
Time: new Date().toJSON()
TriggerName: 'test',
Type: 'Timer',
}
COS is a service used by Tencent Cloud to store some resource files.
import {
Provide,
Inject,
ServerlessTrigger,
ServerlessTriggerType
} from '@midwayjs/core';
import { Context, SCF } from '@midwayjs/faas';
@Provide()
export class HelloTencentService {
@Inject()
ctx: Context;
@ServerlessTrigger(ServerlessTriggerType. OS, {
bucket: 'cli-appid.cos.ap-beijing.myqcloud.com',
events: 'cos:ObjectCreated :*',
filter: {
prefix: 'filterdir /',
suffix: '.jpg',
},
})
async handleCOSEvent(event: SCF.COSEvent) {
// xxx
}
}
After npm run deploy
.
COS Trigger Configuration
Attribute name | Type | Description |
---|---|---|
bucket | string | The bucket address of the object store. |
events | string | The name of the event that triggered the execution of the function. |
Filter | { prefix: string; suffix: string; } | the object filtering parameter. only objects that meet the filtering criteria can trigger the function. it contains a configuration property key, which indicates the object key that the filter supports filtering. |
Example:
Listener Object Creation Event
@ServerlessTrigger(ServerlessTriggerType. OS, {
bucket: 'cli-appid.cos.ap-beijing.myqcloud.com',
events: 'cos:ObjectCreated :*,
filter: {
prefix: 'filterdir /',
suffix: '.jpg',
},
})
Event structure
The structure returned by COS messages is as follows, which is described in the type of SCF.COSEvent
.
{
"Records": [
{
"cos": {
"cosSchemaVersion": "1.0 ",
"cosObject": {
"url": "http://testpic-1253970026.cos.ap-chengdu.myqcloud.com/testfile ",
"meta": {
"x-cos-request-id": "NWMxOWY4MGFfMjViMjU4NjRfMTUyMV8yNzhhZjM =",
"Content-Type": ""
},
"vid": "",
"key": "",
"size": 1029
},
"cosBucket": {
"region": "cd ",
"name": "testpic ",
"appid": "1253970026"
},
"cosNotificationId": "unkown"
},
"event": {
"eventName": "cos:ObjectCreated :*",
"eventVersion": "1.0 ",
"eventTime": 1545205770
"eventSource": "qcs::cos ",
"requestParameters": {
"requestSourceIP": "192.168.15.101 ",
"requestHeaders": {
"Authorization": "****************"
}
},
"eventQueue": "qcs:0:lambda:cd:appid/1253970026:default.printevent.$LATEST ",
"reservedInfo": "",
"reqid": 179398952
}
}
]
}
CMQ(mq) triggers subscribe to Tencent Cloud's message queue service.
import { Provide, Inject, ServerlessTrigger, ServerlessTriggerType } from '@midwayjs/core';
import { Context, SCF } from '@midwayjs/faas';
@Provide()
export class HelloTencentService {
@Inject()
ctx: Context;
@ServerlessTrigger(ServerlessTriggerType.MQ, {
topic: 'test-topic',
tags: 'bbb',
})
async handleCMQEvent(event: SCF.CMQEvent) {
// xxx
}
}
Note that under Tencent Cloud, the default message queue format provided by midway faas is JSON
CMS Trigger Configuration
Attribute name | Type | Description |
---|---|---|
topic | string | topic for receiving messages |
tags | string | optional, which describes the tags of message filtering in the subscription (only messages with consistent tags will be pushed) |
Example:
Monitor MQ messages
@ServerlessTrigger(ServerlessTriggerType.MQ, {
topic: 'test-topic',
region: 'cn-shanghai'
strategy: 'BACKOFF_RETRY'
})
Event structure
The structure returned by CMQ messages is as follows and is described in the type of SCF.CMQEvent
.
{
"Records": [
{
"CMQ": {
"type": "topic ",
"topicOwner": 1567
"topicName": "testtopic ",
"subscriptionName": "xxxxxx ",
"publishTime": "1970-01-01T00:00:00.000Z ",
"msgId": "123345346 ",
"requestId": "123345346 ",
"msgBody": "Hello from CMQ! ",
"msgTag": "tag1,tag2"
}
}
]
}
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
- API 网关
- Timer
- COS
- CMQ
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');
});
});
Unlike HTTP testing, the function app is created by createFunctionApp
, and the instance of the entire class is obtained by getServerlessInstance
, thus calling a specific method to test.
You can quickly create the structure passed in by the platform by createTimerEvent
methods.
import { createFunctionApp, close } from '@midwayjs/mock';
import { Framework, Application } from '@midwayjs/serverless-app';
import { HelloTencentService } from '../src/function/hello_tencent';
import { createTimerEvent } from '@midwayjs/serverless-scf-trigger';
import { join } from 'path';
describe('test/hello_tencent.test.ts', () => {
let app: Application;
let instance: HelloTencentService;
beforeAll(async () => {
// create app
app = await createFunctionApp<Framework>();
instance = await app.getServerlessInstance<HelloTencentService>(HelloTencentService);
});
afterAll(async () => {
await close(app);
});
it('should get result from timer trigger', async () => {
expect(await instance.handleTimerEvent(createTimerEvent())).toEqual('hello world');
});
});
Unlike HTTP testing, the function app is created by createFunctionApp
, and the instance of the entire class is obtained by getServerlessInstance
, thus calling a specific method to test.
You can quickly create the structure passed in by the platform by createCOSEvent
methods.
import { createFunctionApp, close } from '@midwayjs/mock';
import { Framework, Application } from '@midwayjs/serverless-app';
import { HelloTencentService } from '../src/function/hello_tencent';
import { createCOSEvent } from '@midwayjs/serverless-scf-trigger';
import { join } from 'path';
describe('test/hello_tencent.test.ts', () => {
let app: Application;
let instance: HelloTencentService;
beforeAll(async () => {
// create app
app = await createFunctionApp<Framework>();
instance = await app.getServerlessInstance<HelloTencentService>(HelloTencentService);
});
afterAll(async () => {
await close(app);
});
it('should get result from timer trigger', async () => {
expect(await instance.handleCOSEvent(createCOSEvent())).toEqual('hello world');
});
});
Unlike HTTP testing, the function app is created by createFunctionApp
, and the instance of the entire class is obtained by getServerlessInstance
, thus calling a specific method to test.
You can quickly create the structure passed in by the platform by createCMQEvent
methods.
import { createFunctionApp, close } from '@midwayjs/mock';
import { Framework, Application } from '@midwayjs/serverless-app';
import { HelloTencentService } from '../src/function/hello_tencent';
import { createCMQEvent } from '@midwayjs/serverless-scf-trigger';
import { join } from 'path';
describe('test/hello_tencent.test.ts', () => {
let app: Application;
let instance: HelloTencentService;
beforeAll(async () => {
// create app
app = await createFunctionApp<Framework>(join(__dirname, '../'), {
initContext: createInitializeContext()
});
instance = await app.getServerlessInstance<HelloTencentService>(HelloTencentService);
});
afterAll(async () => {
await close(app);
});
it('should get result from cmq trigger', async () => {
expect(await instance.handleCMQEvent(createCMQEvent())).toEqual('hello world');
});
});
Published to Tencent Cloud SCF
Make sure it is tencent
at the provider
paragraph of f.yml
in the project root directory.
service:
name: midway-faas-examples
provider:
name: tencent
Configuration runtime
service:
name: midway-faas-examples
provider:
name: tencent
runtime: nodejs12
Configuration function timeout
service:
name: midway-faas-examples
provider:
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
service:
name: midway-faas-examples
provider:
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 4.47.41.mov
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
provider:
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
provider:
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:// service-xxxxx-xxxxxxxx.gz.apigw.tencentcs.com:80
. 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.
