6
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Google Cloud Functions の使い方

Last updated at Posted at 2018-03-10

Google Cloud Functions はサーバーレスの関数です。

次のページを参考にしました。
Quickstart: Using the gcloud Command-Line Tool

Google Cloud Platform で、
Cloud Functions API を有効にして下さい。

サンプルのプログラム

フォルダー構造

$ tree 
.
├── index.js
└── package.json
index.js
/**
 * HTTP Cloud Function.
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
exports.hello = (req, res) => {
  var str_out = ""
  str_out += "これはテストです。\n"
  str_out += "Hello World!\n"
  str_out += "Good Evening\n"
  str_out += "今晩は\n"
  str_out += "Mar/10/2018 PM 20:51\n"
  res.send(str_out)
}
package.json
{
  "name": "sample-http",
  "version": "0.0.1"
}

デプロイ

gcloud functions deploy function-nov25 \
--entry-point=hello \
--trigger-http \
--runtime=nodejs22 --gen2 \
--region asia-northeast1
Allow unauthenticated invocations of new function [function-nov25]? (y/N)?  y

関数の一覧

$ gcloud functions list
NAME            STATE   TRIGGER       REGION           ENVIRONMENT
function-nov25  ACTIVE  HTTP Trigger  asia-northeast1  2nd gen

URL の確認

$ gcloud functions describe function-nov25 --region asia-northeast1 | grep url:
url: https://asia-northeast1-project-nov15-2024.cloudfunctions.net/function-nov25

関数のテスト

$ curl https://asia-northeast1-project-nov15-2024.cloudfunctions.net/function-nov25
これはテストです。
Hello World!
Good Evening
今晩は
Nov/25/2022 AM 10:50

関数の削除

gcloud functions delete function-nov25 --region asia-northeast1

引数を Get で受け取るサンプル

Google Cloud Functions で Get の引数を受け取る

引数を Post で受け取るサンプル

Google Cloud Functions で Post の引数を受け取る

参考ページ

gcloud functions deploy

6
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?