LoginSignup
1
1

More than 5 years have passed since last update.

Google Cloud Functions を使ってみた

Posted at

名前の通りAWS Lamba + APIGatewayみたいなやつ
記事中のプロジェクト名などは仮

環境

MacOS High Sierra 10.13.1
Python 3.6.0 :: Anaconda custom (x86_64)
npm 5.5.1

クイックスタート

プロジェクトの作製

ここに書いてある手順に従って、
Before you begin の1〜3までやる
https://cloud.google.com/functions/docs/quickstart?hl=ja

クラウドコンソールツールをインストール

gcloudコンポーネントのインストール

$ gcloud components update beta

betaというコンポーネントをインストールするらしい
勝手にパス通ったりしないので必要に応じて google-cloud-sdk/bin/gcloud とかたたく

Node.js環境のセットアップ

Cloud Functionを作る

プロジェクトのディレクトリを作って移動

$ mkdir gcf_hello_world
$ cd gcf_hello_world

index.js という名前でファイルを作製
javascript:index.js
/**
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.helloGET = function helloGET (req, res) {
res.send('Hello World!');
};

デプロイ

Cloud Strageの準備

ローカル環境からデプロイするためにはCloud Strageにバケットが必要
https://console.cloud.google.com/storage/browser

ここでは myfunction-bucket という名前で作ったとする

関数のデプロイ

プロジェクトのルートディレクトリにおいて以下のコマンドを実行
gcloudへのパスは環境に応じて読替えのこと

$ ../google-cloud-sdk/bin/gcloud beta functions deploy helloGET --trigger-http --stage-bucket myfunction-bucket

クイックスタートには記述がないがローカル環境から上げる場合は --stage-bucket の指定が必要らしい
(クラウドコンソール使えばいらないのかもだが未確認)

完了

Copying file:///var/folders/8w/jt2mlcs55nn6jcylpjzpp7900000gn/T/tmpaqWLvB/fun.zip [Content-Type=application/zip]...
/ [1 files][  258.0 B/  258.0 B]                                                
Operation completed over 1 objects/258.0 B.                                      
Deploying function (may take a while - up to 2 minutes)...done.                
availableMemoryMb: 256
entryPoint: helloGET
httpsTrigger:
  url: https://us-central1-my-function.cloudfunctions.net/helloGET
labels:
  deployment-tool: cli-gcloud
name: projects/my-function/locations/us-central1/functions/helloGET
serviceAccountEmail: my-function@appspot.gserviceaccount.com
sourceArchiveUrl: gs://my-function/us-central1-helloGET-uqkuoyeupard.zip
status: ACTIVE
timeout: 60s
updateTime: '2017-12-20T10:42:35Z'
versionId: '2'

デプロイ完了まで1〜2分かかる
urlにアクセスするとHello World!が表示される

感想

まぁまぁ手軽に使える
まだBetaなのね
デプロイはちょっと時間かかるなぁ

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