检查点
Deploying the Endpoints configuration
/ 20
Deploying the API backend
/ 20
Sending requests to the API
/ 20
Add a quota to the API
/ 20
Create API key and testing quota limit by sending requests
/ 20
Cloud Endpoints: Qwik Start
GSP164
Overview
In this lab you will deploy a sample API with Google Cloud Endpoints, which are a set of tools for generating APIs from within an App Engine application. The sample code will include:
- A REST API that you can query to find the name of an airport from its three-letter
IATA
code (for example, SFO, JFK, AMS). - A script that uploads the API configuration to Cloud Endpoints.
- A script that deploys a Google App Engine flexible backend to host the sample API.
After you send some requests to the sample API, you can view the Cloud Endpoints Activity Graphs and Logs. These are tools that allow you to monitor your APIs and gain insights into their usage.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Getting the sample code
- Enter the following command in Cloud Shell to get the sample API and scripts:
- Change to the directory that contains the sample code:
Task 2. Deploying the Endpoints configuration
To publish a REST API to Endpoints, an OpenAPI configuration file that describes the API is required. The lab's sample API comes with a pre-configured OpenAPI file called openapi.yaml
.
Endpoints uses Google Service Management
, an infrastructure service of Google Cloud, to create and manage APIs and services. To use Endpoints to manage an API, you deploy the API's OpenAPI configuration to Service Management.
To deploy the Endpoints configuration...
- In the
endpoints-qwikstart
directory, enter the following:
- Run the following script, which is included in the sample:
Cloud Endpoints uses the host
field in the OpenAPI configuration file to identify the service. The deploy_api.sh
script sets the ID of your Cloud project as part of the name configured in the host
field. (When you prepare an OpenAPI configuration file for your own service, you will need to do this manually.)
The script then deploys the OpenAPI configuration to Service Management using the command: gcloud endpoints services deploy openapi.yaml
As it is creating and configuring the service, Service Management outputs some information to the console. You can safely ignore the warnings about the paths in openapi.yaml
not requiring an API key. On successful completion, you see a line like the following that displays the service configuration ID and the service name:
Click Check my progress to verify the objective.
Task 3. Deploying the API backend
So far you have deployed the OpenAPI configuration to Service Management, but you have not yet deployed the code that will serve the API backend. The deploy_app.sh
script included in the lab sample creates an App Engine flexible environment to host the API backend, and then the script deploys the API to App Engine.
- To deploy the API backend, make sure you are in the
endpoints-quickstart/scripts
directory. Then, run the following script:
The script runs the following command to create an App Engine flexible environment in the gcloud app create --region="$REGION"
It takes a couple minutes to create the App Engine flexible backend.
ERROR: NOT_FOUND: Unable to retrieve P4SA: from GAIA
message, rerun the deploy_app.sh
script.
You'll see the following displayed in Cloud Shell after the App Engine is created:
The script goes on to run the gcloud app deploy
command to deploy the sample API to App Engine.
You'll then see a line like the following in Cloud Shell:
It takes several minutes for the API to be deployed to App Engine. You'll see a line like the following when the API is successfully deployed to App Engine:
Click Check my progress to verify the objective.
Task 4. Sending requests to the API
- After deploying the sample API, you can send requests to it by running the following script:
The script echoes the curl
command that it uses to send a request to the API, and then displays the result. You'll see something like the following in Cloud Shell:
The API expects one query parameter, iataCode
, that is set to a valid IATA airport code such as SEA or JFK.
- To test, run this example in Cloud Shell:
You just deployed and tested an API in Cloud Endpoints!
Click Check my progress to verify the objective.
Task 5. Tracking API activity
With APIs deployed with Cloud Endpoints, you can monitor critical operations metrics in the Cloud Console and gain insight into your users and usage with Cloud Logging:
- Run this traffic generation script in Cloud Shell to populate the graphs and logs:
- In the Console, go to Navigation menu > Endpoints > Services and click Airport Codes service to look at the activity graphs for your service. It may take a few moments for the requests to be reflected in the graphs. You can do this while you wait for data to be displayed:
-
If the Permissions side panel is not open, click Show Permissions Panel. The Permissions panel allows you to control who has access to your API and the level of access.
-
Click the Deployment history tab. This tab displays a history of your API deployments, including the deployment time and who deployed the change.
-
Click the Overview tab. Here you'll see the traffic coming in. After the traffic generation script has been running for a minute, scroll down to see the three lines on the Total latency graph (50th, 95th, and 99th percentiles). This data provides a quick estimate of response times.
-
At the bottom of the Endpoints graphs, under Method, click the View logs link for GET/airportName. The Logs Viewer page displays the request logs for the API.
-
Enter CTRL+C in Cloud Shell to stop the script.
Task 6. Add a quota to the API
Cloud Endpoints lets you set quotas so you can control the rate at which applications can call your API. Quotas can be used to protect your API from excessive usage by a single client.
- Deploy the Endpoints configuration that has a quota:
- Redeploy your app to use the new Endpoints configuration (this may take a few minutes):
Click Check my progress to verify the objective.
- In the Console, navigate to Navigation menu > APIs & Services > Credentials.
- Click Create credentials and choose API key. A new API key is displayed on the screen.
- Click the Copy to clipboard icon to copy it to your clipboard.
- In Cloud Shell, type the following. Replace YOUR-API-KEY with the API key you just created:
- Send your API a request using the API key variable you just created:
You'll see something like the following on the console:
- The API now has a limit of 5 requests per second. Run the following command to send traffic to the API and trigger the quota limit:
-
After running the script for 5-10 seconds, enter CTRL+C in Cloud Shell to stop the script.
-
Send another authenticated request to the API:
You'll see something like the following on the console:
If you get a different response, try running the generate_traffic_with_key.sh
script again and retry.
Click Check my progress to verify the objective.
Congratulations!
Congratulations! You've successfully rate-limited your API. You can also set varying limits on different API methods, create multiple kinds of quotas, and keep track of which consumers use which APIs.
Take your next lab
This lab is part of a series of labs called Qwik Starts. These labs are designed to give you a little taste of the many features available with Google Cloud. Search for “Qwik Starts” in the lab catalog to find the next lab you’d like to take!
Next steps / Learn more
For more information about quotas, see the following:
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual last updated April 25, 2024
Lab last tested April 25, 2024
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.