Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development Write the first cloud function


May 27, 2021 A minimalist getting started manual for cloud development serverless


Table of contents


In Serverless, cloud functions exist as computing containers, can be used as service interfaces, can be used as transit services, or can be written as business logic. A t the heart of the FaaS (Functions as a Service) we talk about so often is the cloud function. I f you need a simple understanding, you write a piece of code (a function) that can be deployed directly on the server, but this function is scalable. T raffic comes, the function can be pulled up directly, resources can be expanded, and traffic can fall back to 0. T here is no discussion of how the industry can achieve cold start or 0-1 optimization.

In the last section, we created our own environment in the console;

Create a function

Step 1: Sign in to the cloud development console
Website: https://console.cloud.tencent.com/tcb

Step 2: Create a cloud function
Here's the picture:
Cloud Development Write the first cloud function

  • Select the sidebar in the underlying service (cloud function)
  • Select the environment created before, be sure to choose the Tencent cloud development console created, the current small program cloud development of environmental management has not been released.
  • Click on the "New Cloud Function" button;

If the cloud function does not require a large amount of memory, you can select 128 MB;

You can then click directly on Next, as shown below:
Cloud Development Write the first cloud function

Write functions

We're done creating functions in the console, and we're ready to write the code. Cloud Studio is currently available by default as an editor, basically meeting the underlying web IDE needs.
Click on "Function Name" to go to the function configuration and details page.
Cloud Development Write the first cloud function
Cloud Development Write the first cloud function

We streamlined the default generated code to the following:
Cloud Development Write the first cloud function

  1. 'use strict';
  2. exports.main = async (event, context) => {
  3. //这里代码比较简单
  4. //后期会准备从数据库取数据,然后返回结果
  5. return {
  6. msg: 'Hello Serverless! Good good study !',
  7. maybe: '从入门到放弃'
  8. }
  9. };

Click here to save. T here is a difference between Save and Save and Install Dependencies:

  • Save and install dependencies requires the installation of dependency libraries, such as node_modules, based on package.json in the function directory;
  • Save is to save the code directly;

If you really can't tell, just click Save and install Dependency.

The settings function can be accessed using HTTP

There are several forms of access to functions, such as inter-function calls, client SDK calls, and so on; S ome students do not understand the trigger, in fact, can be understood as "using HTTP access."

Click function configuration to set up the function.
Cloud Development Write the first cloud function

Click Edit to start setting the HTTP access path, which is set here to "/say-hello". Just modify this place by clicking Save.
Cloud Development Write the first cloud function

Access and verify the returned results

Click on the generated link to see the returned data in your browser.
Cloud Development Write the first cloud function

The browser returns the data as follows:

  1. {
  2. "msg": "Hello Serverless! Good good study !",
  3. "maybe": "从入门到放弃"
  4. }

When you're done publishing, this cloud function has the following features:

  • With the scalability to deal with traffic, this is the bottom layer of cloud development, no developer relationship;
  • No need to install web containers such as Tomcat, Apache, Node.js Server;
  • No need to set up and expose ports;
  • No Nginx, API gateway required;
  • You can customize the function domain name

Of course, real service is more than that, for example:

  • Cross-domain access needs to be set up. Console sidebars Environment, Security Settings, WEB Security Domain Name can be turned on;
  • If you need to query the database to return data, see the next section;

Pass parameters to the cloud function via http URL

With the previous section, we're ready to write and publish cloud functions. B ut there's a question, since it's an HTTP service, how do you get the parameters passed by the front end?

  1. http://api.serverless80.com/say-hello?name=vczero

For example, how do I get the name parameter in the url above? A vailable queryStringParameters F or example, to calculate the number of two, the code is as follows:

  1. 'use strict';
  2. exports.main = async (event, context) => {
  3. //这里代码比较简单
  4. //这里通过 queryStringParameters 获取请求参数
  5. let a = parseInt(event.queryStringParameters.a || 0)
  6. let b = parseInt(event.queryStringParameters.b || 0)
  7. return {
  8. msg: 'Hello Serverless! Good good study !',
  9. maybe: '从入门到放弃',
  10. sum: a + b
  11. }
  12. }

The HTTP request parameters are as follows:

  1. //可以点击生成的路径直接方案,需要加上 a, b 两个参数
  2. https://你的环境ID.service.tcloudbase.com/say-hello?a=1&b=23

The results are as follows:

  1. {
  2. "msg": "Hello Serverless! Good good study !",
  3. "maybe": "从入门到放弃",
  4. "sum": 24
  5. }

For more information, refer to Cloud Access

Turn on cross-domain access

Cloud functions can be accessed over HTTP, or they can get request parameters, then. I n the front-end app, http url generated using ajax request is bound to have a cross-domain situation. S o, where to set up the function can be "designated domain name" access, other domain names can not be accessed? T hat is, turn on the "secure domain name".

Cloud Development Write the first cloud function

Can be configured:

Can be modified according to the development, formal environment, after the application is officially online, remember to delete irrelevant domain names.

Custom cloud function service domain name

In general, service calls can be made using the default generated domain name. B ut if you have your own domain name, you can also configure it. B ecause the default domain name generation is longer, there is no law, the configuration of custom domain name appears to be more uniform.

Cloud Development Write the first cloud function

You can configure it as shown above, and if you don't have an SSL security certificate, you can choose a free certificate from Tencent Cloud. Free certificates can only be configured with one domain name, not wildcard domain names, that is, sub-domain names need to be reapplyed.