Hey there!

This codelab will walk you through the process of creating a simple "Hello World" application using Python Flask and deploying it to Google Cloud Run.

In order to follow this codelab, you'll need the following:

  1. A Google Cloud Platform account.
  2. Google Cloud CLI installed and initialized.
  3. Python installed locally.
  1. Head over to Google Cloud Platform and create an account.
  2. Enable billing
  3. Create a new project, let's name it project-x.

Follow the official guide to install the Google Cloud CLI: Installing Google Cloud SDK.

  1. Set up a new directory for your project:
    mkdir hello-world-cloud-run
    cd hello-world-cloud-run
    
  2. Create a Python virtual environment and activate it:
    python3 -m venv .venv
    source .venv/bin/activate
    
  3. Install Flask:
    pip install Flask
    
  4. Create a file named main.py with the following content:
    from flask import Flask
    import os
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello, World!'
    
    if __name__ == '__main__':
        app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
    
  1. Run your application locally:
    python main.py
    
  2. Open your web browser and go to http://localhost:8080. You should see the "Hello, World!" message.
  1. Generate the requirements.txt file
    pip list --format=freeze > requirements.txt
    
  2. Deploy your application to Google Cloud Run:
    gcloud run deploy
    
  3. Wait for the deployment to complete. You will receive a URL where your application is live.

Congratulations! You've just built and deployed a simple application on Google Cloud Run using Python and Flask!