This article discusses how you can run and debug functions in Function Compute locally through using the API.
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.
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.
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
Last, there's also Fun Local, which is a sub-command of Fun. You can use it directly
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
fun local invoke -hto 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
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.
fun local startto run the service. The execution log is as follows:
fun local start api 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.
pip install aliyun-fc2
import fc2 client = 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