Get started with Python

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

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.

New Google Cloud users might be eligible for a free trial.

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

  1. 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.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. To create a Firestore database in Native mode, complete the following steps:
    1. In the Google Cloud console, go to the Firestore create database page.

      Go to Firestore create database

    2. From the Select a Cloud Firestore mode screen, click Select Native Mode.
    3. 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.
    4. Click Create Database.
  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  8. Clone the sample repo and open the sample application in Cloud Shell:
    Go to Cloud Shell

    Cloud Shell provides command-line access to your Google Cloud resources directly from the browser.

  9. To download the sample code and change into the app directory, click Proceed.
  10. In Cloud Shell, configure the gcloud tool 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 gcloud tool to deploy and monitor your app.

Run your app

  1. In Cloud Shell, install your app dependencies using pip:

    pip3 install -r requirements.txt --user
    
  2. Run the Gunicorn HTTP server:
    ~/.local/bin/gunicorn -b :8080 main:app
    
  3. 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.

  1. Use Cloud Build to build a Docker container and publish to Container Registry:
    gcloud builds submit --tag gcr.io/PROJECT_ID/bookshelf .

    Replace PROJECT_ID with the Google Cloud project ID that you created.

  2. Run the container with Cloud Run:
    gcloud run deploy bookshelf --image gcr.io/PROJECT_ID/bookshelf \
    --platform managed --region us-central1 --allow-unauthenticated

    Your app is now viewable at the URL displayed in the output of gcloud run: