Run and Debug Functions locally through the API

Background Information

First, there’s Alibaba Cloud Function Compute, which is 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 approrpriate amount of computing resources to run user code. The user only pays for the resources required to run their code.

Fun Local Start Command Format

First, you can use fun local invoke -h to view the help information to involve Fun Local:

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

Trigger functions locally through the API

The command format is consistent with HTTP Triggers:

fun local start [options]
$ fun local startapi localdemo/php72 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/php72/invocations/
api localdemo/python27 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/python27/invocations/
api localdemo/python3 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/python3/invocations/
api localdemo/nodejs6 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/nodejs6/invocations/
api localdemo/nodejs8 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/nodejs8/invocations/
api localdemo/java8 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/java8/invocations/
function compute app listening on port 8000!
pip install aliyun-fc2
import fc2client = fc2.Client(endpoint='http://localhost:8000', accessKeyID='<your access key id>', accessKeySecret='your access key secret')resp = client.invoke_function('localdemo', 'php72')print resp.headers

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: