Read through the tutorial here: How to build a Google Cloud Function with NodeJS
Google Cloud Platform (GCP) Cloud Function Example for NodeJS with YAML secrets, CORS, and local testing
This is an example starter codebase for launching a cloud function into Google Cloud Platform (GCP). Learn more about cloud functions at https://cloud.google.com/functions/. This cloud function is written in NodeJS v8.
This README serves as a tutorial on how to launch your first cloud function with Node JS. Conducted on macOS Mojave. Run through can be done in 15 minutes.
Zesty.io is a cloud headless content management system. Integrations are extensions are often made leveraging cloud functions. At Zesty.io we use cloud functions for many of our side services, and really enjoy their quick deployment, reliablity, and cheap cost structure.#
- Create a Google Cloud account https://cloud.google.com/ and create a project (
MY_PROJECTwill be used as an exmaple) - Download and install Google Cloud SDK https://cloud.google.com/sdk/docs/quickstarts and from your terminal run
gcloud auth login - Install NodeJS https://nodejs.org/en/
- Install Node Version Manager (NVM) https://github.com/nvm-sh/nvm/blob/master/README.md or from your terminal run
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bashin your terminal - Install GCP functions emulator for local testing by running
npm install -g @google-cloud/functions-emulatorin your terminal - Seting functions emulator
functions config set projectId MY_PROJECT, and runfunctions start - Install a code editor like Atom.io or VS Code
- (OPTIONAL) Install Postman https://www.getpostman.com/ to make test requests the cloud function (rather than using your browser)
You are going to use your terminal to run most commands.
- Create a new directory on your computer e.g.
mkdir ./my-cloud-function - run
cd ./my-cloud-functionto open the directory - run
npm initand fill out the init question prompted in terminal, defaults are fine - run
npm install corsinstalls a package we will use to control cross origin resource sharing - run
npm install env-yamlinstalls a package to read a local yaml file with secrets - run
nvm use v8to use node8 - run
echo "v8" > .nvmrcto create an nvm file to always use node 8 when you open the directory - run
touch .env.yamlto create your base secret file - run
echo ".env.yaml" > .gitignoreto create a file that tells git not to commit secrets (security reasons) - run
touch index.jsto create your base javascript file
- run
code .oratom .to open the directory in your code editor - open
.env.yamland add this lineTEST_SECRET: hello worldwhich creates a secret stringTEST_SECRET - open
index.jsand add this code:
require('env-yaml').config({ path: './.env.yaml' })
console.log(process.env.TEST_SECRET)
this code will load up your secret file and output a secret to console
4. run node index.js to see hello world outputted to the terminal. At this point if you are running into errors, trace back to the Getting Starting section and walk through every step again.
5. open index.js and replace all the code your wrote with:
require('env-yaml').config({ path: './.env.yaml' })
exports.myFunction = (req, res) => {
const cors = require('cors')()
cors(req, res, () => {
myFunction(req, res)
})
}
const myFunction = async (req, res) => {
res.send(process.env.TEST_SECRET)
}
Now you are ready to test
- run
functions deploy myFunction --env-vars-file .env.yaml --trigger-httpwhich will create a local testable function, if this ran successfully, it will return you a url for your function, mine washttp://localhost:8010/my-project/us-central1/myFunction, to test the function copy that url. - open your function url in a web browser, you should see "hello world"
If these steps failed you, you need to check your envirnoment setup be going over easy step in getting started.
- run
gcloud functions deploy myFunction --trigger-http --project=my-project --env-vars-file .env.yaml --runtime=nodejs8 --memory=256mb --timeout=240s" - it should take 2 minutes to deploy, once deployed the terminal will display a URL to load it from remote, open that in your browser.