Run and Debug HTTP Trigger Configured Functions Locally

Background Information

__Function Compute__: an event-driven service that allows users to write and upload code without having to manage server health or consider some other factors. Function Compute prepares and auto-scales to the correct amount of computing resources to run user code. The user only pays for the resources required to run their code.
__Fun__: a deployment tool for serverless applications. It helps you manage resources, such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file.
__Fun Local__: a sub-command of Fun. You can use it directly through the fun local command. The Fun Local tool can fully simulate and run the functions in Function Compute locally and provides the single-step debugging feature, which makes up for Function Compute shortcomings when compared with traditional application development experience, and provide users with a new way to solve Function Compute problems.

Fun Local Start Command Format

Before we get into other things in this blog, let’s look at how to use fun local invoke -h by viewing the help information:

Usage: fun local start [options]  Allows you to run the Function Compute application locally for quick development & testing.
It will start an http server locally to receive requests for http triggers and apis.
It scans all functions in template.yml. If the resource type is HTTP, it will be registered to this http server, which can be triggered by the browser or some http tools.
For other types of functions, they will be registered as apis, which can be called by sdk in each language or directly via api.
Function Compute will look up the code by CodeUri in template.yml.
For interpreted languages, such as node, python, php, the modified code will take effect immediately, without restarting the http server.
For compiled languages such as java, we recommend you set CodeUri to the compiled or packaged localtion.
Once compiled or packaged result changed, the modified code will take effect without restarting the http server.
Options: -d, --debug-port <port> specify the sandboxed container starting in debug mode, and exposing this port on localhost
-c, --config <ide/debugger> output ide debug configuration. Options are vscode
-h, --help output usage information

Run HTTP Trigger Configured Functions Locally

The format of the run command is as follows:

fun local start [options]

Debug the HTTP Trigger Configured Functions Locally

Note: All the debugging technologies involved in Fun Local are implemented based on the debugging protocol common to various languages. Therefore, developers of any language can use the remote debugging method of the corresponding language for debugging, even if they do not like to use VSCode.

skip pulling image aliyunfc/runtime-python3.6:1.2.0...
you can paste these config to .vscode/launch.json, and then attach to your running function
///////////////// config begin /////////////////
{
"version": "0.2.0",
"configurations": [
{
"name": "fc/local-http-demo/python3",
"type": "python",
"request": "attach",
"host": "localhost",
"port": 3000,
"pathMappings": [
{
"localRoot": "/Users/tan/fun_local_http_demo/python3",
"remoteRoot": "/code"
}
]
}
]
}
///////////////// config end /////////////////
FunctionCompute python3 runtime inited.
FC Invoke Start RequestId: 04c57fba-cbe9-4c1f-8c57-f8e0b539fa08

Thermal Loading

Thermal loading is supported when the HTTP trigger configured function is run and debugged locally. After the local service is started through the fun local start command, even if the code is modified, the local service does not need to be restarted, but instead the changed function can be run by directly refreshing the web page or re-triggering the function.

Sample code

The sample code involved in this article is hosted on GitHub. The directory structure of the project is as follows:

.
├── nodejs6
│ ├── README.md
│ ├── index.js
│ └── package.json
├── nodejs8
│ ├── README.md
│ ├── index.js
│ └── package.json
├── php7.2
│ ├── README.md
│ └── index.php
├── python2.7
│ ├── README.md
│ └── index.py
├── python3
│ ├── README.md
│ └── index.py
└── template.yml

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com