クイックスタート: gcloud CLI を使用してサービスへのトラフィックを保護する

このページでは、API Gateway に API をデプロイして、バックエンド サービスへのトラフィックを保護する方法について説明します。

Google Cloud CLI を使用して Cloud Run functions でバックエンド サービスにアクセスする新しい API をデプロイするには、次の手順に沿って操作します。このクイックスタートでは、API キーを使用してバックエンドを不正アクセスから保護する方法についても説明します。

始める前に

  1. Google Cloud コンソールで [ダッシュボード] ページに移動し、 Google Cloud プロジェクトを選択または作成します。

    ダッシュボードに移動

  2. お支払いが有効になっていることを確認します。

    課金を有効にする

  3. Google Cloud CLI がダウンロードされ、マシンにインストールされていることを確認します。

    gcloud CLI をダウンロードする

  4. gcloud コンポーネントを更新します。

    gcloud components update
  5. デフォルト プロジェクトを設定します。PROJECT_ID は、実際の Google Cloud プロジェクト ID に置き換えます。

    gcloud config set project PROJECT_ID

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

API Gateway では、次の Google Cloud サービスを有効にする必要があります。

名前 サービス名
API Gateway API apigateway.googleapis.com
Service Management API servicemanagement.googleapis.com
Service Control API servicecontrol.googleapis.com

必要なサービスを有効にするには:

Google Cloud コンソール

  1. Google Cloud コンソールで、[API とサービス> API ライブラリ] ページに移動します。

    [API ライブラリ] に移動

  2. [API ライブラリ] ページの検索バーに、必要な API 名を入力します。
  3. 検索結果で、API ページを選択します。
  4. API ページで、[有効にする] をクリックします。
  5. 上記の表に記載されているサービスごとに、この手順を繰り返します。

Google Cloud CLI

次のコマンドを使用して、サービスを有効にします。

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

gcloud サービスの詳細については、gcloud サービスをご覧ください。

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

API ゲートウェイは、デプロイされたバックエンド サービスの前に配置され、すべての受信リクエストを処理します。このクイックスタートでは、API Gateway は、以下に示す Node.js 関数を含む helloGET という名前の Cloud Run functions のバックエンドに受信呼び出しをルーティングします。

const functions = require('@google-cloud/functions-framework');

// Register an HTTP function with the Functions Framework that will be executed
// when you make an HTTP request to the deployed function's endpoint.
functions.http('helloGET', (req, res) => {
  res.send('Hello World!');
});

クイックスタート: Google Cloud CLI で Cloud Run functions の関数をデプロイするの手順に沿って、サンプルの Cloud Run functions コードをダウンロードし、Cloud Run functions のバックエンド サービスをデプロイします。このクイックスタートで説明するように、管理者はアカウントと Cloud Build サービス アカウントに追加のロールを付与する必要があります。

Cloud Run functions のデプロイ時に表示されるサービス URL をコピーします。これは、次の手順で API 構成を作成するときに必要になります。

API を作成する

これで、API Gateway で API を作成する準備が整いました。

  1. 次のコマンドを入力します。ここで、

    • API_ID は API の名前を指定します。API の命名ガイドラインについては、API ID の要件をご覧ください。
      gcloud api-gateway apis create API_ID 

    次に例を示します。

    gcloud api-gateway apis create my-api
  2. 正常に完了したら、次のコマンドを使用して新しい API の詳細を表示できます。

    gcloud api-gateway apis describe API_ID 

    例:

    gcloud api-gateway apis describe my-api 

    このコマンドを実行すると、次のようなフィード レスポンスが返されます。

      createTime: '2020-02-29T21:52:20.297426875Z'
      displayName: my-api
      managedService: my-api-123abc456def1.apigateway.my-project.cloud.goog
      name: projects/my-project/locations/global/apis/my-api
      state: ACTIVE
      updateTime: '2020-02-29T21:52:20.647923711Z'

managedService プロパティの値をコピーします。この値は、次のステップで API を有効にするために使用します。

API 構成を作成する

デプロイされた API バックエンドへのトラフィックを API ゲートウェイで管理する前に、API 構成が必要です。

特殊なアノテーションを含む OpenAPI 説明を使用して API 構成を作成し、選択した API Gateway の動作を定義できます。サポートされている OpenAPI 拡張機能の詳細については、以下をご覧ください。

