Skip to main content

Local Cloud Function Development

Cloud functions can not only run in the cloud environment, but also run in the local environment via the tcb-ff command-line tool provided by Cloud Development, thus enabling convenient development and debugging of cloud functions locally.

Compared to cloud environment, local environment requires starting the cloud function with the tcb-ff command first, and then triggering its execution via http requests. Various methods of initiating http requests can invoke cloud functions, such as curl, postman, or request libraries provided by various programming languages, etc.

A sample project for local cloud function development can refer to: Full-stack Project Based on Function Cloud Hosting

The following are detailed steps for local cloud function development, which can be experienced by combining with the sample project.

Invoking Locally Running Cloud Functions

Methods to trigger cloud functions include:

  1. Invoke via the SDK provided by Cloud Development
    1. @cloudbase/js-sdk runs in browser environments, such as Mini Programs, web, etc.
    2. @cloudbase/node-sdk runs in server-side node.js environments, such as within cloud functions.
  2. Invoke via the curl command-line tool

Invoke via the SDK provided by Cloud Development

As shown in the figure below, it demonstrates A scenario where cloud function-A is called via @cloudbase/js-sdk, and then within cloud function-A, cloud function-B is called via @cloudbase/node-sdk.

Call chain: client (@cloudbase/js-sdk) ----> cloud function-A (@cloudbase/node-sdk) ----> cloud function-B

The Cloud Development SDK by default calls cloud functions running in the cloud environment. To invoke locally running cloud functions, requests intended for the cloud environment need to be redirected to the local cloud functions. This can be achieved by adding a local proxy layer to forward the requests.

call-local-cbrf

To invoke locally running cloud functions, first need to start a local proxy layer to forward requests to the locally running cloud functions.

Set up the local proxy layer service

whistle is a commonly used cross-platform network proxy tool that can forward requests to local. It implements the functionality of the local proxy layer described above, forwarding requests to locally running cloud functions.

whistle Reference: https://wproxy.org/whistle/install.html

Install whistle:

npm install -g whistle

Start whistle:

$ w2 start
[!] whistle@2.9.94 is running
[i] 1. use your device to visit the following URL list, gets the IP of the URL you can access:
http://127.0.0.1:8899/
http://10.0.0.123:8899/
http://192.168.0.123:8899/
Note: If all the above URLs are unable to access, check the firewall settings
For help see https://github.com/avwo/whistle
[i] 2. set the HTTP proxy on your device with the above IP & PORT(8899)
[i] 3. use Chrome to visit http://local.whistlejs.com/ to get started

Open the configuration page in the browser.

open http://127.0.0.1:8899/

Configure rules (rewrite requests to local):

^https://*.api.tcloudbasegateway.com/v1/cloudrun/*/* http://127.0.0.1:3000/$3?@fn=$2
^http://*.api.tcloudbasegateway.com/v1/cloudrun/*/* http://127.0.0.1:3000/$3?@fn=$2

Finally, set the browser HTTP proxy to http://127.0.0.1:8899. This can be configured using the Chrome browser extension ZeroOmega/SwitchyOmega. It can also be configured by setting up a system-level proxy. Executing the w2 proxy command provided by whistle configures the system proxy.

Invoke locally running cloud functions using @cloudbase/js-sdk

Using @cloudbase/js-sdk to invoke locally running cloud functions in the browser environment without modifying any code, simply by forwarding browser-initiated requests to the local whistle proxy server.

For example, the frontend code of the sample is as follows:

import cloudbase from '@cloudbase/js-sdk'

const tcbapp = cloudbase.init({
env: 'your-env-id',
clientId: 'your-client-id'
})

const auth = tcbapp.auth()

await auth.signInAnonymously()

// Call the cloudbaserunfunctions/helloworld function
const result = await tcbapp.callFunction({
name: 'helloworld',
type: 'cloudrun'
})

console.log('result:', result)

Complete sample project code can be viewed at: https://github.com/TencentCloudBase/cloudbase-examples/blob/master/cloudrunfunctions/fullstack-project/webapp/src/index.js

Invoke locally running cloud functions using @cloudbase/node-sdk

On the server side, use @cloudbase/node-sdk to invoke locally running cloud functions. When initializing with tcb.init({}), add the proxy parameter to forward requests to the local whistle proxy server.

Note: Generally, the proxy parameter only needs to be configured when invoking locally running cloud functions in a local environment. This parameter can be configured dynamically.

  const tcbapp = tcb.init({
// Here the Proxy is configured as the local proxy layer service Whistle
// Whistle can forward requests to locally running cloud functions
proxy: 'http://127.0.0.1:8899'
})

Complete code:

import tcb from '@cloudbase/node-sdk'

exports.main = async (event, context) => {
const { httpContext } = context
const { url, httpMethod } = httpContext

const tcbapp = tcb.init({
// Here the Proxy is configured as the local proxy layer service Whistle
proxy: 'http://127.0.0.1:8899',
context: {
extendedContext: {
tmpSecret: {
secretId: 'this-is-a-fak-secretId',
secretKey: 'this-is-a-fake-secretKey',
}
},
...context,
}
})

const result = await tcbapp.callFunction({
name: event.otherFuncName,

// Function Cloud Hosting parameters
type: 'cloudrun',
method: 'POST',
path: '/abc',
data: {
key1: 'test value 1',
key2: 'test value 2'
}
}, {
timeout: 5000
})

return { result }
}

Complete sample project code can be viewed at: https://github.com/TencentCloudBase/cloudbase-examples/blob/master/cloudrunfunctions/fullstack-project/cloudbaserunfunctions/call-other-fn/index.js

Invoke via the curl command-line tool

Using curl, you can directly initiate http requests to cloud functions without any configuration.

This method is similar to using the cloud function's default domain name and HTTP access service when invoking cloud functions running in the cloud.

# Send a GET request
curl -v -XGET http://localhost:3000/path/to/xxx

# Send a POST request
curl -v -XPOST 'http://127.0.0.1:3000/path/to/xxx \
-H 'Content-Type: application/json' \
--data-raw '{"name":"xxxxx"}'

# Send a PUT request
curl -v -XPUT 'http://127.0.0.1:3000/path/to/xxx \
-H 'Content-Type: application/json' \
--data-raw '{"name":"xxxxx"}'

# Send a DELETE request
curl -v -XDELETE 'http://127.0.0.1:3000/path/to/xxx

A sample request for developing an AI agent using cloud functions:

curl -XPOST 'http://127.0.0.1:3000/v1/aibot/bots/ibot-xxxx/send-message' \
-H 'Accept: text/event-stream' \
-H 'Content-Type: application/json' \
--data-raw '{"name":"xxxxx"}'

The above is an example of making a request via curl. You can initiate http requests using any other method, such as postman, browser fetch, node.js http.request, etc.