09 Function Debugging and Deployment

09 Function Debugging and Deployment #

Function Debugging #

Debugging and deployment of functions are actually two separate activities. Debugging is generally more challenging, as debugging itself is a major weakness of the Serverless architecture.

Before discussing debugging, let me ask you a question: why do we need to debug our code after writing it? Can’t we just skip the debugging process?

I remember when I was in school, no matter if it was an exam or any other task, there was always a process of checking. For example, when we finished our homework, our parents would say, “Check it when you’re done”; during exams, teachers would say, “Check it after you finish.” After completing any task, we have a checking process to ensure that we make as few mistakes as possible.

The same goes for programming. After writing a chunk of code, it is inevitable that the code will do some strange things, such as missing a letter, using the wrong symbol, or the program output not matching expectations. There could be logical issues with our program, where certain business factors were not considered. There are many potential problems with our program. At this point, we need to check it ourselves to see if it is OK. If it’s not OK, we need to fix it immediately. If it is OK, then we can proceed to submit, upload, and deploy the code. This process can be considered as the debugging process.

1. Ways to Debug Functions #

Of course, debugging is not that easy; it has many methods and theories. In this article, I will explain how to debug functions in Function Compute and related tools.

1) Cloud Debugging

First, the easiest method is to open your browser, go to your project page, and click the Invoke button to debug.

Figure 1

(Online debugging)

In the above figure, you can see that we have completed the debugging and the output is “hello world”. This method is very simple and effective for temporary use.

However, for more complex projects, online debugging may not be suitable. In such cases, we need local development and debugging methods because most of the code development should be done locally. Although everyone says that coding, developing, and debugging in the cloud is the future trend, at least for now, local development is still more familiar and reliable.

Therefore, we need to use plugins or command-line tools for local development and debugging.

2) Command-Line Tools

Figure 2

(Local debugging using command-line tools)

After installation, if you want to perform local debugging, you also need to install Docker. Once installed, you can use the command “invoke local” to invoke the function locally. As shown in the above figure, we can see that when I executed “fun local invoke demo03/demo03”, the result was successfully output. If you are using it for the first time, you may need to go through the process of pulling the Docker image.

3) VSCode Plugin

What if you want to write code in an editor and debug it? It’s very simple. Use the VSCode plugin. Just click the “Run” button in the plugin, and it will automatically launch Docker to help us debug the code locally.

Figure 3

From the above figure, you can see that we have successfully output the result.

At this point, someone might ask, “Do I still need to install Docker? Can I debug without Docker?” The answer is no. Docker is necessary because the debugging mechanism itself relies on Docker. However, human beings are often creative: if we don’t have the necessary conditions, we create them. So, below I will also share a debugging solution without any tools.

4) Tool-Free Debugging

Figure 4

Taking Python as an example, as shown in the figure above, we just need to add a piece of code to invoke our function. As for the event, it can be based on the trigger situation we are about to use. This way, we can achieve a simple debugging method.

2. Appropriate Scenarios #

After introducing so many debugging methods, when should we use which one?

Figure 5

As shown in the above figure, let’s compare the methods. In general, for small-scale scenarios where we don’t want to open an editor or use Docker, and we want to have more reliable debugging, we can use online debugging. If we want to do local debugging and be closer to the development process, we can use command-line tools or the VSCode plugin. If we don’t want to install various tools, then we can use the tool-free debugging solution.

Although online debugging may not align with our development habits, this method can simulate the “live venue” 100%. Command-line tools or the VSCode plugin have simulated the online environment as much as possible through Docker image methods. However, these methods may not solve similar problems well in certain scenarios where interactions with online resources, especially through a VPC or other resources, are involved. Tool-free debugging is only suitable for temporary use. Its environment is completely different from the online environment and may have a significant impact on the actual results when the code goes live.

Function Deployment #

Function deployment is a simple process that doesn’t require any special dependencies and can be done even without Docker.

1. Online Deployment #

You can create a function online and upload the code package, or update a function by uploading the code package, etc.

2. Client Deployment #

1) Using Command-Line Tools

As shown in the figure below, you can deploy the function by running the “fun deploy” command via the command-line tool.

Figure 6

2) Using the VSCode Plugin

Using the VSCode plugin, you can click the deploy button to automatically deploy the function.

Figure 7

Conclusion #

Finally, I would like to mention that this article is not meant to boast, but the “-h” command in the command-line tool is really great. Regardless of the command used, we can view the usage instructions by using “-h”. It is very simple and convenient. You can try it secretly if you don’t believe it.