このクイックスタートで使用される OpenAPI の説明には、Cloud Run functions のバックエンドへの転送手順が含まれています。

OpenAPI 2.0

# openapi-functions.yaml
swagger: '2.0'
info:
  title: API_ID optional-string
  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: SERVICE_URL/helloGET
      responses:
        '200':
          description: A successful response
          schema:
            type: string

OpenAPI 3.x

# openapi-functions.yaml
openapi: 3.0.4
info:
  title: API_ID optional-string
  description: Sample API on API Gateway with a Google Cloud Functions backend
  version: 1.0.0
# Define reusable components in x-google-api-management
x-google-api-management:
  backends:
    functions_backend:
      address: SERVICE_URL/helloGET
      pathTranslation: APPEND_PATH_TO_ADDRESS
      protocol: "http/1.1"
# Apply the backend configuration by referencing it by name. Set at the root so this applies to all operations unless overridden.
x-google-backend: functions_backend
paths:
  /hello:
    get:
      summary: Greet a user
      operationId: hello
      responses:
        '200':
          description: A successful response
          content:
            application/json:
              schema:
                type: string

この OpenAPI 説明をアップロードして、gcloud CLI を使用して API 構成を作成するには:

  1. コマンドラインから、openapi-functions.yaml という名前の新しいファイルを作成します。

  2. 新しく作成したファイルに OpenAPI の説明の内容をコピーして貼り付けます。

  3. ファイルを次のように編集します。

    1. title フィールドで、API_ID を API の名前に置き換え、optional-string を任意の簡単な説明に置き換えます。このフィールドの値は、この API へのアクセス権を付与する API キーを作成するときに使用されます。
    2. address フィールドで、SERVICE_URL を前の手順でコピーした Cloud Run 関数のバックエンド サービスが実行されている URL に置き換えます。
  4. 次のコマンドを入力します。ここで、

    • CONFIG_ID は API 構成の名前を指定します。
    • API_ID は API の名前を指定します。
    • API_DEFINITION には、OpenAPI 仕様のファイル名を指定します。
    • SERVICE_ACCOUNT_EMAIL は、認証が構成されたバックエンドのトークン署名に使用されるサービス アカウントを指定します。詳細については、サービス アカウントを構成するをご覧ください。
    gcloud api-gateway api-configs create CONFIG_ID \
      --api=API_ID --openapi-spec=API_DEFINITION \
       --backend-auth-service-account=SERVICE_ACCOUNT_EMAIL

    例:

    gcloud api-gateway api-configs create my-config \
      --api=my-api --openapi-spec=openapi2-functions.yaml \
      --backend-auth-service-account=0000000000000-compute@developer.gserviceaccount.com

    API 構成がダウンストリームのシステムに伝播される際に、このオペレーションが完了するまでに数分を要する場合があります。 複雑な API 構成の作成が正常に完了するまでに最大 10 分を要する場合があります。

  5. API 構成が作成されたら、次のコマンドを実行して詳細を表示できます。

    gcloud api-gateway api-configs describe CONFIG_ID \
      --api=API_ID 

    例:

    gcloud api-gateway api-configs describe my-config \
      --api=my-api

    出力には、次の例のように API 構成の詳細(名前や状態など)が表示されます。

    createTime: '2020-02-07T18:17:01.839180746Z'
    displayName: my-config
    gatewayConfig:
    backendConfig:
      googleServiceAccount: 0000000000000-compute@developer.gserviceaccount.com
    name: projects/my-project/locations/global/apis/my-api/configs/my-config
    serviceRollout:
    rolloutId: 2020-02-07r0
    state: ACTIVE
    updateTime: '2020-02-07T18:17:02.173778118Z'

ゲートウェイの作成

次に、API 構成をゲートウェイにデプロイします。ゲートウェイに API 構成をデプロイすると、API クライアントで API へのアクセスに使用できる外部 URL が定義されます。

次のコマンドを実行して、作成した API 構成を API Gateway にデプロイします。

gcloud api-gateway gateways create GATEWAY_ID \
  --api=API_ID --api-config=CONFIG_ID \
  --location=GCP_REGION 

説明:

  • GATEWAY_ID はゲートウェイの名前を指定します。
  • API_ID は、このゲートウェイに関連付けられた API Gateway API の名前を指定します。
  • CONFIG_ID は、ゲートウェイにデプロイされた API 構成の名前を指定します。