LoginSignup
1
2

More than 1 year has passed since last update.

個人でGCPを使い始めるまで

Posted at

これは何?

会社ではGCPを使っているものの、それはもう既に誰かが使い方を整えたもの
個人でゼロからやってみることで学べることがあるんじゃないかと思い、
実際に使ってみてやったことなどをメモしていく

目標

今回は取り急ぎCloud functionsで関数をdeployできるまでを目標にやってみる

そもそもGCP(Google Cloud Platform)とは

公式の説明はこちら

ざっくり

  • Googleが提供するクラウドコンピューティングサービス
  • 従量制サービスのため使い始めるハードルが低い(使い方ミスると請求えぐいが。。)
  • Googleが提供するさまざまなサービスの利用もできる(Google Mapや検索エンジンなど)
  • GCP Console(GUI)で簡単に操作ができる

やってみる

とりあえず公式ページにアクセス
https://cloud.google.com/gcp/
「無料で使ってみる」というボタンがあるのでクリック
image.png

アカウント登録画面になるのでいろいろ入力していく
$300分くれるんですね、すぐ使い切りそうだけど。。
image.png
個人認証などやっていってアカウント登録完了させる
クレカ情報など入力して完了するとトップページが開いた
image.png

プロジェクト名はデフォルトだと「My First Project」なんすね
とりあえず目の前はCloud functions使えるようにしてみようと思うので、「すべてのプロダクトを表示」からCloud functionsをメニューに固定しておく
image.png

以降はCloud functionsのクイックスタートを見ながらやる
個人的にやり慣れているnodejsで
https://cloud.google.com/functions/docs/create-deploy-http-nodejs?authuser=2&hl=ja

Cloud Functions and Cloud Build API を有効にして、
image.png

gcloud sdkをインストール
https://cloud.google.com/sdk/docs/install-sdk?authuser=2&hl=ja

ローカルで開発環境構築
IDEは入れてあるのでディレクトリ切るところから

mkdir ~/helloworld
cd ~/helloworld

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('helloHttp', (req, res) => {
  res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
});

以下を実行してpackage.json作って

npm init

以下を記載してnpm i

{
  "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"
  },
  "scripts": {
    "unit-test": "mocha test/index.test.js test/*unit*test.js test/*integration*test.js --timeout=6000 --exit",
    "system-test": "mocha test/*system*test.js --timeout=600000 --exit",
    "all-test": "npm run unit-test && npm run system-test",
    "test": "npm -- run unit-test"
  },
  "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"
  }
}

以下のコマンドでdeployする

gcloud functions deploy helloHttp --runtime nodejs16 --trigger-http --allow-unauthenticated

deployできたか確認

gcloud functions describe helloHttp

ブラウザでurlにアクセスすると表示された!
image.png

ログも確認してみる
https://console.cloud.google.com/logs
コンソールから確認すると実行された形跡が確認できた
image.png

ひとまずここまで
さてここから何をやってみようか。。

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