Run and Debug Functions locally through the API

By Yi Xian

Image for post
Image for post

In previous blogs, I have discussed how you can develop Function compute so to trigger function execution and debugging processes through event and HTTP triggers. Now, this article turns to how to run and debug functions locally through the API. First, let’s take a look at several important concepts mentioned in this article.

Background Information

Then there’s Fun, which is a tool that supports serverless application deployment and allows easy management of 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.

Last, there’s also Fun Local, which is 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.

Note: The techniques described in this article are applicable to Fun 2.8.0 or later.

Fun Local Start Command Format

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

fun local start [options]

Note that Options can be omitted from the above command.

After you run the fun local start command, Fun first starts an HTTP server to provide HTTP services. Then, Fun scans all the functions described in template.yml and registers functions for which no HTTP Triggers are configured with the HTTP server. After the registration, these functions can be invoked by using the InvokeFunction API or SDK.

Direct access through the API requires signing, so you need to complete the Authorization. In this example, invocation through the SDK is recommended. The code is available on GitHub.

First, use fun local start to run the service. The execution log is as follows:

$ 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!

After running the service, you can use the Python SDK for invocation.

First, install the fc python sdk:

pip install aliyun-fc2

Write code:

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
print resp.data

Note: The accessKeyId and the accessKeySecret configured in the SDK must be consistent with the configurations in Fun. Otherwise, the signature authentication would fail during the invocation.

The following figure shows the execution process.

Image for post
Image for post

Original Source

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

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