【GCP】Qwiklabs の「ベースライン: インフラストラクチャ」を勉強しています~Cloud Functions: Qwik Start – コマンドライン~

Cloud Functions: Qwik Start - コマンドライン GCP
Cloud Functions: Qwik Start - コマンドライン

Qwiklabs の「ベースライン: インフラストラクチャ」を進めていきたいと思います。

Cloud Functions: Qwik Start – コマンドライン

概要

Cloud Shell のコマンドラインを使用して、Cloud Functions の関数を作成、デプロイ、検証する方法について学びます。

コマンド

関数を作成する

mkdir gcf_hello_world
cd gcf_hello_world
vi index.js

index.js の内容はこちらになります。

exports.helloWorld = function helloWorld (event, callback) {
  console.log(`My Cloud Function: ${JSON.stringify(event.data.message)}`);
  callback();
};

Cloud Storage バケットを作成する

export MY_BUCKET_NAME=<GCPプロジェクトID>-bucket-1
echo $MY_BUCKET_NAME
gsutil mb -p <GCPプロジェクトID> gs://$MY_BUCKET_NAME
gsutil cp index.js gs://$MY_BUCKET_NAME/

関数をデプロイする

gcloud functions deploy helloWorld --stage-bucket $MY_BUCKET_NAME --trigger-topic hello_world --runtime nodejs6
gcloud functions describe helloWorld

関数をテストする

gcloud functions call helloWorld --data '{"message":"Hello World!"}'

ログを表示する

gcloud functions logs read helloWorld

まとめ

Cloud Functions の関数を作成、デプロイ、検証する方法について学びました。
サーバーレスなので使いやすそうです。

https://www.papacoder.net/gcp-training-free-campaign/

コメント

タイトルとURLをコピーしました