Skip to content

Commit 0c530b4

Browse files
author
Vivek Lakshmanan
authored
Merge pull request #967 from pulumi/vl/GCPNativeTSFunctions
Google-Native port of ts-functions
2 parents 113ef71 + bd3e8f4 commit 0c530b4

File tree

6 files changed

+173
-0
lines changed

6 files changed

+173
-0
lines changed
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: google-cloud-ts-functions
2+
runtime: nodejs
3+
description: Google Cloud Functions example
4+
template:
5+
config:
6+
google-native:project:
7+
description: The Google Cloud project to deploy into
8+
google-native:region:
9+
description: The Google Cloud region

google-native-ts-functions/README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Google Cloud Functions
4+
5+
An example of deploying an HTTP Google Cloud Function endpoint using Google Cloud Native provider and TypeScript.
6+
7+
## Prerequisites
8+
9+
0. [Ensure you have the latest Node.js and NPM](https://nodejs.org/en/download/)
10+
2. [Install the Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
11+
3. [Configure Pulumi to access your Google Cloud account](https://www.pulumi.com/docs/intro/cloud-providers/google/setup/)
12+
13+
## Running the App
14+
15+
1. Restore NPM dependencies:
16+
17+
```
18+
$ npm install
19+
```
20+
21+
2. Create a new stack:
22+
23+
```
24+
$ pulumi stack init google-fn
25+
```
26+
27+
3. Configure your Google Cloud project and region:
28+
29+
```
30+
$ pulumi config set google-native:project <projectname>
31+
$ pulumi config set google-native:region <region>
32+
```
33+
34+
4. Run `pulumi up` to preview and deploy changes:
35+
36+
```
37+
$ pulumi up
38+
Previewing changes:
39+
...
40+
41+
Performing changes:
42+
...
43+
info: 6 changes performed:
44+
+ 6 resources created
45+
Update duration: 39.65130324s
46+
```
47+
48+
5. Check the deployed function endpoint:
49+
50+
```
51+
$ pulumi stack output url
52+
https://us-central1-pulumi-development.cloudfunctions.net/greeting-function-7f95447
53+
$ curl "$(pulumi stack output functionUrl)"
54+
Greetings from Google Cloud Functions!
55+
```
56+
57+
6. Clean up your Google Cloud and Pulumi resources:
58+
59+
```
60+
$ pulumi destroy
61+
...
62+
$ pulumi stack rm
63+
...
64+
```

google-native-ts-functions/index.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2016-2021, Pulumi Corporation.
2+
3+
import * as gcloud from "@pulumi/google-native";
4+
import * as pulumi from "@pulumi/pulumi";
5+
import * as random from "@pulumi/random";
6+
7+
const config = new pulumi.Config("google-native");
8+
const project = config.require("project");
9+
const region = config.require("region");
10+
11+
const randomString = new random.RandomString("name", {
12+
upper: false,
13+
number: false,
14+
special: false,
15+
length: 8,
16+
});
17+
18+
const bucketName = pulumi.interpolate`bucket-${randomString.result}`;
19+
const bucket = new gcloud.storage.v1.Bucket("bucket", {
20+
project: project,
21+
bucket: bucketName,
22+
name: bucketName,
23+
});
24+
25+
const archiveName = "zip";
26+
const bucketObject = new gcloud.storage.v1.BucketObject(archiveName, {
27+
object: archiveName,
28+
name: archiveName,
29+
bucket: bucket.name,
30+
source: new pulumi.asset.AssetArchive({
31+
".": new pulumi.asset.FileArchive("./pythonfunc"),
32+
}),
33+
});
34+
35+
const functionName = pulumi.interpolate`func-${randomString.result}`;
36+
const func = new gcloud.cloudfunctions.v1.Function("function-py", {
37+
projectsId: project,
38+
locationsId: region,
39+
functionsId: functionName,
40+
name: pulumi.interpolate`projects/${project}/locations/${region}/functions/${functionName}`,
41+
sourceArchiveUrl: pulumi.interpolate`gs://${bucket.name}/${bucketObject.name}`,
42+
httpsTrigger: {},
43+
entryPoint: "handler",
44+
timeout: "60s",
45+
availableMemoryMb: 128,
46+
runtime: "python37",
47+
ingressSettings: "ALLOW_ALL",
48+
});
49+
50+
const invoker = new gcloud.cloudfunctions.v1.FunctionIamPolicy("function-py-iam", {
51+
projectsId: project,
52+
locationsId: region,
53+
functionsId: functionName, // func.name returns the long `projects/foo/locations/bat/functions/buzz` name which doesn't suit here
54+
bindings: [
55+
{
56+
members: ["allUsers"],
57+
role: "roles/cloudfunctions.invoker",
58+
},
59+
],
60+
}, { dependsOn: func});
61+
62+
export const functionUrl = func.httpsTrigger.url;
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "google-cloud-ts-functions",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"@types/node": "^8.0.0",
6+
"@types/express": "^4.16.0"
7+
},
8+
"dependencies": {
9+
"@pulumi/google-native": "^0.0.1a",
10+
"@pulumi/pulumi": "^3.0.0a",
11+
"@pulumi/random": "^4.0.0a"
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def handler(request):
2+
headers = {
3+
'Content-Type': 'text/plain'
4+
}
5+
6+
return ('Hello, World!', 200, headers)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "bin",
4+
"target": "es2016",
5+
"module": "commonjs",
6+
"moduleResolution": "node",
7+
"sourceMap": true,
8+
"experimentalDecorators": true,
9+
"pretty": true,
10+
"noFallthroughCasesInSwitch": true,
11+
"noImplicitAny": true,
12+
"noImplicitReturns": true,
13+
"forceConsistentCasingInFileNames": true,
14+
"strictNullChecks": true
15+
},
16+
"files": [
17+
"index.ts",
18+
]
19+
}

0 commit comments

Comments
 (0)