Skip to main content

Cloud Function V2

New CloudBase environments will gradually use Cloud Function V2. Cloud Function V2 is billed by instance running time instead of by invocation count, and its specifications combine CPU and memory. Existing environments keep using Cloud Function V1 (the original cloud function), with no change to features or billing.

This page describes the differences between the two, and what to note when you write code, access the database, and estimate costs in Cloud Function V2.

One type of cloud function per environment

An environment has only one type of cloud function: either Cloud Function V1 or Cloud Function V2. Existing environments are not switched automatically, and there is no upgrade button. See Existing environments and migration.

Check which version an environment uses​

Option 1: In the console. The cloud function page shows whether the current environment uses "Cloud Function V1" or "Cloud Function V2".

Option 2: Call the environment info API. Call DescribeEnvInfo and check the cloud function backend type in the response:

{
"Functions": [
{ "Backend": "cbf" }
]
}
Backend valueMeaning
cbfCloud Function V2
scfCloud Function V1

Differences from Cloud Function V1​

CapabilityCloud Function V1Cloud Function V2Details
RuntimeNode.js, Python, Go, PHP, Java, and moreNode.js 24 onlyRuntime
Function typesRegular cloud functions, HTTP cloud functionsRegular cloud functions, HTTP cloud functions—
SpecificationsChoose by memoryChoose a specification tier, CPU:memory = 1:2, starting at 0.5 core 1 GBSpecifications
Maximum execution time per invocationRegular cloud functions 900 seconds, HTTP cloud functions 7200 seconds900 secondsSpecifications
Multiple concurrent requests per instanceConfigurableEnabled by default, no configuration neededSpecifications
Access to the environment's database and storageTemporary credentials issued automaticallyAvailable after you turn on "API Key Settings"Access database and storage
Total size of environment variables4 KB8 KBEnvironment variables
Public network accessSupportedSupported, egress IP not fixedNetwork
Private network (VPC)SupportedNot supported yetNetwork
Fixed egress IPSupportedNot supported yetNetwork
Streaming output (SSE)SupportedSupported—
WebSocketSupportedSupported (HTTP cloud functions)—
LayersSupportedNot supportedUnsupported features
Provisioned concurrencySupportedNot supportedUnsupported features
Timer triggersSupportedNot supported yetUnsupported features
Versions and trafficPublish versions, split traffic by percentagePublish versions; split traffic by percentage (up to 2 versions) or by request header—
BillingExecution time × memory (GBs) + invocations + outbound public trafficInstance running time (CU) + outbound public trafficBilling

Runtime​

Cloud Function V2 supports Node.js 24 only (set Runtime to Nodejs24.11 or Nodejs24.14 when you create a function). Both regular cloud functions and HTTP cloud functions can use it.

For Python, Go, Java, PHP, or other languages, use CloudRun.

Specifications​

Cloud Function V2 offers CPU and memory in specification tiers. The CPU-to-memory ratio is fixed at 1:2:

SpecificationUsage per hour of runningNotes
0.5 core 1 GB0.5 CUDefault
1 core 2 GB1 CU
2 cores 4 GB2 CU

Other limits:

ItemLimit
Maximum execution time per invocation900 seconds
Multiple concurrent requests per instanceEnabled by default; one instance can handle multiple requests at the same time
Cold start timeAbout 1–2 seconds

Access database and storage​

Cloud Function V1 issues temporary credentials to functions automatically, so no configuration is needed in code. Cloud Function V2 does not, so complete the following three steps.

1. Install the Node SDK​

Use @cloudbase/node-sdk 4.x:

npm install @cloudbase/node-sdk@latest
caution

Version 3.x cannot access the database, storage, and other resources properly in Cloud Function V2. Upgrade to 4.x.

2. Turn on "API Key Settings" for the function​

  1. In the console, open the function configuration and find "Environment Variables".
  2. Turn on "API Key Settings" and select an API key of this environment (create one if none exists).
  3. After you save, the function gets an environment variable named CLOUDBASE_APIKEY.

3. Initialize the SDK​

const tcb = require('@cloudbase/node-sdk')

// The SDK reads TCB_ENV and CLOUDBASE_APIKEY automatically
const app = tcb.init({ env: tcb.SYMBOL_CURRENT_ENV })

exports.main = async (event) => {
// Read and write PostgreSQL in this environment
const { data, error } = await app.rdb().from('orders').select('*').limit(1)
return { data, error }
}

