GCP Functions NodeJS - dennisholee/notes GitHub Wiki
Environment setup
-
Install google functions emulator
npm install --save-dev @google-cloud/functions-emulator
-
Set the GCP project ID
npx functions config set projectId {GCP Project ID}
Sample Functions Deployment on Emulator
-
Clone src from repository
git clone https://github.com/dennisholee/my-gcp-function.git ~/
-
Deploy function
npx functions deploy greet --trigger-http
-
Test function
npx functions call greet --data '{"message" , "dennis"}'
Serverless Framework
https://serverless.com/framework/docs/providers/google/guide/
Setup service account
-
Create service account
gcloud iam service-accounts create {service_account} --display-name {service_account_display_name}
-
Export service account key
gcloud iam service-accounts keys create {service_account}.json --iam-account {service_account_full}
To get the service account's name gcloud iam service-accounts list
3 Grant GCP Functions deployment permissions
gcloud projects add-iam-policy-binding {project_name} --member serviceAccount:{service_account_full} --role roles/deploymentmanager.editor
gcloud projects add-iam-policy-binding {project_name} --member serviceAccount:{service_account_full} --role roles/storage.objectCreator
Initialise folder
serverless create --template google-nodejs --path api
provider:
name: google
runtime: nodejs
project: {project_name}
credentials: {service_account_key}
plugins:
- serverless-google-cloudfunctions
package:
exclude:
- node_modules/**
- .gitignore
- .git/**
functions:
first:
handler: {function_name_in_index.js}
events:
- http: api
-
Deploy function
serverless deploy
-
List Functions
gcloud functions list
Deployment Errors
1 "Not Found" Error
Error message:
Error --------------------------------------------------
Not Found
Fix:
- Check deployment manager whether previous incomplete deployment exists and if so delete
gcloud deployment-manager deployments list
gcloud deployment-manager deployments delete {function_name}
If storage error then check Cloud Storage and delete bucket if exists then delete deployment
gsutil ls
gsutil rm -r {function_bucket}