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.
- Create or select a Google Cloud project.
- If you haven't deployed your own Cloud Run, deploy a sample service. See step 7 in Before you begin.
- Enable the required API Gateway services.
- Create an OpenAPI description that describes your API, and configure the routes to your Cloud Run backend service. See Creating an API config.
- Deploy an API gateway using your API config. See Deploying an API gateway.
- Track activity to your services. See Tracking API activity.
- Avoid incurring charges to your Google Cloud account. See Clean up.
Before you begin
In the Google Cloud console, go to the Dashboard page and select or create a Google Cloud project.
Make sure that billing is enabled for your project.
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.
Download and install the Google Cloud CLI.
Update
gcloudcomponents:gcloud components update
Set the default project. Replace PROJECT_ID with your Google Cloud project ID
gcloud config set project PROJECT_ID
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
In the Google Cloud console, go to the APIs & Services > API Library page.
- On the API Library page, enter the required API name into the search bar.
- In the search results, select the API page.
- On the API page, click Enable.
- 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.comgcloud services enable servicemanagement.googleapis.comgcloud 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:
- 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. - Copy the contents of the following file into your
openapi-run.yamlfile: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: