Rapidly develop and deploy Serverless applications

This article will introduce how to develop and deploy Serverless applications in detail, and initialize and deploy applications through Alibaba Cloud's function computing console and developer tool Serverless Devs; Finally, share the debugging of the application, and introduce the deployment and operation and maintenance summary of the application through scientific release and observability, so as to realize the exploration of the basic process and core steps from application initialization to debugging, release and operation and maintenance.

1、 How to develop and deploy Serverless applications

1. Create functions through the console

Next, we will implement the output of Hello World on the FaaS platform based on the Serverless architecture. The basic steps can be divided into:

1) Register an account and log in;

2) Find the corresponding FaaS product: Alibaba Cloud's function calculation;

3) Click Create Function to create a function;

4) Configure functions, including function name and runtime (which can be considered as the programming language to be used or the programming environment to be used);

5) Complete the creation and test.

Take Alibaba Cloud function computing as an example. After registering and logging in to Alibaba Cloud account, you need to find the function computing product and click to enter the product homepage, as shown in the figure:

Home page of Alibaba Cloud functional computing products

Select "Service and Function" on the left and create the service, as shown in the figure.

Alibaba Cloud function computing creation service page

Then create the function, as shown in the figure.

Alibaba Cloud function computing creation function page

Compared with other cloud platforms, in Alibaba Cloud function computing platform, we need not only to set the function name and select the runtime for the function to be created, but also to set the service where the function is located. In Alibaba Cloud's functional computing system, the introduction of the concept of service will bring certain benefits:

• Related functions can be classified under a service, which is actually more intuitive than label classification.

• Associated functions share certain configurations under the same service, such as VPC configuration, NAS configuration, and even the configuration of some log warehouses.

• Through services, we can well divide the functional environment. For example, for an album project, there may be an online environment, a test environment, and a development environment. Then we can distinguish at the service level, that is, we can set three services, namely, Album-release, Album-test, and Album-dev, to isolate the environment.

• Through services, we can store functions well. If the project is relatively large, many functions may be generated, and it will be very confusing to put them at the same level. At this time, you can effectively receive them through services.

After creating the function, we can edit the code. Alibaba Cloud function computing supports uploading code from object storage, directly uploading code packages, and online editing. In addition, Alibaba Cloud function computing also supports direct upload of folders, as shown in the figure.

After saving the code, you can click Execute to trigger and test the function.

You can see that the system has output the relevant log: Hello world. So far, a very simple function has been created successfully.

2. Create and deploy functions through tools

It is very convenient to get started with the development, deployment and operation and maintenance of Serverless applications through the Serverless developer tool. We take Serverless Devs as an example to introduce the deployment of Alibaba Cloud functional computing applications, and explore the function creation, deployment and other related functions on the tool side.

Serverless Devs is an open source serverless developer platform dedicated to providing developers with a powerful tool chain. Through this platform, developers can experience multi-cloud Serverless products with one click and deploy Serverless projects at a high speed. According to the current official description, Serverless Devs has supported serverless related products from multiple cloud manufacturers, including Lanbda, Alibaba Cloud function computing, Baidu AI Cloud function computing, Tencent Cloud function, etc.

Let's explore how to create and deploy Serverless applications through the Serverless Devs developer tool, taking Alibaba Cloud functional computing as an example.

1) Install the Serverless Devs developer tool (execute the npm install - g @ Serverless Devs/s command).

2) Set Alibaba Cloud credential information (execute the s config add -- AccessKeyID AccessKeyID -- AccessKeySecret AccessKeySecret -- AccountID AccountID command).

3) Create a template project (execute the s init node.js12-http - d fc-hello-world-demo command). The initialization process is shown in the figure.

Create a project map through Serverless Devs

4) Enter the project directory (execute the cd fc-hello-world-demo command) and deploy (execute the s deploy command). The results after deployment are shown in the figure.

Deploy projects through Serverless Devs

After the project is successfully deployed, more operations can be performed, as follows: trigger the function (execute the s invoke command), and the results are shown in the figure.

Trigger function through Serverless Devs

View the online function details (execute the s info command), and the results are as shown in the figure.

View function details through Serverless Devs

Serverless Devs also has a relatively complete desktop client. Developers can create and manage applications and use related supporting functions through the desktop client. For example, see the application list and quickly create applications, as shown in the figure.

View application list through Serverless Devs desktop client

After creating an application, you can manage it. The following figure shows the serverless Devs desktop client management application interface.

Manage applications through Serverless Devs desktop client

The use of other supporting functions is as follows. As shown in the figure, the performance of one-key pressure measurement function is shown.

One-click pressure test function performance through Serverless Devs desktop client

