{GCP Cloud Functions}Node.js で HTTP Cloud Functions の関数を作成してデプロイする

 

https://cloud.google.com/functions/docs/create-deploy-http-nodejs?hl=ja

https://cloud.google.com/functions/docs/console-quickstart-1st-gen?hl=ja

https://cloud-ace.jp/column/detail321/

 


-- 1. 前作業

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

 

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

gcloud components update


コンソールから下記APIを有効にする
Cloud Functions API
Cloud Build API

有効にせずにデプロイするとエラーとともに有効化するURLが案内される


-- 2. Node.js 開発環境のセットアップ

-- 2.1 NVM のインストール

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

-- 2.2 Node.js と npm(Node Package Manager)のインストール

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

nvm install stable
nvm alias default stable
node -v


-- 2.3 Node.js 用 Google Cloud クライアント ライブラリのインストール

npm install --save @google-cloud/storage


-- 3. 関数の作成

rm -rf ~/helloworld
mkdir ~/helloworld
cd ~/helloworld

vim index.js

const functions = require('@google-cloud/functions-framework');
const escapeHtml = require('escape-html');

/**
 * Responds to an HTTP request using data from the request body parsed according
 * to the "content-type" header.
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
functions.http('func01', (req, res) => {
  res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
});

-- 4. 依存関係の指定

vim package.json

{
  "name": "nodejs-docs-samples-functions-hello-world",
  "version": "0.0.1",
  "private": true,
  "license": "Apache-2.0",
  "author": "Google Inc.",
  "repository": {
    "type": "git",
    "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
  },
  "engines": {
    "node": ">=12.0.0"
  },
  "dependencies": {
    "@google-cloud/debug-agent": "^7.0.0",
    "@google-cloud/functions-framework": "^3.1.0",
    "escape-html": "^1.0.3"
  },
  "devDependencies": {
    "@google-cloud/pubsub": "^3.0.0",
    "@google-cloud/storage": "^6.0.0",
    "gaxios": "^4.3.2",
    "mocha": "^9.0.0",
    "moment": "^2.24.0",
    "promise-retry": "^2.0.0",
    "sinon": "^14.0.0",
    "supertest": "^6.0.0",
    "uuid": "^8.0.0",
    "wait-port": "^0.3.0"
  }
}

-- 5. 関数のデプロイ

gcloud functions runtimes list \
--region=asia-northeast1


gcloud functions deploy func01 \
--runtime nodejs16 \
--trigger-http \
--allow-unauthenticated \
--region=asia-northeast1 \
--memory=128MB \
--timeout 30s \
--max-instances 1

 


gcloud functions list


-- 6. 関数のテスト

gcloud functions describe func01 \
--region=asia-northeast1

curl https://asia-northeast1-project01-9999999.cloudfunctions.net/func01

curl https://asia-northeast1-project01-9999999.cloudfunctions.net/func01?name=test

 


-- 7. ログの表示

gcloud functions logs read func01 \
--region=asia-northeast1

 

 

-- 8. クリーンアップ

gcloud projects list

gcloud projects delete project01-9999999