Skip to content

Commit 8de9ab3

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 8de9ab3

File tree

7 files changed

+1060
-16
lines changed

7 files changed

+1060
-16
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

+71-14
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:
@@ -96,14 +97,70 @@ Run the following command from within your microservice/package directory to bui
9697
cd <b>mynewservice</b> && serverless deploy
9798
</pre>
9899

99-
## Remove Deployment
100+
## Remove deployment
100101

101102
Run the following command from within your microservice/package directory to remove the deployment of all functions:
102103

103104
<pre>
104105
cd <b>mynewservice</b> && serverless remove
105106
</pre>
106107

108+
## Access control (public vs. private functions)
109+
110+
If you're deploying an HTTP triggered function you'll most likely want to have the HTTP function be publicly accessible and google seems to make them private by default. At this time it appears the `serverless-google-cloudfunctions` plugin does provide a way to make functions public in the `serverless.yml` file.
111+
112+
Until this is supported by the plugin, we can work around it in a few ways.
113+
114+
### Update manually
115+
116+
This can be done either through the console or the gcloud cli [as detailed here](https://cloud.google.com/run/docs/authenticating/public#gcloud).
117+
118+
### Update via plugin commands
119+
120+
The included `serverless.yml` file uses the `serverless-plugin-scripts` plugin and defines 2 commands.
121+
122+
To make a function public, run the following from within your microservice/package directory:
123+
124+
```
125+
npx sls mkfunc-pub --function=hello
126+
```
127+
128+
To make a function private, run the following from within your microservice/package directory:
129+
130+
```
131+
npx sls mkfunc-pvt --function=hello
132+
```
133+
134+
Note: Using the serverless plugin these commnads can build the full `gcloud` command that will update the iam-policy-binding for the specified function.
135+
136+
### Update via script
137+
138+
This project includes a script that can automate the whole update process.
139+
140+
It works by first grabing all the functions defined in `serverless.yml` then sorting them as public or private (it does this by checking for the precence of the `allowUnauthenticated: true` key-value pair within the function definition), then runs `mkfunc-pub` or `mkfunc-pvt` for each function.
141+
142+
To run the script manually, run the following from within your microservice/package directory:
143+
144+
```
145+
../scripts/sls-update-allow-unauthenticated.sh
146+
```
147+
148+
To have the script run automatically after every serverless deploy, uncomment the `custom.scripts.commands.hooks` section in the `serverless.yml` file:
149+
150+
<pre>
151+
...
152+
153+
custom:
154+
scripts:
155+
commands:
156+
...
157+
<b>
158+
hooks:
159+
"after:deploy:deploy": ../scripts/sls-update-allow-unauthenticated.sh
160+
</b>
161+
</pre>
162+
107163
## References
108164

109165
1. [Serverless GCP Golang Example](https://github.com/serverless/examples/tree/master/google-golang-simple-http-endpoint)
166+
2. [Inspiration for Script Workaround](https://github.com/serverless/serverless-google-cloudfunctions/issues/205#issuecomment-658759740)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
# Get a list of functions in the serverless.yml file and format as args
4+
functions=$(sls print --path functions --transform keys --format text | xargs)
5+
6+
# Sort functions as public and private
7+
pub=()
8+
prv=()
9+
for fn in ${functions[@]}; do
10+
# if the `allowUnauthenticated: true` flag is defined for the function flag it to be made public
11+
if [[ "$(sls print --path functions."$fn" --format yaml | xargs)" == *"allowUnauthenticated: true"* ]]; then
12+
pub+=($fn)
13+
else
14+
prv+=($fn)
15+
fi
16+
done
17+
18+
# Run the mkfunc-pub command for each public function
19+
for fn in ${pub[@]}; do
20+
echo "Making function \""$fn"\" public..."
21+
npx sls mkfunc-pub --function="$fn"
22+
done
23+
24+
# Run the mkfunc-pvt command for each private function
25+
for fn in ${prv[@]}; do
26+
echo "Making function \""$fn"\" private..."
27+
npx sls mkfunc-pvt --function="$fn"
28+
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)