Notes:

  • Do not write the API key into code or commit it to a code repository. Always use it through environment variables.
  • Read the environment ID from TCB_ENV in the function.

Environment variables​

  • The total size of environment variables is limited to 8 KB (all names and values combined, including the API key).
  • Variable names starting with TCB_ or AGS_ are reserved by the system and cannot be set.

Network​

CapabilityCloud Function V2
Public network accessSupported, egress IP not fixed
Private network (VPC) accessNot supported yet
Fixed egress IPNot supported yet
Need VPC or a fixed egress IP

If your service needs to connect to resources in your own VPC, or a third-party service requires adding the egress IP to an allowlist (such as WeChat Pay or WeCom), use CloudRun for now. Upgrading your plan does not add these two capabilities to Cloud Function V2.

Unsupported features​

FeatureCloud Function V2Alternative
LayersNot supportedBundle dependencies into the code package
Provisioned concurrencyNot supportedNo configuration needed; multiple concurrent requests per instance are enabled by default
Timer triggersNot supported yetUse CloudRun for scheduled tasks
Private network (VPC), fixed egress IPNot supported yetUse CloudRun
Languages other than Node.jsNot supportedUse CloudRun
Promotional resource packsNot applicable—

If you pass a configuration that Cloud Function V2 does not support (such as VPC or layers) through the API or CLI, an error is returned that names the field.

Billing​

Cloud Function V1: by execution time and invocations​

Billing itemPrice
Resource usage (execution time × configured memory)0.11108 resource points / GBs
Invocations13.3 resource points / 10,000 invocations
Outbound public traffic800 resource points / GB

Cloud Function V2: by instance running time​

Billing itemPrice
Compute usage220 resource points / CU
Outbound public traffic800 resource points / GB
  • 1 CU is an instance of the 1 core 2 GB specification running for 1 hour. 0.5 core 1 GB running for 1 hour is 0.5 CU, and 2 cores 4 GB running for 1 hour is 2 CU.
  • Running time starts when an instance starts successfully and ends when the instance is destroyed, including the time the instance is kept after requests finish. The instance startup (cold start) is not billed.
  • An instance that runs for less than 1 minute is billed as 1 minute. New requests that arrive while the instance is kept reuse it and are not billed again.
Invocations

Cloud Function V2 itself is not billed by invocation count. When you call a function through the HTTP access service or the CloudBase API, these requests are still billed as gateway invocations, the same as calls to other resources. See the HTTP gateway billing adjustment announcement.

Example​

Using 0.5 core 1 GB (the default specification):

ScenarioCalculationResource usage
One invocation takes 200 ms, with no other requests for a long time before or afterThe instance runs less than 1 minute, billed as 1 minute0.5 CU × 1/60 ≈ 0.0083 CU, about 1.83 resource points
One instance keeps handling requests for 1 hourBilled as the instance running for 1 hour0.5 CU, 110 resource points

Cloud Function V2 is billed by how long instances run, not by how many milliseconds your code executes. The more concentrated the requests, the more requests one instance handles and the lower the cost per request. With sporadic invocations, the 1-minute minimum makes up most of the cost.

View usage​

View Cloud Function V2 resource usage in "Plan Usage" in the console. In usage details, 3600 core-seconds = 1 CU.

Existing environments and migration​

  • Existing environments keep using Cloud Function V1, with no change to features or billing.
  • Existing environments are not switched to Cloud Function V2, and there is no upgrade button.
  • An environment has only one type of cloud function.
  • No tool is currently provided for migrating from Cloud Function V1 to Cloud Function V2.

FAQ​

Will my environment be switched to Cloud Function V2 automatically?

No. Existing environments keep using Cloud Function V1.

What should I do when Cloud Function V2 reports "missing credentials"?

Check two things: whether @cloudbase/node-sdk is 4.x, and whether "API Key Settings" is turned on in the function configuration. See Access database and storage.

Why am I still billed after a request finishes?

After a request finishes, the instance is kept for a period so the next request can use it directly without another cold start. The kept instance still occupies resources, so the time counts toward running time.

Can Cloud Function V2 connect to my VPC or use a fixed egress IP?

Not yet. Use CloudRun for now. See Network.

How do I view function logs and monitoring?

View them in "Logs & Monitoring" in the console.