Building Google Cloud Platform Solutions
上QQ阅读APP看书,第一时间看更新

Integrating with other Google services

One of the primary use cases of Cloud Functions is to act as the glue that integrates various services across the GCP catalog. This is an ideal use case for Cloud Functions as there is often very little code involved and invocations can be relatively sporadic or infrequent. The default service account for Cloud Functions has project editor rights. This means that functions leveraging the default service account will automatically have authorization to act on most Google Cloud APIs.

Services may pull in any number of Google Cloud client libraries (or any npm package) as part of package.json. Once included, these libraries may be imported and used as in a traditional Node.js application.

Be careful that your application does not leverage third-party libraries in a way that creates background processes. These processes may continue across function invocations leading to unpredictable behavior, or cause invocations to timeout.

As an example, try deploying the listBucket function available in chapter_06/example_02 in this book's source code. Notice that package.json includes a reference to @google-cloud/storage. During deployment, GCF will install this dependency for you. Also, be sure to update the config.bucketName in package.json to a bucket that exists within your project. To deploy the example, run the following:

gcloud beta functions deploy listBucket --trigger-http --entry-point=listBucket

As we'll see in later chapters, Cloud Functions make it extremely easy to build powerful solutions by orchestrating workflows across multiple Google Cloud products and services. In Chapter 10, Google Cloud Storage, we'll use Cloud Functions to perform image analysis on files as they're uploaded to Cloud Storage with the help of the Google Cloud Vision API. In Chapter 14, Messaging with Pub/Sub and IoT Core,  we'll see how Cloud Functions can be used with Cloud Pub/Sub to transparently extend existing service functionality.