Skip to content

Commit

Permalink
GCP-Native port of ts-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivek Lakshmanan committed Apr 16, 2021
1 parent 113ef71 commit 99ab28c
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gcp-native-ts-functions/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: gcp-functions
runtime: nodejs
description: GCP Functions example
template:
config:
gcp:project:
description: The Google Cloud project to deploy into
gcp:region:
description: The Google Cloud region
64 changes: 64 additions & 0 deletions gcp-native-ts-functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Google Cloud Functions

An example of deploying an HTTP Google Cloud Function endpoint using GCP Native provider and TypeScript.

## Prerequisites

0. [Ensure you have the latest Node.js and NPM](https://nodejs.org/en/download/)
2. [Install the Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
3. [Configure Pulumi to access your GCP account](https://www.pulumi.com/docs/intro/cloud-providers/gcp/setup/)

## Running the App

1. Restore NPM dependencies:

```
$ npm install
```
2. Create a new stack:
```
$ pulumi stack init gcp-fn
```
3. Configure your GCP project and region:
```
$ pulumi config set gcp-native:project <projectname>
$ pulumi config set gcp-native:region <region>
```
4. Run `pulumi up` to preview and deploy changes:
```
$ pulumi up
Previewing changes:
...
Performing changes:
...
info: 6 changes performed:
+ 6 resources created
Update duration: 39.65130324s
```
5. 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!
```
6. Clean up your GCP and Pulumi resources:
```
$ pulumi destroy
...
$ pulumi stack rm
...
```
62 changes: 62 additions & 0 deletions gcp-native-ts-functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2016-2021, Pulumi Corporation.

import * as gcp from "@pulumi/gcp-native";
import * as pulumi from "@pulumi/pulumi";
import * as random from "@pulumi/random";

const config = new pulumi.Config("gcp-native");
const project = config.require("project");
const region = config.require("region");

const randomString = new random.RandomString("name", {
upper: false,
number: false,
special: false,
length: 8,
});

const bucketName = pulumi.interpolate`bucket-${randomString.result}`;
const bucket = new gcp.storage.v1.Bucket("bucket", {
project: project,
bucket: bucketName,
name: bucketName,
});

const archiveName = "zip";
const bucketObject = new gcp.storage.v1.BucketObject(archiveName, {
object: archiveName,
name: archiveName,
bucket: bucket.name,
source: new pulumi.asset.AssetArchive({
".": new pulumi.asset.FileArchive("./pythonfunc"),
}),
});

const functionName = pulumi.interpolate`func-${randomString.result}`;
const func = new gcp.cloudfunctions.v1.Function("function-py", {
projectsId: project,
locationsId: region,
functionsId: functionName,
name: pulumi.interpolate`projects/${project}/locations/${region}/functions/${functionName}`,
sourceArchiveUrl: pulumi.interpolate`gs://${bucket.name}/${bucketObject.name}`,
httpsTrigger: {},
entryPoint: "handler",
timeout: "60s",
availableMemoryMb: 128,
runtime: "python37",
ingressSettings: "ALLOW_ALL",
});

const invoker = new gcp.cloudfunctions.v1.FunctionIamPolicy("function-py-iam", {
projectsId: project,
locationsId: region,
functionsId: functionName, // func.name returns the long `projects/foo/locations/bat/functions/buzz` name which doesn't suit here
bindings: [
{
members: ["allUsers"],
role: "roles/cloudfunctions.invoker",
},
],
}, { dependsOn: func});

export const functionUrl = func.httpsTrigger.url;
13 changes: 13 additions & 0 deletions gcp-native-ts-functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "gcp-ts-functions",
"version": "1.0.0",
"devDependencies": {
"@types/node": "^8.0.0",
"@types/express": "^4.16.0"
},
"dependencies": {
"@pulumi/gcp-native": "^0.0.2a",
"@pulumi/pulumi": "^3.0.0a",
"@pulumi/random": "^4.0.0a"
}
}
6 changes: 6 additions & 0 deletions gcp-native-ts-functions/pythonfunc/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def handler(request):
headers = {
'Content-Type': 'text/plain'
}

return ('Hello, World!', 200, headers)
19 changes: 19 additions & 0 deletions gcp-native-ts-functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts",
]
}

0 comments on commit 99ab28c

Please sign in to comment.