Debug function resources with one click, as shown in the figure.

Debugging function resources through Serverless Devs desktop client

Click to view the function multi-dimensional indicator information, as shown in the figure.

One-click view of function multi-dimensional indicator information through Serverless Devs desktop client

In addition, Serverless Devs also has a convenient Yaml visualization configuration function, as shown in the following figure.

Yaml visualization configuration through Serverless Devs desktop client

2、 How to debug the Serverless application

In the process of application development, or after the application development is completed, when the execution results do not meet the expectations, a certain amount of debugging is usually required. However, under the Serverless architecture, debugging is often subjected to great challenges, especially when limited by environmental factors, such situations usually occur: the developed application can run healthily and as expected locally, but there are some unpredictable problems on the FaaS platform; Or in some special environments, there is no way to simulate the online environment locally, and it is difficult to debug the application. The debugging of Serverless applications has been criticized, but various cloud vendors have not abandoned their in-depth exploration in the direction of debugging. Here are some ways.

1. Online commissioning

(1) Simple debugging

The so-called simple debugging is to debug on the console. Take Alibaba Cloud function computing as an example, you can use the "Code Execution" button on the console to perform basic debugging, as shown in the following figure.

Function calculation code editing page

You can also simulate some events by setting Event when necessary.

Alibaba Cloud function computing event page

The advantage of online debugging is that you can use some online environments for code testing. When the online environment has VPC and other resources, it is difficult to debug in the local environment.

(2) Breakpoint debugging

In addition to simple online debugging, some cloud vendors also support breakpoint debugging, such as remote debugging of Alibaba Cloud function computing. Taking Alibaba Cloud remote debugging of function computing as an example, we can realize online debugging of functions through the console. After creating the function, you can select remote debugging and click the "Start Debugging" button, as shown in the figure.

Function Calculation Remote Debugging Page

After starting debugging, wait a moment, and the system will enter the remote debugging interface, as shown in the figure.

Function calculation remote debugging start page

When the interface shown below appears, we can perform breakpoint debugging.

Function calculation remote debugging breakpoint debugging page

2. End-cloud joint debugging

When developing Serverless applications locally, some online resources are often involved, such as triggering function execution through object storage triggers, accessing databases through VPC, etc. At this time, the inconsistency between online and offline environments will pose great challenges to offline development and debugging. The Serverless Devs developer tool connects online and offline resources by building a proxy auxiliary function, which can quickly help developers develop and debug applications locally. This debugging method is called end cloud joint debugging.

As shown in the figure below, the Serverless Devs developer tool will create auxiliary services and auxiliary functions according to the Yaml configuration file, and connect online and offline resources through auxiliary services and auxiliary functions, as well as complete end-to-end cloud joint debugging.

Schematic diagram of serverless Devs cloud joint commissioning

The Serverless Devs developer tool will create auxiliary services and auxiliary functions according to the content of the Yaml configuration file (the auxiliary service and the business service configuration declared in Yaml are consistent).

Trigger the auxiliary function (function calculation C) through triggers (including SDK, API, s proxied invoke command, or other triggers), and request traffic to return to the local debugging instance (local environment A). At this time, the event and context received by the local debugging instance (local function execution environment container) are actually from the online.

The local debugging instance (local environment A) can directly access the following contents:

• VPC intranet resources, such as RDS, Kafka intranet addresses, etc;

• Intranet addresses of some cloud services;

• Hard disk mount service (direct access to NAS).

The end-to-end cloud joint commissioning process is as follows:

1) Execute the s proxied setup command to prepare the auxiliary resources and local environment required for the end cloud joint debugging;

2) For ordinary event functions or HTTP triggers without triggers, after the preparation is completed, start another new terminal, switch to the project path, and execute the s proxied invoke command to call the local function;

3) After completing the debugging task, execute the s proxied cleanup command to clean up the auxiliary resources and local environment required for the end cloud joint debugging.

In addition to using the end cloud joint debugging function through commands, we can also use the end cloud joint debugging function in the VSCode developer tool, as shown in the figure.

Use the end-to-end cloud joint debugging function in VSCode

3. Remote debugging

In addition to a channel service container, there is also a function calculation container in the local cloud joint debugging to execute local functions; The remote auxiliary function simply sends remote traffic to the local. In the actual debugging process, you need to log in to the instance for project debugging. At this time, you can choose to use remote debugging.

Compared with Duanyun joint debugging, remote debugging has only one channel service container locally, and the execution process depends on the line; The remote function returns the execution result. The overall architecture of remote debugging is shown in the figure.

