By Yi Xian
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 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.
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
The command format is consistent with HTTP Triggers:
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 start to 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.
First, install the fc python sdk:
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
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.
Alibaba Cloud Serverless - August 21, 2019
Alibaba Cloud Serverless - August 21, 2019
Amber Wang - August 6, 2018
Alibaba Clouder - June 5, 2019
amap_tech - December 2, 2019
Alibaba Clouder - April 15, 2019
Alibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.Learn More
Connect your business globally with our stable network anytime anywhere.Learn More
An end-to-end software-defined network solution for multinational enterprises.Learn More
OpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.Learn More
More Posts by Alibaba Cloud Serverless