{GCP API Gateway}クイックスタート: gcloud コマンドライン ツールを使用して API Gateway に API をデプロイする

 

https://cloud.google.com/api-gateway/docs/secure-traffic-gcloud?hl=ja

https://dev.classmethod.jp/articles/google-cloud-api-gateway-handson/


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


-- 2. 必要なサービスを有効にする

gcloud services list
gcloud services list --enabled

gcloud services enable apigateway.googleapis.com
gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com

gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudbuild.googleapis.com
gcloud services enable artifactregistry.googleapis.com

gcloud services enable run.googleapis.com

 


-- 3. API バックエンドをデプロイする

-- 3.1 NVM のインストール

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

-- 3.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


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

npm install --save @google-cloud/storage

npm install --save @google-cloud/functions-framework


-- 3.4 サンプルコードを取得する

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
cd nodejs-docs-samples/functions/helloworld/helloworldGet

 

-- 3.5 関数のデプロイ

 

gcloud functions deploy helloGET \
--runtime=nodejs16 \
--region=asia-northeast1 \
--source=. \
--entry-point=helloGET \
--trigger-http \
--allow-unauthenticated


gcloud functions describe helloGET \
--gen2 \
--region=asia-northeast1 \
--format="value(serviceConfig.uri)"

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


-- 4. API の作成

gcloud api-gateway apis create api01

gcloud api-gateway apis describe api01

 


-- 5. API 構成の作成

vim a.yaml

# a.yaml
swagger: '2.0'
info:
  title: api01 test
  description: Sample API on API Gateway with a Google Cloud Functions backend
  version: 1.0.0
schemes:
  - https
produces:
  - application/json
paths:
  /hello:
    get:
      summary: Greet a user
      operationId: hello
      x-google-backend:
        address: https://asia-northeast1-project01-9999999.cloudfunctions.net/helloGET
      responses:
        '200':
          description: A successful response
          schema:
            type: string


gcloud iam service-accounts list

gcloud api-gateway api-configs create config01 \
--api=api01 \
--openapi-spec=a.yaml \
--backend-auth-service-account=222222222222-compute@developer.gserviceaccount.com

 


gcloud api-gateway api-configs describe config01 \
--api=api01

 

-- 6. ゲートウェイを作成する

gcloud api-gateway gateways create gateway01 \
--api=api01 \
--api-config=config01 \
--location=asia-northeast1

gcloud api-gateway gateways describe gateway01 \
--location=asia-northeast1


-- 7. API デプロイのテスト

curl https://gateway01-11111111.an.gateway.dev/hello

 

 

-- 8. クリーンアップ

gcloud api-gateway gateways delete gateway01 \
--location=asia-northeast1 \
--quiet


gcloud api-gateway api-configs delete config01 \
--api=api01 \
--quiet

gcloud api-gateway apis delete api01 \
--quiet

 

gcloud projects list

gcloud projects delete project01-9999999 \
--quiet