Add a Google Cloud Functions example
Adds a new example of deploying Google Cloud Functions.
ADD
gcp-ts-functions/Pulumi.yaml
+name: gcp-functions
+runtime: nodejs

ADD
gcp-ts-functions/README.md
+# GCP Functions
+
+An example Pulumi component that deploys a TypeScript function to Google Cloud Functions.
+
+## Running the App
+
+1. Create a new stack:
+
+ ```
+ $ pulumi stack init gcp-fn
+ ```
+
+1. Configure GCP project and region:
+
+ ```
+ $ pulumi config set gcp:project <projectname>
+ $ pulumi config set gcp:region <region>
+ ```
+
+1. Restore NPM dependencies:
+
+ ```
+ $ npm install
+ ```
+
+1. Build the Typescript project:
+
+ ```
+ $ npm run build
+ ```
+
+1. Run `pulumi update` to preview and deploy changes:
+
+ ```
+ $ pulumi update
+ Previewing changes:
+ ...
+
+ Performing changes:
+ ...
+ info: 6 changes performed:
+ + 6 resources created
+ Update duration: 39.65130324s
+ ```
+
+1. Check the deployed function endpoint:
+
+ ```
+ $ pulumi stack output url
+ https://us-central1-pulumi-development.cloudfunctions.net/greeting-function-7f95447
+ $ curl "$(pulumi stack output url)"
+ Greetings from Google Cloud Functions!
+ ...
+ ```

ADD
gcp-ts-functions/gcpFunction.ts
+import * as pulumi from "@pulumi/pulumi";
+import * as gcp from "@pulumi/gcp";
+import * as express from "express";
+
+// Note: The `HttpFunction` helper defined here will in the future be made available via a `@pulumi/gcp-serverless`
+// package, and extended to support additional configuration options similar to that available in
+// `@pulumi/aws-serverless`.
+
+// The callback type of a handler for a Google Cloud Functions HTTP Function.
+// See https://cloud.google.com/functions/docs/writing/http.
+export type HttpHandler = (req: express.Request, resp: express.Response) => void;
+
+// HttpFunction creates a Google Cloud Function HTTP Function from the provided JavaScript callback.
+export class HttpFunction extends pulumi.ComponentResource {
+ public bucket: gcp.storage.Bucket;
+ public bucketObject: gcp.storage.BucketObject;
+ public function: gcp.cloudfunctions.Function;
+
+ public httpsTriggerUrl: pulumi.Output<string>;
+
+ constructor(name: string, handler: HttpHandler, options?: pulumi.ResourceOptions) {
+ super("gcp:serverless:HttpFunction", name, {}, options);
+
+ // Serialize JavaScript callback to text
+ const serializedFunction = pulumi.runtime.serializeFunction(handler);
+ const functionText = serializedFunction.then(x => x.text);
+ const functionEntryPoint = serializedFunction.then(x => x.exportName);
+
+ // Create a bucket and object to store the function ZIP file
+ this.bucket = new gcp.storage.Bucket(`${name}-bucket`, {}, { parent: this });
+ this.bucketObject = new gcp.storage.BucketObject(`${name}-object`, {
+ bucket: this.bucket.name,
+ source: new pulumi.asset.AssetArchive({
+ "index.js": new pulumi.asset.StringAsset(functionText),
+ }),
+ }, { parent: this });
+
+ // Create the Function resource
+ this.function = new gcp.cloudfunctions.Function(`${name}-function`, {
+ sourceArchiveBucket: this.bucket.name,
+ sourceArchiveObject: this.bucketObject.name,
+ entryPoint: functionEntryPoint,
+ triggerHttp: true,
+ }, { parent: this });
+
+ // Make the trigger URL easily accessible
+ this.httpsTriggerUrl = this.function.httpsTriggerUrl;
+ }
+}
\ No newline at end of file

ADD
gcp-ts-functions/index.ts
+import * as gcpFunction from "./gcpFunction";
+
+let greetingFunction = new gcpFunction.HttpFunction("greeting", (req, res) => {
+ res.send(`Greetings from ${req.body.name || 'Google Cloud Functions'}!`);
+});
+
+export let url = greetingFunction.httpsTriggerUrl;

ADD
gcp-ts-functions/package.json
+{
+ "name": "gcp-ts-functions",
+ "version": "1.0.0",
+ "main": "bin/index.js",
+ "typings": "bin/index.d.ts",
+ "scripts": {
+ "build": "tsc"
+ },
+ "devDependencies": {
+ "@types/node": "^10.3.3",
+ "@types/express": "^4.16.0",
+ "typescript": "^2.9.2"
+ },
+ "dependencies": {
+ "@pulumi/gcp": "^0.14.3",
+ "@pulumi/pulumi": "^0.14.3"
+ }
+}

ADD
gcp-ts-functions/tsconfig.json
+{
+ "compilerOptions": {
+ "outDir": "bin",
+ "target": "es6",
+ "lib": [
+ "es6"
+ ],
+ "module": "commonjs",
+ "moduleResolution": "node",
+ "sourceMap": true,
+ "experimentalDecorators": true,
+ "pretty": true,
+ "noFallthroughCasesInSwitch": true,
+ "noImplicitAny": true,
+ "noImplicitReturns": true,
+ "forceConsistentCasingInFileNames": true,
+ "strictNullChecks": true
+ },
+ "files": [
+ "gcpFunction.ts",
+ "index.ts",
+ ]
+}

Add a Google Cloud Functions example

Adds a new example of deploying Google Cloud Functions.