In addition to logging in to the online environment for code debugging or problem location through the channel service shown in the above remote debugging architecture diagram, some cloud vendors also provide the function of directly logging in to the instance for code debugging. Take Serverless Devs as an example. When using Alibaba Cloud function computing, we can log in to the online instance directly through the instance command.

Although the instance login command has provided a convenient login experience and can help users solve problems such as application exception positioning in complex scenarios, after logging in to the instance, users cannot directly locate the problem through function logs and monitoring indicators, and need to use tools such as coredump, tcpdump, jmap, etc. to conduct in-depth troubleshooting.

For example, a user found that some function error prompts appeared in his online program recently. The error contents were timeout when connecting to a remote service. The user suspected that the network connection between the function instance and the remote service was unstable, so he wanted to enter the instance and analyze the network situation between the instance and the remote service. At this point, we can follow the following steps to troubleshoot the problem.

1) As shown in the figure, after logging into the instance, you need to execute the commands apt-get update and apt-get install tcpdump to install the tcpdump tool.

Rendering of instance login and installation software

2) After the installation is completed, execute the tcpdump command to capture the request of the remote service IP, and save the packet capture results in the tcpdump.cap file.

3) After packet capturing, upload the tcpdump.cap file to your OSS with the help of OSS command line tool ossutil64, and then download it to your local OSS for analysis with the help of the analysis tool Wireshark.

4. Local commissioning

(1) Command Line Tools

Most FaaS platforms will provide users with relatively complete command line tools, such as Alibaba Cloud's Funcraft, and some open source projects, such as Serverless Framework, Serverless Devs, and other FaaS platforms that support multi-cloud vendors. The method of code debugging through command line tools is very simple. Taking Serverless Devs as an example, the method of local debugging Alibaba Cloud function calculation is to ensure that there is a local project for function calculation, and then execute debugging instructions under the project, such as debugging in Docker, as shown below.

Local debugging from the command line

(2) Editor plug-in

Take the VSCode plug-in as an example. After downloading the VSCode plug-in for Alibaba Cloud function computing and configuring the account information, create a new function locally, and perform breakpoint debugging after counting, as shown in the figure.

3、 Dependency installation and project construction through developer tools

There is a big difference between the application development under the Serverless architecture and the application development under the traditional architecture. The content dimension they focus on is different. For example, in the ideal state, the former does not require people to pay attention to the underlying resources such as servers. But in today's development stage of Serverless, does the application development under the Serverless architecture really need people's equal attention to the server? Actually, it is not. Although the Serverless architecture emphasizes the Noserver mind, there are many dependencies that cannot be used across platforms in actual production. For example, some dependencies in the Python language need to be binary compiled, which has a great relationship with the operating system and software environment. Therefore, if such dependencies are introduced into the project, they need to be installed in an environment consistent with the online environment of the functional computing platform Code packaging or project deployment.

Alibaba Cloud function computing has a detailed description of its online function environment and provides corresponding documents. For example, executable files compiled using C, C++and Go need to be compatible with the running environment of function computing. The Python runtime environment for function calculation is as follows.

• Linux kernel version: Linux 4.4.24-2.al7.x86_ 64。

• Docker basic image: Docker pull python: 2.7; docker pull python:3.6。

However, in the actual application development process, the packaging of dependencies is still a headache for many developers. They will face similar challenges in the development process of many Serverless applications: the project can run normally locally, and once it is published online, it can't find a dependency, but the dependency actually exists. At this time, the problem location becomes very difficult.

At present, there are three methods to install dependencies or build projects for Serverless applications:

1) After the project is created locally, build the online environment according to the environment data provided by the cloud manufacturer, and then install the dependency. This method is relatively autonomous and controllable, but it is very difficult and cumbersome.

2) Use existing developer tools to install dependencies, as shown in the figure:

Diagram of dependency installation through tools

Take the Serverless Devs developer tool and Alibaba Cloud functional computing products for example. Developers only need to prepare relevant dependent installation files for the corresponding language according to the language habits, such as requirements.txt for Python language, package.json for Node.js language, etc. Then under the current project, execute the command build -- use-docker to complete the installation of dependencies in an environment consistent with the online environment of Alibaba Cloud's functional computing platform. Taking the Python project as an example, developers only need to complete the following operations in the project directory:

1. Develop source code;

2. After executing the s build – use-docker command, automatically download the corresponding dependencies to the local location according to the requirements.txt file, and form the deliverables together with the source code;

3. Execute the s deploy command to package the entire delivery, create a function, and set the environment variables of the dependent package, so that the function can directly input the corresponding code dependent package.

3) At present, some cloud vendors' FaaS platform consoles support WebIDE. Alibaba Cloud's WebIDE has the ability to implement command line programs, so you can also install dependencies directly in the console's WebIDE.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us