How to quickly develop and deploy serverless applications?

Introduction: This article will introduce in detail how to develop and deploy Serverless applications, and initialize and deploy applications through the Alibaba Cloud Function Computing console and developer tools Serverless Devs; finally share application debugging, introduce applications through scientific release, observability, etc. Deployment and operation and maintenance summary, and then realize the exploration of the basic process and core steps from application initialization to debugging, release, 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 Compute;
3) Click the "Create Function" button to create a function;
4) Configuration function, including function name, runtime (which can be considered as the programming language to be used, or the programming environment to be used, etc.);
5) Complete the creation and test.

Taking Alibaba Cloud Function Compute as an example, after registering and logging in to the Alibaba Cloud account, you need to find the Function Compute product and click to enter the product home page.

Alibaba Cloud Function Compute Product Homepage

Select "Services and Functions" on the left, and create a service.

Alibaba Cloud Function Compute Create Function Page

Compared with other cloud platforms, on the Alibaba Cloud Function Computing Platform, we not only need to set the function name, select the runtime, etc. for the function to be created, but also need to set the service where the function is located. In the Alibaba Cloud function computing system, introducing the concept of service will bring certain benefits:
• Associated 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 configurations, NAS configurations, and even some log warehouse configurations.
• Through services, we can divide the function environment very well. For example, for an album project, the project may have an online environment, a test environment, and a development environment, so we can distinguish at the service level, that is, we can set album-release , album-test, album-dev three services, and then isolate the environment.
• With services, we can nicely house functions. 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, services can be used for effective storage.

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

After saving the code, you can click the "Execute" button to trigger and test the function.
You can see that the system has output related logs: Hello world. So far, a very simple function has been successfully created.

2. Function creation and deployment through tools

It is very convenient to get started with serverless application development, deployment, and operation and maintenance through serverless developer tools. We will use serverless devs as an example to introduce the deployment of Alibaba Cloud function 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 quickly deploy serverless projects. According to the current official description, Serverless Devs has already supported multiple cloud vendors including AWS Lanbda, Alibaba Cloud Function Computing, Baidu Smart Cloud Function Computing, Tencent Cloud Cloud Function, Cloud Function Workflow, etc.

Serverless related products.

Let's use the Serverless Devs developer tool and take Alibaba Cloud Function Compute as an example to practice and explore how to create and deploy Serverless applications.

1) Install the Serverless Devs developer tools (execute npm install -g @Serverless-devs/s command).
2) Set the Aliyun credential information (execute 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 graph with Serverless Devs
4) Enter the project directory (execute the cd fc-hello-world-demo command), and deploy (execute the s deploy command). The result after deployment is shown in the figure.

Deploy projects via Serverless Devs

After the project is successfully deployed, more operations can be performed, as follows, trigger function (execute s invoke command)
Trigger functions via Serverless Devs

View online function details (execute the s info command)
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, view the application list and quickly create applications, as shown in the figure.

View the application list through the Serverless Devs desktop client

After creating an application, you can manage the application. The figure below is the Serverless Devs desktop client management application interface.

Manage applications through the Serverless Devs desktop client
The use of other supporting functions is as follows. The figure shows the performance of the one-click pressure test function.
One-click pressure test function performance through Serverless Devs desktop client
One-click debugging of function resources, as shown in the figure.
One-click debugging of function resources through the Serverless Devs desktop client
View function multi-dimensional indicator information with one click, as shown in the figure.
One-click viewing of function multi-dimensional indicator information through the Serverless Devs desktop client
In addition, Serverless Devs also has a more convenient Yaml visual configuration function, as shown in the following figure.
Yaml visual configuration through Serverless Devs desktop client

2. How to debug Serverless applications

During the application development process or after the application development is completed, when the execution result does not meet expectations, certain debugging is usually required. However, under the Serverless architecture, debugging is often subject to great challenges, especially when limited by environmental factors. Usually, such a situation occurs: the application developed locally can run healthily and as expected, but on the FaaS platform There are some unpredictable problems; 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 always been criticized, but various cloud vendors have not given up on in-depth exploration in the direction of debugging. Below we introduce several methods.

1. Online debugging
(1) Simple debugging
The so-called simple debugging is to debug on the console. Taking Alibaba Cloud Function Compute as an example, you can perform basic debugging through the "Code Execution" button on the console, as shown in the figure below.
When necessary, some events can also be simulated by setting Event.
The advantage of online debugging is that some online environments can be used for code testing. When the online environment has resources such as VPC, 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 the remote debugging of Alibaba Cloud Function Compute. Let's take the remote debugging of Alibaba Cloud Function Compute as an example, which can realize online debugging of functions through the console. After the function is created, you can select remote debugging and click the "Enable Debugging" button, as shown in the figure.

Function Compute remote debugging page

After enabling debugging, wait for a while, and the system will enter the remote debugging interface, as shown in the figure.
When the Function Compute remote debugging start page appears
When the interface shown in the figure below appears, we can perform breakpoint debugging.
Function Compute Remote Debugging Breakpoint Debugging Page

2. Device-cloud joint debugging

When developing serverless applications locally, some online resources are often involved, such as triggering function execution through object storage triggers and accessing databases through VPC. At this time, the inconsistency between online and offline environments will make offline development and debugging extremely difficult big challenge. The Serverless Devs developer tool connects online and offline resources by building proxy auxiliary functions, which can quickly help developers develop and debug applications locally. This debugging method is called device-cloud joint debugging.

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

Schematic Diagram of Serverless Devs Device-Cloud Joint Debugging Principle

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

