Getting started with API Gateway and Cloud Run

This page shows you how to set up API Gateway to manage and secure a Cloud Run backend service.

Task List

Use the following task list as you work through the tutorial. All tasks are required to deploy an API Gateway for your Cloud Run backend service.

  1. Create or select a Google Cloud project.
  2. If you haven't deployed your own Cloud Run, deploy a sample service. See step 7 in Before you begin.
  3. Enable the required API Gateway services.
  4. Create an OpenAPI description that describes your API, and configure the routes to your Cloud Run backend service. See Creating an API config.
  5. Deploy an API gateway using your API config. See Deploying an API gateway.
  6. Track activity to your services. See Tracking API activity.
  7. Avoid incurring charges to your Google Cloud account. See Clean up.

Before you begin

  1. In the Google Cloud console, go to the Dashboard page and select or create a Google Cloud project.

    Go to Dashboard

  2. Make sure that billing is enabled for your project.

    Enable billing

  3. Make a note of the project ID you want to use for this tutorial. On the rest of this page, this project ID is referred to as PROJECT_ID.

  4. Download and install the Google Cloud CLI.

    Download the gcloud CLI

  5. Update gcloudcomponents:

    gcloud components update
  6. Set the default project. Replace PROJECT_ID with your Google Cloud project ID

    gcloud config set project PROJECT_ID

  7. If you haven't deployed your own Cloud Run service, follow the steps in Quickstart: Deploy a Prebuilt Sample Container to select or create a Google Cloud project and deploy a sample backend. Make a note of the app URL, as well as the region and project ID where your apps are deployed.

Enable required services

API Gateway requires that you enable the following Google Cloud services:

Name Service name
API Gateway API apigateway.googleapis.com
Service Management API servicemanagement.googleapis.com
Service Control API servicecontrol.googleapis.com

To enable required services:

Google Cloud console

  1. In the Google Cloud console, go to the APIs & Services > API Library page.

    Go to API Library

  2. On the API Library page, enter the required API name into the search bar.
  3. In the search results, select the API page.
  4. On the API page, click Enable.
  5. Repeat these steps for each of the services listed in the preceding table.

Google Cloud CLI

Use the following commands to enable the services:

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

For more information about the gcloud services, see gcloud services.

Create an API config

Before API Gateway can be used to manage traffic to your deployed Cloud Run backend, it needs an API config.

You can create an API config using an OpenAPI description that contains specialized annotations to define the chosen API Gateway behavior. You will need to add a Google-specific field that contains the URL for each Cloud Run app so that API Gateway has the information it needs to invoke an app.

For more details on supported OpenAPI extensions, see the following:

To create your API config:

  1. Create a text file called openapi-run.yaml. For convenience, this page refers to the OpenAPI description by that filename, but you can name it something else if you prefer.
  2. Copy the contents of the following file into your openapi-run.yaml file:

    OpenAPI 2.0

    # openapi-run.yaml
    swagger: '2.0'
    info:
      title: API_ID optional-string
      description: Sample API on API Gateway with a Cloud Run backend
      version: 1.0.0
    schemes:
    - https
    produces:
    - application/json
    x-google-backend:
      address: APP_URL
    paths: