すべての Cloud Functions のコードサンプル

This page contains code samples for Cloud Functions. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Avoid infinite retries

Discards all events older than 10 seconds.

View in documentation

Cached library

HTTP Cloud Function that uses a cached client library instance to reduce the number of connections required per function invocation.

View in documentation

Cloud Function Concepts: Function Termination

Signaling function termination in Node

Cloud Pub/Sub (2nd Gen)

Process a Cloud Pub/Sub message using Cloud Functions (2nd Gen) and Eventarc

Cloud Pub/Sub (2nd Gen) Unit Test

Example unit test for a Cloud Pub/Sub-triggered function running on Cloud Functions (2nd Gen).

Cloud Storage (1st Gen) Hello World

Example Storage-triggered function that runs on Cloud Functions (1st Gen).

Cloud Storage (2nd Gen)

Process a Cloud Storage event using Cloud Functions (2nd Gen) and Eventarc

View in documentation

Cloud Storage (2nd Gen) Unit Test

Example unit test for a Cloud Storage-triggered function running on Cloud Functions (2nd Gen).

Cloud Storage integration tests

Demonstrates how to integration test a function triggered by Cloud Storage.

View in documentation

Cloud Storage unit tests

Demonstrates how to unit test a function triggered by Cloud Storage.

View in documentation

Deploy Cloud Function 2nd gen with Audit Log trigger using Terraform

Full terraform config to deploy an event-driven Cloud Function 2nd gen with resources

Deploy Cloud Function 2nd gen with Cloud Storage trigger using Terraform

Full terraform config to deploy an event-driven Cloud Function 2nd gen with resources

Deploy Cloud Function 2nd gen with HTTP trigger using Terraform

Full terraform config to deploy a Cloud Function 2nd gen with resources

View in documentation

Deploy Cloud Function 2nd gen with Pub/Sub trigger using Terraform

Full terraform config to deploy an event-driven Cloud Function 2nd gen with resources

View in documentation

Environment variables

Demonstrates how to use environment variables within a Cloud Function.

View in documentation

Execution after HTTP response

Example of code that won't execute after an HTTP response is sent.

Execution after timeout

Sample that demonstrates code that should (but won't) execute after a GCF call times out.

File system

Shows how to access a Cloud Functions instance's file system.

Firebase Analytics

Triggers a function when a Firebase Analytics event is received.

Firebase Auth

Triggers a function when a Firebase Auth user object changes.

View in documentation

Firebase remote config

Process changes to Firebase remote config values.

View in documentation

Firebase RTDB trigger

Triggers a function when a Firebase realtime database is updated.

View in documentation

Firestore trigger

Triggers a function in response to a Firestore database update.

Global versus function scope

Creates a heavy object only once per function instance, and shares it across all function invocations reaching the given instance.

Hello World error

Demonstrates how to report an error.

View in documentation

HTTP Auth with CORS

HTTP function that supports CORS requests with credentials.

HTTP Bearer token

Shows how to make Google Cloud-authenticated HTTP requests.

View in documentation

HTTP CORS

Shows how to make CORS-enabled requests with Cloud Functions.

HTTP method types

Shows how to handle HTTP method types (such as GET, PUT, and POST) in Cloud Functions.

HTTP parse multipart/form-data

Cloud Function that parses HTTP form requests.

HTTP unit tests

Demonstrates how to unit test an HTTP function.

View in documentation

HTTP XML

Parses an HTTP request that contains "application/xml" content.

Lazy globals

Demonstrates waiting to initialize global variables until they're used.

View in documentation

Memorystore for Redis

Demonstrates a Cloud Function that connects to a managed Redis instance.

Node.js: terminate HTTP connection

Show how to terminate an HTTP connection in Node.js

View in documentation

PHP Info

Get PHP info

View in documentation

Process a Cloud Audit Logging log entry

Shows how to process an Audit Log entry.

Pub/Sub integration tests

Demonstrates how to integration test a function triggered by Pub/Sub.

View in documentation

Pub/Sub publish message

Demonstrates how to interact with Pub/Sub from Cloud Functions.

Pub/Sub Subscribe to a Topic

Demonstrates how to subscribe to a topic and parse the received event with Pub/Sub from Cloud Functions.

Pub/Sub system tests

Demonstrates how to system test a function triggered by Pub/Sub.

View in documentation

Pub/Sub unit tests

Demonstrates how to unit test a function triggered by Pub/Sub.

View in documentation

React to value change by updating a value

Updates a value whenever it is changed in Firestore.

Retry on error

Tells your function whether or not to retry execution when an error happens.

View in documentation

Send HTTP requests

Shows how to make an HTTP request from a Cloud Function.

Statelessness

An example of what variable state function instances do (and don't) store.

Trigger function from Cloud Logging

Triggers a function based on Cloud Logging entries.

Use HTTP connection pooling

Shows how to recycle HTTP connections using HTTP connection pools.

View in documentation