Getting started with Python on Compute Engine

This tutorial shows how to get started with Compute Engine. Follow this tutorial by deploying a Hello World Python web app to Compute Engine. For help getting started with App Engine, see the App Engine standard environment.

Objectives

  • Use Cloud Shell to download and deploy a Hello World sample app.
  • Deploy a Hello World sample app to a single Compute Engine instance.

Costs

This tutorial uses the following billable components of Google Cloud:

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.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Console, on the project selector page, select or create a Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

  4. Enable the Compute Engine API.

    Enable the API

  5. In the Google Cloud Console, open the app in Cloud Shell.

    Go to Cloud Shell

    Cloud Shell provides command-line access to your cloud resources directly from the browser. Open Cloud Shell in your browser and click Proceed to download the sample code and change into the app directory.

  6. In Cloud Shell, configure the gcloud tool to use your new Google Cloud project:
    # Configure gcloud for your project
    gcloud config set project YOUR_PROJECT_ID
    

Running the app in Cloud Shell

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

    pip3 install -r requirements.txt --user
    
  2. Install the Gunicorn HTTP server:

    pip3 install gunicorn --user
    
  3. Run the Gunicorn HTTP server:

    ~/.local/bin/gunicorn -b :8080 main:app
    
  4. In Cloud Shell, click Web preview , and select Preview on port 8080. This opens a new window with your running app.

  5. To stop the local web server, press Control+C.

Deploying to a single instance

This section walks you through running a single instance of your app on Compute Engine.

Single-instance deployment.

From Cloud Shell, you can deploy to a single Compute Engine instance virtual machine (VM) which runs your app.

Use a startup script to initialize an instance

You need a way to instruct your instance to download and run your code. An instance can have a startup script that runs whenever the instance is started or restarted.

# Install Stackdriver logging agent
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh

# Install or update needed software
apt-get update
apt-get install -yq git supervisor python python-pip
pip install --upgrade pip virtualenv

# Account to own server process
useradd -m -d /home/pythonapp pythonapp

# Fetch source code
export HOME=/root
git clone https://github.com/GoogleCloudPlatform/getting-started-python.git /opt/app

# Python environment setup
virtualenv -p python3 /opt/app/gce/env
source /opt/app/gce/env/bin/activate
/opt/app/gce/env/bin/pip install -r /opt/app/gce/requirements.txt

# Set ownership to newly created account
chown -R pythonapp:pythonapp /opt/app

# Put supervisor configuration in proper place
cp /opt/app/gce/python-app.conf /etc/supervisor/conf.d/python-app.conf

# Start service via supervisorctl
supervisorctl reread
supervisorctl update

The startup script performs these tasks:

  • Installs Python 3 and Pip.

  • Installs the Stackdriver Logging agent. The agent automatically collects logs from syslog.

  • Installs Supervisor to run the app as a daemon.

  • Clones the app's source code from the Cloud Source Repositories and installs dependencies.

  • Configures Supervisor to run the app. Supervisor makes sure the app is restarted if it exits unexpectedly or is terminated by an admin or other process. It also sends the app's stdout and stderr to syslog to be collected by the Logging agent.

Create and configure a Compute Engine instance

  1. Create a Compute Engine instance:

    Linux/macOS

    MY_INSTANCE_NAME="my-app-instance"
    ZONE=us-central1-a
    
    gcloud compute instances create $MY_INSTANCE_NAME \
        --image-family=debian-9 \
        --image-project=debian-cloud \
        --machine-type=g1-small \
        --scopes userinfo-email,cloud-platform \
        --metadata-from-file startup-script=startup-script.sh \
        --zone $ZONE \
        --tags http-server

    Windows

    gcloud compute instances create my-app-instance ^
        --image-family=debian-9 ^
        --image-project=debian-cloud ^
        --machine-type=g1-small ^
        --scopes userinfo-email,cloud-platform ^
        --metadata-from-file startup-script=gce/startup-script.sh ^
        --zone us-central1-f ^
        --tags http-server
    

    Replace the zone with a development zone, for example us-central1-a. For more information on regions and zones, see Geography and regions.

    This creates a new instance, allows it to access Google Cloud services, and runs your startup script. The instance name is my-app-instance.

  2. Check the progress of the instance creation:

    gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
    

    Replace YOUR_ZONE with the zone you deployed your instance to.

    When the startup script is complete, you see the following message:

    startup-script: INFO Finished running startup scripts.
    
  3. Create a firewall rule to allow traffic to your instance:

    gcloud compute firewall-rules create default-allow-http-8080 \
        --allow tcp:8080 \
        --source-ranges 0.0.0.0/0 \
        --target-tags http-server \
        --description "Allow port 8080 access to http-server"
    

  4. Get the external IP address of your instance:

    gcloud compute instances list
    
  5. To see your app running, enter this URL in your browser:

    http://YOUR_INSTANCE_IP:8080
    

    Replace YOUR_INSTANCE_IP with the external IP address of your instance.

Manage and monitor an instance

You can use the Cloud Console to monitor and manage your instance.

  1. In the Cloud Console, go to the VM instances page.

    Go to the VM instances page

  2. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
  3. To view all of the logs generated by your Compute Engine resources, go to the Logs viewer page. Go to Logs viewer

    Stackdriver Logging is automatically configured to gather logs from various common services, including syslog.

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Delete the project

  1. In the Cloud Console, go to the Manage resources page.

    Go to the Manage resources page

  2. In the project list, select the project that you want to delete and then click Delete .
  3. In the dialog, type the project ID and then click Shut down to delete the project.

Delete the individual resources

gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all
gcloud compute firewall-rules delete default-allow-http-8080

What's next