This tutorial shows you how to configure and deploy a sample API and the Extensible Service Proxy V2 (ESPv2) running in prebuilt Docker containers on Managed Instance Group (MIGs) .
The sample code's REST API is described using the OpenAPI specification. The tutorial also shows you how to create an API key and use it in requests to the API.
For an overview of Cloud Endpoints, see About Endpoints and Endpoints architecture.
Objectives
Use the following high-level task list as you work through the tutorial. All tasks are required to successfully send requests to the API.- Set up a Google Cloud project. See Before you begin.
- Download the sample code. See Getting the sample code.
- Configure the
openapi.yaml
file, which is used to configure Endpoints. See Configuring Endpoints. - Deploy the Endpoints configuration to create a Endpoints service. See Deploying the Endpoints configuration.
- Deploy the API and ESPv2 on the Managed Instance Group (MIGs) backend. See Deploying the API backend.
- Send a request to the API by using an IP address. See Sending a request by using IP address.
- Configure a DNS record for the sample API. See Configuring DNS for Endpoints.
- Send a request to the API by using the fully qualified domain name. See Sending a request by using FQDN.
- Track API activity. See Tracking API activity.
- Avoid incurring charges to your Google Cloud account. See Clean up.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
- Make a note of the project ID because it's needed later.
-
You need an application to send requests to the sample API.
- Linux and macOS users: This tutorial provides an example of using
curl
, which typically comes pre-installed on your operating system. If you don't havecurl
, you can download it from thecurl
Releases and downloads page. - Windows users: This tutorial provides an example using
Invoke-WebRequest
, which is supported in PowerShell 3.0 and later.
- Linux and macOS users: This tutorial provides an example of using
- Download the Google Cloud CLI.
-
Update the gcloud CLI and install the Endpoints
components.
gcloud components update
-
Make sure that the Google Cloud CLI (
gcloud
) is authorized to access your data and services on Google Cloud: In the new browser tab that opens, select an account.gcloud auth login
-
Set the default project to your project ID.
gcloud config set project YOUR_PROJECT_ID
Replace YOUR_PROJECT_ID with your project ID. If you have other Google Cloud projects, and you want to use
gcloud
to manage them, see Managing gcloud CLI configurations.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
Downloading the sample code
Download the sample code to your local machine.
To clone or download the sample API:
Alternatively, download the sample
as a zip file and extract it.
To clone or download the sample API:
Alternatively, download the sample
as a zip file and extract it.
To clone or download the sample API:
To clone or download the sample API:
Alternatively, download the sample
as a zip file and extract it.
To clone or download the sample API:
Alternatively, download the sample
as a zip file and extract it.
To clone or download the sample API:
Alternatively, download the sample
as a zip file and extract it.
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/endpoints/getting-started
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/endpoints/getting-started
GOPATH
environment variable is set.
go get -d github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
git clone https://github.com/GoogleCloudPlatform/php-docs-samples
cd php-docs-samples/endpoints/getting-started
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples
cd ruby-docs-samples/endpoints/getting-started
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/endpoints/getting-started
Configuring Endpoints
The sample code includes the OpenAPI configuration file, openapi.yaml
, which
is based on
OpenAPI Specification v2.0.
You configure and deploy openapi.yaml
on your local machine.
To configure Endpoints:
- In the sample code directory, open the
openapi.yaml
configuration file.Java Python Go PHP Ruby NodeJS Note the following:
- The configuration sample displays the lines near the
host
field, which you need to modify. To deploy theopenapi.yaml
file to Endpoints, the complete OpenAPI document is required. - The example
openapi.yaml
file contains a section for configuring authentication that isn't needed for this tutorial. You don't need to configure the lines with YOUR-SERVICE-ACCOUNT-EMAIL and YOUR-CLIENT-ID. - OpenAPI is a language-agnostic specification. The same
openapi.yaml
file is in thegetting-started
sample in each language GitHub repository for convenience.
- The configuration sample displays the lines near the
- In the
host
field, replace the text with the Endpoints service name, which should be in the following format:host: "echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog"
Replace YOUR_PROJECT_ID with your Google Cloud project ID. For example:
host: "echo-api.endpoints.example-project-12345.cloud.goog"
Note that echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog
is the Endpoints service name. It isn't the fully qualified
domain name (FQDN) that you use for sending requests to the API.
For information about the fields in the OpenAPI document that
Endpoints requires, see Configuring
Endpoints.
After you have finished all the following configuration steps such that you can
successfully send requests to the sample API by using an IP address, see
Configuring Endpoints DNS for
information on how to configure echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog
to be the FQDN.
Deploying the Endpoints configuration
To deploy the Endpoints configuration, you use the gcloud endpoints
services deploy
command. This command uses Service Management
to create a managed service.
To deploy the Endpoints configuration:
- Make sure you are in the
endpoints/getting-started
directory. - Upload the configuration and create a managed service:
gcloud endpoints services deploy openapi.yaml
The gcloud
command then calls the Service Management
API to create a managed service with the name that you specified in the
host
field of the openapi.yaml
file.
Service Management configures the service according to the
settings in the openapi.yaml
file. When you make changes to
openapi.yaml
, you must redeploy the file to update the
Endpoints service.
As it is creating and configuring the service, Service Management
outputs information to the terminal. You can safely ignore the warnings about
the paths in the openapi.yaml
file not requiring an API key.
When it finishes configuring the service, Service Management displays a
message with the service configuration ID and the service name, similar to the
following:
Service Configuration [2017-02-13r0] uploaded for service [echo-api.endpoints.example-project-12345.cloud.goog]
In the preceding example, 2017-02-13r0
is the service
configuration ID, and echo-api.endpoints.example-project-12345.cloud.goog
is the
Endpoints service. The service configuration ID consists of a
date stamp followed by a revision number. If you deploy the
openapi.yaml
file again on the same day, the revision
number is incremented in the service configuration ID. You can view
the Endpoints service configuration on the Endpoints >
Services page in the Google Cloud console.
If you get an error message, see Troubleshooting Endpoints configuration deployment.
Checking required services
At a minimum, Endpoints and ESP require the following Google services to be enabled:Name | Title |
---|---|
servicemanagement.googleapis.com |
Service Management API |
servicecontrol.googleapis.com |
Service Control API |
In most cases, the gcloud endpoints services deploy
command enables these
required services. However, the gcloud
command completes successfully but
doesn't enable the required services in the following circumstances:
If you used a third-party application such as Terraform, and you don't include these services.
You deployed the Endpoints configuration to an existing Google Cloud project in which these services were explicitly disabled.
Use the following command to confirm that the required services are enabled:
gcloud services list
If you do not see the required services listed, enable them:
gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com
Also enable your Endpoints service:
gcloud services enable ENDPOINTS_SERVICE_NAME
To determine the ENDPOINTS_SERVICE_NAME you can either:
After deploying the Endpoints configuration, go to the Endpoints page in the Cloud console. The list of possible ENDPOINTS_SERVICE_NAME are shown under the Service name column.
For OpenAPI, the ENDPOINTS_SERVICE_NAME is what you specified in the
host
field of your OpenAPI spec. For gRPC, the ENDPOINTS_SERVICE_NAME is what you specified in thename
field of your gRPC Endpoints configuration.
For more information about the gcloud
commands, see
gcloud
services.
Deploying the API Backend
Create an instance template
Create a template that you will use to create a group of VM instances. Each instance created from the template launches an ESPv2 and a backend application server.
In the Google Cloud console, go to the Instance templates page.
Click Create instance template.
Under Name, enter
load-balancing-espv2-template
.Under Machine configuration, set the Machine type to
e2-micro
.Under Boot disk, set the Image to
Container Optimized OS stable version
.Under Firewall, select Allow HTTP traffic.
Click Management, security, disks, networking, sole tenancy to reveal the advanced settings.
Click the Management tab. Under Automation, enter the following Startup script. Remember to update ENDPOINTS_SERVICE_NAME.
sudo docker network create --driver bridge esp_net sudo docker run \ --detach \ --name=echo \ --net=esp_net \ gcr.io/google-samples/echo-python:1.0 sudo docker run \ --detach \ --name=esp \ --publish=80:9000 \ --net=esp_net \ gcr.io/endpoints-release/endpoints-runtime:2 \ --service=ENDPOINTS_SERVICE_NAME \ --rollout_strategy=managed \ --listener_port=9000 \ --healthz=/healthz \ --backend=echo:8080
The script gets, installs, and launches the echo application server and the ESPv2 proxy server at instance startup.
Click Create.
Wait until the template has been created before continuing.
Create a regional managed instance group
To run the application, use the instance template to create a regional managed instance group:
In the Google Cloud console, go to the Instance groups page.
Click Create instance group.
Under Name, enter
load-balancing-espv2-group
.Under Location, select Multiple zones.
Under Region, select us-central1.
Click the Configure zones drop-down menu to reveal Zones. Select the following zones:
- us-central1-b
- us-central1-c
- us-central1-f
Under Instance template, select
load-balancing-espv2-template
.Under Autoscaling, select Don't autoscale.
Set Number of instances to
3
.Under Instance redistribution, select On.
Under Autohealing and Health check, select No health check.
Click Create. This redirects you back to the Instance groups page.
Create a load balancer
This section explains the steps required to create a global load balancer that directs HTTP traffic to your instance group.This load balancer uses a frontend to receive incoming traffic and a backend to distribute this traffic to healthy instances. Because the load balancer is made of multiple components, this task is divided into several parts:
- Backend configuration
- Frontend configuration
- Review and finalize
Complete all the steps to create the load balancer.
In the Google Cloud console, go to the Create a load balancer page.
In the Application Load Balancer (HTTP/S) section, click Start configuration.
Under Internet facing or internal only, select From Internet to my VMs. Then, click Continue.
For the Name of the load balancer, enter
espv2-load-balancer
.
Backend configuration
- In the left panel of the Create global external Application Load Balancer page, click Backend configuration.
- Click Create or select backend services & backend buckets to open a drop-down menu. Click Backend services, then click Create a backend service.
- In the new window, for the Name of the backend
application, enter
espv2-backend
. - Set Instance group to
load-balancing-espv2-group
. - Set Port numbers to
80
. This allows HTTP traffic between the load balancer and the instance group. - Under Balancing mode, select Utilization.
- Click Done to create the backend.
Create the health check for the backend of the load balancer:
- Under Health check, select Create a health check (or Create another health check) from the drop-down menu. A new window opens.
- In the new window under Name, enter
espv2-load-balancer-check
. - Set the Protocol to HTTP.
- Under Port, enter
80
. - For this tutorial, set the Request path to
/healthz
, which is a path that the ESPv2 is set up to respond to. Set the following Health criteria:
- Set Check interval to
3
seconds. This defines the amount of time from the start of one probe to the start of the next one. - Set Timeout to
3
seconds. This defines the amount of time that Google Cloud waits for a response to a probe. Its value must be less than or equal to the check interval. - Set Healthy Threshold to
2
consecutive successes. This defines the number of sequential probes that must succeed in order for the instance to be considered healthy. - Set Unhealthy Threshold to
2
consecutive failures. This defines the number of sequential probes that must fail in order for the instance to be considered unhealthy.
- Set Check interval to
Click Save and continue to create the health check.
Click Create to create the backend service.
Frontend configuration
- In the left panel of the Create global external Application Load Balancer page, click Frontend configuration.
- On the Frontend configuration page, under Name, enter
espv2-ipv4-frontend
. - Set the Protocol to
HTTP
. - Set the Port to
80
. - Click Done to create the frontend.
Review and finalize
Verify your load balancing settings before creating the load balancer:
- In the left panel of the Create global external Application Load Balancer page, click Review and finalize.
On the Review and finalize page, verify the following Backend settings:
- The Backend service is
espv2-backend
. - The Endpoint protocol is
HTTP
. - The Health check is
espv2-load-balancer-check
. - The Instance group is
load-balancing-espv2-group
.
- The Backend service is
On the same page, verify that Frontend uses an IP address with a Protocol of
HTTP
.
In the left panel of the Create global external Application Load Balancer page, click Create to finish creating the load balancer.
You might need to wait a few minutes for the load balancer creation to finish.
After the load balancer is created, find the IP address from the Load Balancer page.
Sending a request by using an IP address
After the sample API and ESPv2 are running on the deployed backend, you can send requests to the API from your local machine.
Create an API key and set an environment variable
The sample code requires an API key. To simplify the request, you set an environment variable for the API key.
In the same Google Cloud project that you used for your API, create an API key on the API credentials page. If you want to create an API key in a different Google Cloud project, see Enabling an API in your Google Cloud project.
- Click Create credentials, and then select API key.
- Copy the key to the clipboard.
- Click Close.
- On your local computer, paste the API key to assign it to an environment
variable:
- In Linux or macOS:
export ENDPOINTS_KEY=AIza...
- In Windows PowerShell:
$Env:ENDPOINTS_KEY="AIza..."
- In Linux or macOS:
Send the request
Linux or mac OS
Use curl
to send an HTTP request by using the ENDPOINTS_KEY environment
variable you set previously. Replace IP_ADDRESS with
the external IP address of your instance.
curl --request POST \ --header "content-type:application/json" \ --data '{"message":"hello world"}' \ "http://IP_ADDRESS:80/echo?key=${ENDPOINTS_KEY}"
In the preceding curl
:
- The
--data
option specifies the data to post to the API. - The
--header
option specifies that the data is in JSON format.
PowerShell
Use Invoke-WebRequest
to send an HTTP request by using the ENDPOINTS_KEY
environment variable you set previously. Replace
IP_ADDRESS with the external IP address of your
instance.
(Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' ` -Headers @{"content-type"="application/json"} ` -URI "http://IP_ADDRESS:80/echo?key=$Env:ENDPOINTS_KEY").Content
In the previous example, the first two lines end in a backtick. When you paste the example into PowerShell, make sure there isn't a space following the backticks. For information about the options used in the example request, see Invoke-WebRequest in the Microsoft documentation.
Third-party app
You can use a third-party application such as the Chrome browser extension Postman to send the request:
- Select
POST
as the HTTP verb. - For the header, select the key
content-type
and the valueapplication/json
. - For the body, enter the following:
{"message":"hello world"}
-
In the URL, use the actual API key rather than the environment variable.
For example:
http://192.0.2.0:80/echo?key=AIza...
The API echoes back the message that you send, and responds with the following:
{
"message": "hello world"
}
If you didn't get a successful response, see Troubleshooting response errors.
You just deployed and tested an API in Endpoints!
Configuring DNS for Endpoints
Because the Endpoints service name for the API is in the
.endpoints.YOUR_PROJECT_ID.cloud.goog
domain, you can
use it as the fully qualified domain name (FQDN) by making a small
configuration change in your openapi.yaml
file. This way, you can
send requests to the sample API by using
echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog
instead of the IP address.
To configure Endpoints DNS:
- Open your OpenAPI configuration file,
openapi.yaml
, and add thex-google-endpoints
property at the top level of the file (not indented or nested) as shown in the following snippet:host: "echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog" x-google-endpoints: - name: "echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog" target: "IP_ADDRESS"
- In the
name
property, replace YOUR_PROJECT_ID with your project ID. - In the
target
property, replace IP_ADDRESS with the IP address that you used when you sent a request to the sample API. - Deploy your updated OpenAPI configuration file to Service Management:
gcloud endpoints services deploy openapi.yaml
For example, assume the openapi.yaml
file has the following
configured:
host: "echo-api.endpoints.example-project-12345.cloud.goog" x-google-endpoints: - name: "echo-api.endpoints.example-project-12345.cloud.goog" target: "192.0.2.1"
When you deploy the openapi.yaml
file by using the preceding
gcloud
command, Service Management creates a DNS A-record,
echo-api.endpoints.my-project-id.cloud.goog
, which resolves to the
target IP address, 192.0.2.1
. It might take a few minutes for the
new DNS configuration to propagate.
Configuring SSL
For more details on how to configure DNS and SSL, see Enabling SSL for Endpoints.
Sending a request by using FQDN
Now that you have the DNS record configured for the sample API, send a request to it by using the FQDN (replace YOUR_PROJECT_ID with your project ID) and the ENDPOINTS_KEY environment variable set previously:- In Linux or mac OS:
curl --request POST \ --header "content-type:application/json" \ --data '{"message":"hello world"}' \ "http://echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog:80/echo?key=${ENDPOINTS_KEY}"
- In Windows PowerShell:
(Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' -Headers @{"content-type"="application/json"} -URI "http://echo-api.endpoints.[YOUR_PROJECT_ID].cloud.goog:80/echo?key=$Env:ENDPOINTS_KEY").Content
Tracking API activity
To track API activity:
- Look at the activity graphs for your API in the Endpoints > Services page.
Go to the Endpoints Services page
It may take a few moments for the request to be reflected in the graphs. - Look at the request logs for your API in the Logs Explorer page.
Creating a developer portal for the API
You can use Cloud Endpoints Portal to create a developer portal, a website that you can use to interact with the sample API. To learn more, see Cloud Endpoints Portal overview.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Make sure that the gcloud CLI (
gcloud
) is authorized to access your data and services on Google Cloud:gcloud auth login
Enter the following to display the project IDs for your Google Cloud projects:
gcloud projects list
Using the applicable project ID from the previous step, set the default Google Cloud project to the one that your application is in:
gcloud config set project [YOUR_PROJECT_ID]
Obtain the name of all managed services in your Google Cloud project:
gcloud endpoints services list
Delete the service from Service Management. Replace
SERVICE_NAME
with the name of the service you want to remove.gcloud endpoints services delete SERVICE_NAME
Running
gcloud endpoints services delete
doesn't immediately delete the managed service. Service Management disables the managed service for 30 days, which allows you time to restore it if you need to. After 30 days, Service Management permanently deletes the managed service.Go to the Load Balancer page.
Delete load balancer
espv2-load-balancer
with backend serviceespv2-backend
and health checkespv2-load-balancer-check
.Go to the Instance Groups page.
Delete
load-balancing-espv2-group
Go to the Instance Template page.
Delete
load-balancing-espv2-template
.