https://github.com/cgossain/serverless-template-golang-google-cloud-functions
A simple starting point for a Google Cloud Functions project using the Golang runtime and the Serverless Framework.
https://github.com/cgossain/serverless-template-golang-google-cloud-functions
cloud cloud-functions framework functions gcp go goland golang golang-runtime google microservice serverless serverless-framework template
Last synced: 3 months ago
JSON representation
A simple starting point for a Google Cloud Functions project using the Golang runtime and the Serverless Framework.
- Host: GitHub
- URL: https://github.com/cgossain/serverless-template-golang-google-cloud-functions
- Owner: cgossain
- Created: 2020-11-19T03:07:19.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-13T17:51:30.000Z (over 5 years ago)
- Last Synced: 2025-05-31T21:49:41.881Z (about 1 year ago)
- Topics: cloud, cloud-functions, framework, functions, gcp, go, goland, golang, golang-runtime, google, microservice, serverless, serverless-framework, template
- Language: Shell
- Homepage:
- Size: 45.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
- Install the [Serverless Framework](https://www.serverless.com/framework/docs/getting-started/)
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install)
- Follow [this guide](https://www.serverless.com/framework/docs/providers/google/guide/credentials/) to make sure your GCP project is setup to work with Severless
## 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](https://cloud.google.com/functions/docs/writing#structuring_source_code) 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
```
### 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
```
## 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](https://cloud.google.com/run/docs/authenticating/public).
## 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
2. 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
3. From the root directory, make a copy of the `templateservice` directory and rename it to match your microservice/package name:
cp -R templateservice mynewservice
4. Update the package name in both `fn.go` and `fn_test.go` to match your microservice/package name:
package mynewservice
...
5. 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
...
6. 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](https://github.com/serverless/examples/tree/master/google-golang-simple-http-endpoint)
2. [Inspiration for Script Workaround](https://github.com/serverless/serverless-google-cloudfunctions/issues/205#issuecomment-658759740)