Skip to main content

How to Migrate Cloud Function Code to Cloud Hosting

If you need to migrate existing cloud function code to function-based cloud hosting, you can refer to the following content to learn about some changes that need to be made.

Adjustments to Function Code Writing

Entry Function Differences

Cloud Function Writing:

export const main = function(event, context) {}

Function Cloud Hosting Writing:

export const main = function(event, context) {}

Both functions share the same entry function format, but their parameter content formats have some differences:

  • `event`: The event parameter passed when the cloud function is triggered. Both function types are essentially the same, but Function Cloud Hosting supports more types of input formats.
  • context: The context parameter of the cloud function runtime, which is completely different between the two function types.

Therefore, if you use the context parameter in cloud functions, some adjustments may be required when migrating to function-based cloud hosting.

Function Return Value Differences

@cloudbase/node-sdk Usage Differences

Cloud Function Writing:

import tcb from '@cloudbase/node-sdk'

// tcb.init can be placed outside the entry function
tcb.init({ env: 'abc-xyz' })

export const main = function(event, context) {
// tcb.init can be placed inside the entry function
const tcbapp = tcb.init({ env: 'abc-xyz' })
// ...
return 'done'
}

Function Cloud Hosting Writing:

import tcb from '@cloudbase/node-sdk'

export const main = function(event, context) {
// tcb.init must be placed inside the entry function because it depends on the context parameter and therefore must reside within the `main` function.
const tcbapp = tcb.init({ context: context, env: 'abc-xyz' })
// ...
return 'done'
}

In Function Cloud Hosting, tcb.init requires the context parameter to obtain relevant information, so it must be placed inside the main function during use.

For how to use the capabilities of @cloudbase/node-sdk in Function Cloud Hosting, refer to the How to Invoke Other TCB CloudBase Capabilities? documentation.

Adjustments to Directory Structure

Sample Cloud Function Code Directory Structure:


function_nodejs /
- index.js
- package.json

Function Cloud Hosting Sample Code Directory Structure

  • A single service contains one function logic:

runfunction_nodejs /
- index.js
- package.json

  • A single service contains multiple function logics:
runfunction_nodejs /
- cloudbase-functions.json # Multiple functions configuration file
- cloudrunfunctions/funcA # Function A directory
- index.js
- cloudrunfunctions/funcB # Function B directory
- index.mjs
- package.json # Specifies index.mjs as the entry file

In Function Cloud Hosting, you can either organize code according to the original function directory structure or integrate multiple function logics within a single Cloud Hosting service.

In actual use, it is highly recommended to integrate multiple functions into a single service. This approach fully utilizes the running instance resources of the Cloud Hosting service, reduces cold starts, and simplifies service management.

Adjustments to Function Invocation Methods

Sample method for invoking cloud functions in a mini program:

wx.cloud
.callFunction({
// Cloud Function name
name: "hello_world",
// Parameters passed to the cloud function
data: {
a: 1,
b: 2,
},
})
.then((res) => {
console.log(res.result); // 3
})
.catch(console.error);

Example method for invoking Function Cloud Hosting in a mini program:

  • A single service contains one function logic:
// Environment id is required for container invocation and cannot be empty
const c1 = new wx.cloud.Cloud({
resourceEnv: "Environment id",
});
await c1.init();

const r = await c1.callContainer({
path: "/", // Enter the custom business path
header: {
"X-WX-SERVICE": "xxx", // Enter the service name
},
// Other parameters are the same as wx.request
method: "POST",
});
console.log(r);


  • A single service contains multiple function logics:
// Environment id is required for invocation and cannot be empty
const c1 = new wx.cloud.Cloud({
resourceEnv: 'environment id' // Fill in your CloudBase environment id
})
await c1.init()

const r1 = await c1.callContainer({
path: '/', // request path for the default function
header: {
'X-WX-SERVICE': 'testfunc2', // Enter the function name used during creation, testfunc2
},
// Other parameters are the same as wx.request
method: 'GET',
})
console.log(r1) // Prints Hello world!

const r2 = await c1.callContainer({
path: '/echo', // request path for the echo function
header: {
'X-WX-SERVICE': 'testfunc2', // Enter the function name used during creation, testfunc2
},
// Other parameters are the same as wx.request
method: 'POST',
data: {
a: 1,
b: 2
}
})
console.log(r2) // Prints the specific request and time

For more invocation methods of Function-based Cloud Hosting, see the documentation.