Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse global variable components and individually deployed components in Serverless component development

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to parse global variable components and separate deployment components in Serverless component development? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Preface

To be honest, Serverless Framework's Components is really easy to use. Although it is convenient to deploy Tencent Cloud functions using SCF CLI and VSCode plug-ins, you can only deploy cloud functions.

If I have static resources, want to configure CDN, want to bind domain names, or other more operations. Probably can't do without the console. But Serverless Framework's Components almost allows me to leave the console for a while. For such a tool, I really respect!

However, as I tried to use Components for a slightly larger project, I encountered two problems that were not a problem, but they were also really maddening.

Component has no global variables

Component cannot be deployed separately

Global variable component

If there is only one component to deploy, such as the following Yaml, then the existence of global variables really doesn't make much sense.

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, a Yaml will write a lot of parts.

For example, my Blog's Yaml: https://github.com/anycodes/ServerlessBlog/blob/master/serverless.yaml. There are more than a dozen functions, and if there are no global variables, it may really be a nightmare.

For example, there are 10 functions, all of which are deployed in ap-guangzhou. After the deployment is complete, I will deploy them to the ap-shanghai area, and if there are no global variables, I will have to modify the configuration of more than a dozen functions. Even if the changes are replaced in batches, problems may occur. Therefore, it is particularly important to have components of global variables at this time.

To this end, I have contributed such a component: serverless-global. With this component, we can set some global variables to use in the program:

Conf: component: "serverless-global" inputs: region: ap-shanghai mysql_host: mytest mysql_password: mytest mysql_port: 62580 mysql_db: mytestAlbum_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}

With serverless-global, we can define some global public parameters and reference them through variable methods, for example, ${Conf.region} refers to the region variable defined in Conf-inputs. It is expected that the Serverless team will support global variables in the future.

Deploy components separately

Or the example of Serverless Blog, there are several modules, including more than a dozen functions, API gateway and Website and so on. The first deployment is really cool + happy: one-click deployment is cool!

However, when I modified one of the functions, only one configuration information was modified, and when I performed the sls-debug deployment, it was redeployed for me again! I deployed about 10min once, but I only modified one line of code. It's not a big deal, but the experience is not satisfactory: why doesn't Component specify the ability to deploy a resource?

I wondered: how nice would it be if I could use a parameter to control which resource I want to deploy?

For example, I can deploy only website with the command sls-- debug-n website, instead of deploying all the resources again. How convenient that is! So I thought about it and implemented a very simple set of Component through a few lines of code:

Yes, I am on the top of the official Component, nesting a tempComponent. 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 component inside 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 Tencent Cloud components can be changed by modifying the previous prefix. If you do not want to use it, you can change it back to @ serverless at any time. The content and format of the following inputs, exactly the same as the official one, can be forwarded directly to the corresponding @ serverless/tencent-website. For example:

# serverless.ymltest1: component: "@ gosls/tencent-website" inputs: code: src:. / public index: index.html error: index.html region: ap-shanghai bucketName: test1test2: component: "@ gosls/tencent-website" inputs: code: src:. / public index: index.html error: index.html region: ap-shanghai bucketName: test2test3: 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 execute the deployment test2 using parameters:

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 argument, only test2 is deployed, and no changes have taken place in the other components. Currently, this feature supports most of the official Tencent components (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 's answers to the questions about how to parse global variable components and deploy components separately in Serverless component development are shared here. I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report