Skip to content

Commit 793e787

Browse files
committed
Add post-deploy script to make functions public
Shout out to this post for pointing out this workaround: serverless/serverless-google-cloudfunctions#205 (comment) Once this is natively supported in the serverless-google-cloudfunctions plugin, the serverless.yml file should be updated accordingly.
1 parent 0622582 commit 793e787

File tree

7 files changed

+1007
-15
lines changed

7 files changed

+1007
-15
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
13
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
24
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
35

README.md

+21-13
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
This template project is designed to be used as a starting point for a Google Cloud Functions project using the Golang runtime and the Serverless Framework.
44

5-
## Requirements
5+
## Prerequisites
66

7-
- You have the [Serverless Framework](https://www.serverless.com/framework/docs/getting-started/) installed.
8-
- You have a project already created and configured in Google Cloud. You can follow [this guide](https://www.serverless.com/framework/docs/providers/google/guide/credentials/) to make sure its setup to work with Severless.
9-
- You've setup your overall environment to work with GCP and the Serverless Framework. You should follow [these guides](https://www.serverless.com/framework/docs/providers/google/guide/intro/) if not.
7+
- Install the [Serverless Framework](https://www.serverless.com/framework/docs/getting-started/)
8+
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install), and then run `gcloud auth login` to authenticate
9+
- Create and configure a project in Google Cloud Console. You can follow [this guide](https://www.serverless.com/framework/docs/providers/google/guide/credentials/) to make sure its setup to work with Severless.
1010

1111
## Project structure
1212

13-
The root directory contains a folder for each of your microservices (i.e. Go package).
13+
The root directory contains a folder for each microservice/package.
1414

1515
The `go.mod` file also resides in the root directory.
1616

@@ -42,21 +42,15 @@ Create a directory (i.e. package) for each logical microservice you intend to de
4242
cp -R templateservice <b>mynewservice</b>
4343
</pre>
4444

45-
2. Navigate to the new directory and install the Google Cloud Functions Provider Plugin:
46-
47-
<pre>
48-
cd <b>mynewservice</b> && serverless plugin install --name serverless-google-cloudfunctions
49-
</pre>
50-
51-
3. Update the package name in both `fn.go` and `fn_test.go` to match your microservice/package name:
45+
2. Update the package name in both `fn.go` and `fn_test.go` to match your microservice/package name:
5246

5347
<pre>
5448
package <b>mynewservice</b>
5549

5650
...
5751
</pre>
5852

59-
4. Open `serverless.yml` and update the configuration (i.e. service name, GCP project name, GCP credentials keyfile, etc.):
53+
3. Open `serverless.yml` and update the configuration (i.e. service name, GCP project name, GCP credentials keyfile, etc.):
6054

6155
<pre>
6256
service: <b>mynewservice</b>
@@ -80,10 +74,17 @@ functions:
8074
handler: Hello
8175
events:
8276
- http: path # https://www.serverless.com/framework/docs/providers/google/events/http#http-events
77+
allowUnauthenticated: true # unofficial flag that ties into the post-deploy script
8378

8479
...
8580
</pre>
8681

82+
4. Install the serverless plugin dependencies (specified in `package.json`):
83+
84+
<pre>
85+
npm install
86+
</pre>
87+
8788
### Additional info
8889

8990
Take a look at [this guide](https://cloud.google.com/functions/docs/writing#structuring_source_code) for ideas on how to structure your source code for different scenarios:
@@ -104,6 +105,13 @@ Run the following command from within your microservice/package directory to rem
104105
cd <b>mynewservice</b> && serverless remove
105106
</pre>
106107

108+
## Make functions public/private
109+
110+
1. In your `serverless.yml` file add the `allowUnauthenticated: true` key to every function you want to allow public access to, and `allowUnauthenticated: false` (or omit it) for every function you want to make/keep private.
111+
2. From the microservice/package directory run `../scripts/sls-update-allow-unauthenticated.sh`
112+
113+
Note: If you want this script to run automatically after every deploy, uncomment the `custom.scripts.commands.hooks` section in the `serverless.yml` file.
114+
107115
## References
108116

109117
1. [Serverless GCP Golang Example](https://github.com/serverless/examples/tree/master/google-golang-simple-http-endpoint)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
functions=$(sls print --path functions --transform keys --format text | xargs)
4+
5+
# Sort functions into public vs. private
6+
pub=()
7+
prv=()
8+
for fn in ${functions[@]}; do
9+
# if the `allowUnauthenticated: true` flag is defined for the function flag it to be made public
10+
if [[ "$(sls print --path functions."$fn" --format yaml | xargs)" == *"allowUnauthenticated: true"* ]]; then
11+
pub+=($fn)
12+
else
13+
prv+=($fn)
14+
fi
15+
done
16+
17+
# Run the "make-function-public" command for each public function
18+
for fn in ${pub[@]}; do
19+
echo "Making function \""$fn"\" public"
20+
npx sls make-function-public --function="$fn"
21+
done
22+
23+
# Run the "make-function-private" command for each private function
24+
for fn in ${prv[@]}; do
25+
echo "Making function \""$fn"\" private"
26+
npx sls make-function-private --function="$fn"
27+
done

templateservice/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
13
# Binaries for programs and plugins
24
*.exe
35
*.exe~
@@ -15,6 +17,5 @@
1517
# vendor/
1618

1719
# Other
18-
.DS_Store
1920
node_modules
2021
.serverless

0 commit comments

Comments
 (0)