GCP Functions NodeJS - dennisholee/notes GitHub Wiki

Environment setup

  1. Install google functions emulator npm install --save-dev @google-cloud/functions-emulator

  2. Set the GCP project ID npx functions config set projectId {GCP Project ID}

Sample Functions Deployment on Emulator

  1. Clone src from repository git clone https://github.com/dennisholee/my-gcp-function.git ~/

  2. Deploy function npx functions deploy greet --trigger-http

  3. Test function npx functions call greet --data '{"message" , "dennis"}'

Serverless Framework

https://serverless.com/framework/docs/providers/google/guide/

Setup service account

  1. Create service account gcloud iam service-accounts create {service_account} --display-name {service_account_display_name}

  2. 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
  1. Deploy function serverless deploy

  2. List Functions gcloud functions list

Deployment Errors

1 "Not Found" Error

Error message:

Error --------------------------------------------------
 
Not Found

Fix:

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