Skip to content

A simple starting point for a Google Cloud Functions project using the Golang runtime and the Serverless Framework.

Notifications You must be signed in to change notification settings

cgossain/serverless-template-golang-google-cloud-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless Template - Golang + Google Cloud Functions

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.

Prerequisites

Project structure

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 a cmd directory that contains a main.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.

Create a new project using the included scripts

1. Bootstrap new project

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>

2. Review the serverless.yml file of the templateservice

Navigate to the microservice/package directory of the templateservice, and verify the following in the serverless.yml file:

  1. Make sure the provider.project field matches the project ID of your GCP project
  2. 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/

...

3. Create a new microservice/package

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>

Local testing

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.

Deploy

Run the following command from within your microservice/package directory to build and deploy all functions:

cd mynewservice && serverless deploy

Remove deployment

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

cd mynewservice && serverless remove

Access control (making functions public)

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.

Option 1 (update via script):

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.

Option 2: Update via plugin commands

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

Option 3: Update manually

This can be done either through the console or the gcloud cli as detailed here.

Appendix A: Create a new project manually from the template

This section is included for completeness sake, but you should probably just use the above scripts instead.

  1. 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
  1. 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
  1. From the root directory, make a copy of the templateservice directory and rename it to match your microservice/package name:
cp -R templateservice mynewservice
  1. Update the package name in both fn.go and fn_test.go to match your microservice/package name:
package mynewservice

...
  1. 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

...
  1. Install the serverless plugin dependencies (specified in package.json):
// Run this from the root of the project directory
npm install

References

  1. Serverless GCP Golang Example
  2. Inspiration for Script Workaround

Releases

No releases published

Packages

No packages published