Serverless component development attempt: global variable component and separate deployment component

Preface

In fact, the Components of the Serverless Framework are really easy to use. Although it is convenient to use SCF CLI and VSCode plug-in to deploy Tencent cloud functions, they can only deploy cloud functions.

If I have static resources, want to configure CDN, want to bind domain name, or other more operations... I can't do without console. But the Components of the Serverless Framework can almost make me bid farewell to the console for a while. For such a tool, I really look!

However, just when I tried to use Components to do a slightly larger project, I encountered two problems that were not considered as problems, but they were really maddening.

  1. Component has no global variable;
  2. Component cannot be deployed separately;

Before reading the following, you can understand these background knowledge:

Global variable component

If only one component needs to be deployed, such as the Yaml below, the existence of global variables does not mean much.

hello_world:
  component: "@serverless/tencent-website"
  inputs:
    code:
      src: ./public
      index: index.html
      error: index.html
    region: ap-shanghai
    bucketName: hello_world

But in actual production, there are many parts in a Yaml.

For example, my Blog's Yaml: https://github.com/anycodes/ServerlessBlog/blob/master/serverless.yaml. There are a dozen functions in it. If there is no global variable, it may be a nightmare.

For example, there are 10 functions that need to be deployed in AP Guangzhou. After the deployment, I will deploy them to the AP Shanghai area. If there is no global variable, I will modify the configuration of more than ten functions. Problems may occur even if the changes are replaced in bulk. Therefore, if there are components of global variables at this time, it is particularly important.

To this end, I have contributed such a component: serverless global. Through this component, we can set some global variables and use them in the program:


Conf:
  component: "serverless-global"
  inputs:
    region: ap-shanghai
    mysql_host: gz-cdb-mytest.sql.tencentcdb.com
    mysql_user: mytest
    mysql_password: mytest
    mysql_port: 62580
    mysql_db: mytest

Album_Login:
  component: "@serverless/tencent-scf"
  inputs:
    name: Album_Login
    codeUri: ./album/login
    handler: index.main_handler
    runtime: Python3.6
    region: ${Conf.region}
    environment:
      variables:
        mysql_host: ${Conf.mysql_host}
        mysql_port: ${Conf.mysql_port}
        mysql_user: ${Conf.mysql_user}
        mysql_password: ${Conf.mysql_password}
        mysql_db: ${Conf.mysql_db}

Through Serverless global, we can define some global public parameters, and reference these parameters through variable methods. For example, ${Conf.region} refers to the region variable defined in conf inputs. It is expected that the Serverless team can support the function of global variables in the future.

Deploy components separately

It's also the example of Serverless Blog, which has multiple modules, including more than a dozen functions, API gateway, Website, etc. The first deployment is really straightforward + Meizizi: one click deployment is cool!

However, when I modify one of the functions, only modify one configuration information, and execute SLS debug deployment again, it will redeploy for me again! The deployment time is about 10 minutes, but I only modified one line of code. Although it's not a big problem, the experience is not satisfactory: why does Component not specify the function of deploying a resource?

I guess: if I can control which resource I want to deploy through a certain parameter, how good is it?

For example, with the command SLS -- debug-n website, I can only deploy the website, rather than run all resources to deploy again. How convenient it is! So I thought about it and realized a very simple set of components through simple lines of code:

Yes, I nested a tempComponent on the top of the official Component. It is easy to use. For example, there is a part of website:

test1:
  component: "@serverless/tencent-website"
  inputs:
    code:
      src: ./public
      index: index.html
      error: index.html
    region: ap-shanghai
    bucketName: test1

Change the name of the component in it to @ gosls:

test1:
  component: "@gosls/tencent-website"
  inputs:
    code:
      src: ./public
      index: index.html
      error: index.html
    region: ap-shanghai
    bucketName: test1

In this way, it becomes a component that supports the deployment of a single component, and all components of Tencent cloud can be changed by modifying the previous prefix. If you don't want to use it, you can modify it back at any time @serverless The following inputs is as like as two peas in the same content and format, and is forwarded to the corresponding @serverless/tencent-website directly. For example:

# serverless.yml

test1:
  component: "@gosls/tencent-website"
  inputs:
    code:
      src: ./public
      index: index.html
      error: index.html
    region: ap-shanghai
    bucketName: test1


test2:
  component: "@gosls/tencent-website"
  inputs:
    code:
      src: ./public
      index: index.html
      error: index.html
    region: ap-shanghai
    bucketName: test2


test3:
  component: "@gosls/tencent-website"
  inputs:
    code:
      src: ./public
      index: index.html
      error: index.html
    region: ap-shanghai
    bucketName: test3

Execute sls --debug:

DFOUNDERLIU-MB0:website_test dfounderliu$ sls --debug

  DEBUG ─ Resolving the template's static variables.
  DEBUG ─ Collecting components from the template.
  DEBUG ─ Downloading any NPM components found in the template.
  DEBUG ─ Analyzing the template's components dependencies.
  .....
  DEBUG ─ Website deployed successfully to URL: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com.
  DEBUG ─ Website deployed successfully to URL: http://test3-1256773370.cos-website.ap-shanghai.myqcloud.com.

  test1: 
    url: http://test1-1256773370.cos-website.ap-shanghai.myqcloud.com
    env: 
  test2: 
    url: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com
    env: 
  test3: 
    url: http://test3-1256773370.cos-website.ap-shanghai.myqcloud.com
    env: 

  19s › test1 › done

You can see that three deployments have been completed. When I use parameters to deploy test2:


DFOUNDERLIU-MB0:website_test dfounderliu$ sls --debug -n test2

  DEBUG ─ Resolving the template's static variables.
  DEBUG ─ Collecting components from the template.
  DEBUG ─ Downloading any NPM components found in the template.
  DEBUG ─ Analyzing the template's components dependencies.
  DEBUG ─ Creating the template's components graph.
  ......
  DEBUG ─ Uploading directory /Users/dfounderliu/Desktop/ServerlessComponents/test/website_test/public to bucket test2-1256773370
  DEBUG ─ Website deployed successfully to URL: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com.

  test1: 
  test2: 
    url: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com
    env: 
  test3: 

  6s › test3 › done

As you can see, with the - n parameter, only test2 is deployed, and no other components have changed. At present, this function supports most of the components officially provided by Tencent( https://github.com/gosls ):

@serverless/tencent-apigateway
@serverless/tencent-cam-policy
@serverless/tencent-cam-role
@serverless/tencent-cdn
@serverless/tencent-cos
@serverless/tencent-egg
@serverless/tencent-express
@serverless/tencent-flask
@serverless/tencent-koa
@serverless/tencent-laravel
@serverless/tencent-scf
@serverless/tencent-website

Come on, let's get started

Portal:

Welcome to: Serverless Chinese network , you can Best practices Experience more about Serverless application development!

Recommended reading: Serverless architecture: from principle, design to project implementation

Tags: github npm SQL Laravel

Posted on Wed, 25 Mar 2020 07:59:15 -0400 by dawsba