This tutorial is intended for those new to building apps in the cloud, such as engineers and web developers, who want to learn key app development concepts as they apply to Google Cloud.
Objectives
- Learn basic Google Cloud tools, such as the Google Cloud console and
gcloud. - Deploy your app to Cloud Run.
- Persist your data with Firestore.
- Store file uploads in Cloud Storage.
- Monitor your app using Google Cloud Observability.
Costs
In this document, you use the following billable components of Google Cloud:
The instructions in this document are designed to keep your resource usage within
the limits of Google Cloud's Always Free
tier.
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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
To create a Firestore database in Native mode, complete the following steps:
-
In the Google Cloud console, go to the Firestore create database page.
- From the Select a Cloud Firestore mode screen, click Select Native Mode.
- Select a location for your Firestore database. This location setting is the default Google Cloud resource location for your Google Cloud project . This location is used for Google Cloud services in your Google Cloud project that require a location setting, specifically, your default Cloud Storage bucket and your Cloud Run app.
- Click Create Database.
-
In the Google Cloud console, go to the Firestore create database page.
-
Enable the Cloud Run Admin, Cloud Storage, Cloud Logging, and Error Reporting APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Clone the sample repo and open the sample application in Cloud Shell:
Go to Cloud ShellCloud Shell provides command-line access to your Google Cloud resources directly from the browser.
- To download the sample code and change into the app directory, click Proceed.
-
In Cloud Shell, configure the
gcloudtool to use your new Google Cloud project:# Configure gcloud for your project gcloud config set project PROJECT_ID
Replace PROJECT_ID with the Google Cloud project ID that you created using the Google Cloud console.
The Google Cloud CLI is the primary way you interact with your Google Cloud resources from the command line. In this tutorial, you use the
gcloudtool to deploy and monitor your app.
Run your app
-
In Cloud Shell, install your app dependencies using
pip:pip3 install -r requirements.txt --user
-
Run the Gunicorn HTTP server:
~/.local/bin/gunicorn -b :8080 main:app
- In Cloud Shell, click Web preview , and select Preview on port 8080. This opens a new window with your running app.
Deploy your app to Cloud Run
Google Cloud offers several options for running your code. For this example, you use Cloud Run to deploy a scalable app to Google Cloud. Cloud Run doesn't require you to manage servers and automatically scales to support traffic spikes.
- Use Cloud Build to build a Docker container and publish to Container Registry:
gcloud builds submit --tag gcr.io/
PROJECT_ID/bookshelf .Replace
PROJECT_IDwith the Google Cloud project ID that you created. - Run the container with Cloud Run:
gcloud run deploy bookshelf --image gcr.io/
PROJECT_ID/bookshelf \ --platform managed --region us-central1 --allow-unauthenticatedYour app is now viewable at the URL displayed in the output of
gcloud run: