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.
- Install the Serverless Framework
- Install the Google Cloud SDK
- Follow this guide to make sure your GCP project is setup to work with Severless
A few notes about the project structure:
- The root directory contains a
package.json
file which defines dev dependencies needed to deploy functions using the Serverless Framework - This template assumes that each logical microservice will be contained within its own package/directory and deployed separately using its own
serverless.yml
definition - Each package/directory could contain one or more cloud functions, and has its own
go.mod
file to manage dependencies by microservice - This template includes a
.env
file which you can use to put environment variables used for local development as well as acmd
directory that contains amain.go
file that starts up a server that you can use to test your functions locally
. ├── ... ├── microservice1 │ └── cmd │ └── main.go │ └── .env │ └── fn_test.go │ └── fn.go │ └── go.mod │ └── serverless.yml │ └── ... ├── microservice2 │ └── cmd │ └── main.go │ └── .env │ └── fn_test.go │ └── fn.go │ └── go.mod │ └── serverless.yml │ └── ... ├── package.json ├── ...
Take a look at this guide to learn more about structuring your code.
Download the bootstrap-new-project.sh
script to the directory where you want to create your project:
curl https://raw.githubusercontent.com/cgossain/serverless-template-golang-google-cloud-functions/master/scripts/bootstrap/bootstrap-new-project.sh --output bootstrap-new-project.sh && chmod +x bootstrap-new-project.sh
Then run it:
// Arguments:
// $1 - Your github username (i.e. `cgossain`)
// $2 - The name of your new project
./bootstrap-new-project.sh <mygithubusername> <myprojectname>
Navigate to the microservice/package directory of the templateservice, and verify the following in the serverless.yml
file:
- Make sure the
provider.project
field matches the project ID of your GCP project - Make sure the
provider.credentials
field matches your json keyfile
service: templateservice useDotenv: true provider: name: google runtime: go113 project: gcp-project-id # defaults to the project name credentials: ~/.gcloud/keyfile.json # https://www.serverless.com/framework/docs/providers/google/guide/credentials/ ...
Run the /scripts/bootstrap/bootstrap-new-service.sh
script from your projects' root directory to create a new microservice package from the templateservice.
./scripts/bootstrap/bootstrap-new-service.sh <mynewservicename>
For each microservice/package directory that you want to test, modify the server defined in cmd/main.go
with routes to your functions (and provide any test payloads needed), then run the file:
go run main.go
Now call your functions.
Run the following command from within your microservice/package directory to build and deploy all functions:
cd mynewservice && serverless deploy
Run the following command from within your microservice/package directory to remove the deployment of all functions:
cd mynewservice && serverless remove
Google makes your functions private by default. If you're deploying an HTTP triggered function you'll likely want to have it be publicly accessible.
At this time the Serverless Framework does provide a way to do this via the serverless.yml
file, but we can work around it in a few way.
This project includes a script that works with your serverless.yml
file to make your functions private or public.
First add the allowUnauthenticated: true
key to each of the functions you'd like to make public in your serverless.yml
file. This is a custom key that the script looks for.
... functions: hello: handler: Hello events: - http: path # https://www.serverless.com/framework/docs/providers/google/events/http#http-events allowUnauthenticated: true # unofficial flag that ties into the post-deploy script ...
Then run the following script from within your microservice/package directory:
../scripts/deploy/sls-update-allow-unauthenticated.sh
Alternatively, to have the script run automatically after every serverless deploy
, uncomment the custom.scripts.commands.hooks
section in the serverless.yml
file:
... custom: scripts: commands: ... hooks: "after:deploy:deploy": ../scripts/sls-update-allow-unauthenticated.sh
Note: Permissions don't actually need to be updated on each deploy which is why the post-deploy hook is commented out by default in this project. It should be sufficient to just manually run the script after each deploy that includes a new function of if you change the allowUnauthenticated
value within a function definition.
The included serverless.yml
file uses the serverless-plugin-scripts
plugin and defines 2 commands.
To make a function public, run the following from within your microservice/package directory:
sls mkfunc-pub --function=hello
To make a function private, run the following from within your microservice/package directory:
sls mkfunc-pvt --function=hello
This can be done either through the console or the gcloud cli as detailed here.
This section is included for completeness sake, but you should probably just use the above scripts instead.
- Clone the template repo into a new local project folder:
git clone https://github.com/cgossain/serverless-google-cloud-functions-golang-template.git my-gcp-project-name
- Update
go.mod
in the root directory with your module path. For example:
module github.com/my-github-username/my-gcp-project-name go 1.13
- From the root directory, make a copy of the
templateservice
directory and rename it to match your microservice/package name:
cp -R templateservice mynewservice
- Update the package name in both
fn.go
andfn_test.go
to match your microservice/package name:
package mynewservice ...
- Open
serverless.yml
and update the configuration (i.e. service name, GCP project name, GCP credentials keyfile, etc.):
service: mynewservice useDotenv: true provider: name: google runtime: go113 project: gcp-project-id credentials: ~/.gcloud/keyfile.json # https://www.serverless.com/framework/docs/providers/google/guide/credentials/ plugins: - serverless-google-cloudfunctions package: exclude: - .gitignore - .git/** functions: hello: handler: Hello events: - http: path # https://www.serverless.com/framework/docs/providers/google/events/http#http-events allowUnauthenticated: true # unofficial flag that ties into the post-deploy script ...
- Install the serverless plugin dependencies (specified in
package.json
):
// Run this from the root of the project directory npm install