Auxiliary functions (Function Compute C) are triggered through triggers (including via SDK, API, s proxied invoke command, or other triggers), and the request flow returns to the local debugging instance (local environment A). At this time, the local debugging instance (local function The event and context received by the execution environment container) are actually from the line.

The local debug instance (local environment A) has direct access to the following:
• VPC intranet resources, such as RDS, Kafka intranet address, etc.;
• Intranet addresses of some cloud services;
• Hard disk mount service (direct access to NAS).
The device-cloud joint debugging process is as follows:
1) Execute the s proxied setup command to prepare the auxiliary resources and local environment required for device-cloud joint debugging;
2) For common event functions without triggers or HTTP triggers, after the preparations are complete, start another new terminal, switch to the project path, and execute the s proxied invoke command to call the local function;
3) After the debugging task is completed, execute the s proxied cleanup command to clean up the auxiliary resources and the local environment required for the device-cloud joint debugging.
In addition to using the device-cloud joint debugging function through commands, we can also use the device-cloud joint debugging function in the VSCode developer tool, as shown in the figure.
Use the device-cloud joint debugging function in VSCode
3. Remote debugging
In addition to a channel service container locally, the device-cloud joint debugging also has a function computing container 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 to debug the project. At this time, you can choose to use remote debugging.

Compared with terminal-cloud joint debugging, remote debugging has only one channel service container locally, and the execution process depends entirely on the online; the remote function returns the execution result. The schematic diagram of the overall architecture of remote debugging is shown in the figure.

In addition to logging into the online environment through the channel service shown in the remote debugging architecture diagram above for code debugging or problem location, some cloud vendors also provide the function of directly logging in to the instance for code debugging. Taking Serverless Devs as an example, when using Alibaba Cloud Function Compute, 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 location in complex scenarios, after logging in to the instance, the user cannot directly locate the problem through function logs and monitoring indicators, and needs to use coredump, for example. , tcpdump, jmap and other tools for in-depth troubleshooting.

For example, a user found that some function error prompts appeared in his online program recently, and the content of the error report was that the connection to a remote service timed out. The user suspects that the network connection between the function instance and the remote service is unstable, so he wants to enter the instance to analyze the network situation between the instance and the remote service. At this point, we can follow the steps below to troubleshoot the problem.

1) As shown in the figure, after logging into the instance, you need to execute the two commands apt-get update and apt-get install tcpdump to install the tcpdump tool.
2) After the installation is complete, execute the tcpdump command to capture the request of the remote service IP, and save the captured result in the tcpdump.cap file.
3) After capturing the packets, use the OSS command-line tool ossutil64 to upload the tcpdump.cap file to your own OSS, and then download it locally for analysis with the analysis tool Wireshark.

4. Local debugging

(1) Command line tool
Most FaaS platforms provide users with relatively complete command-line tools, such as Alibaba Cloud's Funcraft, and there are also 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 the command line tool is very simple. Taking Serverless Devs as an example, the local debugging method of Alibaba Cloud Function Compute is as follows: first, ensure that there is a Function Compute project locally, and then execute debugging commands under the project, such as debugging in Docker, as shown below.
Local debugging via the command line
(2) Editor plug-in
Take the VSCode plug-in as an example. After downloading the VSCode plug-in of Alibaba Cloud Function Computing and configuring the account information, create a new function locally, and perform breakpoint debugging after clicking, as shown in the figure.

3. Dependency installation and project construction through developer tools

A big difference between application development under the serverless architecture and application development under the traditional architecture is that the content dimensions of the two are different. For example, under the ideal state, the former does not require people to pay attention to underlying resources such as servers.

But in today's serverless development stage, does application development under the serverless architecture really not require people to pay extra attention to servers? Actually not. Although the serverless architecture emphasizes the Noserver mind, in actual production, there are many dependencies that cannot be used across platforms. For example, some dependencies in the Python language require binary compilation, and the operating system, software environment, etc. There is a relatively large relationship, so if this kind of dependency is introduced into the project, it is necessary to install the dependency, package the code, or deploy the project in an environment that is consistent with the online environment of the function computing platform.

Alibaba Cloud Function Compute has a more detailed description of its own online function environment and provides corresponding documents. For example, executable files compiled with C, C++, and Go must be compatible with the operating environment of Function Compute.

The Python operating environment of Function Compute is as follows.

●Linux kernel version: Linux 4.4.24-2.al7.x86_64.
●Docker base image: docker pull python:2.7; docker pull python:3.6.

However, in the actual application development process, dependent packaging is still a headache for many developers. They will face similar challenges in the development of many Serverless applications: the project can run normally locally, but a certain dependency cannot be found once it is released online, but in fact the dependency exists. At this time, problem location becomes very important. difficult things.

At present, there are usually three ways to install dependencies or build projects for Serverless applications:
1) After creating the project locally, build the online environment by yourself according to the environment data provided by the cloud vendor, and then install the dependencies. This method is relatively autonomous and controllable, but it is very difficult and the operation is cumbersome.
2) Use the existing developer tools to install dependencies, as shown in the figure:
Taking Serverless Devs developer tools and Alibaba Cloud function computing products as examples, developers only need to prepare relevant dependency installation files for the corresponding language according to language habits, such as requirements.txt in Python language, package.json in Node.js language, etc. . Then, under the current project, execute the s build --use-docker command to complete the installation of dependencies in an environment consistent with the online environment of the Alibaba Cloud Function 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 according to the requirements.txt file, and compose the deliverable 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 dependencies can also be installed 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