0
0
GcpHow-ToBeginner · 3 min read

How to Set Timeout for Google Cloud Functions

To set the timeout for a Google Cloud Function, use the --timeout flag with the gcloud functions deploy command or specify the timeout property in the function's deployment configuration. The timeout value controls how long the function can run before it stops automatically.
📐

Syntax

The timeout for a Google Cloud Function is set during deployment using the --timeout flag followed by the duration. The duration format is a number with a suffix: s for seconds, m for minutes, or h for hours.

Example: --timeout=60s sets the timeout to 60 seconds.

bash
gcloud functions deploy FUNCTION_NAME --runtime RUNTIME --trigger-http --timeout=TIMEOUT_DURATION
💻

Example

This example deploys a Cloud Function named helloTimeout with a timeout of 2 minutes (120 seconds). It uses the Node.js 18 runtime and triggers on HTTP requests.

bash
gcloud functions deploy helloTimeout --runtime nodejs18 --trigger-http --timeout=120s --entry-point=helloWorld
Output
Deploying function (may take a while)...done. availableMemoryMb: 256 entryPoint: helloWorld httpsTrigger: url: https://REGION-PROJECT_ID.cloudfunctions.net/helloTimeout timeout: 120s runtime: nodejs18 status: ACTIVE
⚠️

Common Pitfalls

  • Timeout too short: Setting a timeout shorter than the function's actual execution time causes premature termination.
  • Timeout too long: Setting a very long timeout can lead to higher costs and resource locking.
  • Incorrect format: Timeout must include a suffix like s, m, or h. Omitting it causes deployment errors.
  • Default timeout: If not set, the default timeout is 60 seconds.
bash
Wrong:
gcloud functions deploy myFunc --timeout=120

Right:
gcloud functions deploy myFunc --timeout=120s
📊

Quick Reference

OptionDescriptionExample
--timeoutSets max execution time for the function--timeout=90s
Duration formatNumber + suffix: s (seconds), m (minutes), h (hours)120s, 2m, 1h
Default timeoutIf not set, defaults to 60 secondsN/A
Max timeoutMaximum allowed is 540 seconds (9 minutes)540s

Key Takeaways

Set timeout using the --timeout flag during deployment with a valid duration suffix.
Timeout controls how long a function can run before it stops automatically.
Default timeout is 60 seconds if not specified.
Use a timeout value that matches your function's expected execution time to avoid errors or extra costs.
Timeout format must include a suffix: s, m, or h.