0% found this document useful (0 votes)
10 views5 pages

Deploy Python Apps on GCP App Engine

Uploaded by

luthorking92
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Deploy Python Apps on GCP App Engine

Uploaded by

luthorking92
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Application Deployment Using App Engine

in GCP
1. What is App Engine?
Google App Engine (GAE) is a Platform as a Service (PaaS) on Google Cloud that lets
you deploy web applications without managing servers.

Key points:

• You just provide your code → GAE handles scaling, load balancing, patching.
• Supports multiple runtimes (Python, Java, [Link], etc.).
• Two main environments:
o Standard environment (sandboxed, very fast scaling, supports specific
Python versions)
o Flexible environment (Docker-based, more control, slower scaling)

For a simple Python app, we usually use App Engine Standard.

2. Prerequisites
Before deployment, you need:

1. A Google Cloud account.


2. A GCP Project created.
3. Billing enabled.
4. Google Cloud SDK (gcloud) installed on your local machine.
5. Python installed (e.g., Python 3.10+).

3. Create a Simple Python Web Application


We’ll create a very basic Flask app.

3.1 Create a project folder


mkdir my-python-app
cd my-python-app

3.2 Create and activate a virtual environment (optional but recommended)


python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
3.3 Install Flask
pip install Flask

3.4 Create [Link]

Create a file named [Link]:

from flask import Flask

app = Flask(__name__)

@[Link]('/')
def hello():
return "Hello, App Engine! This is a simple Python app."

if __name__ == '__main__':
[Link](host='[Link]', port=8080)

App Engine will look for a WSGI app callable named app in [Link].

3.5 Create [Link]


Flask==3.0.0

(Version can vary; just ensure Flask is listed.)

4. Create [Link] (App Engine Configuration File)


[Link] tells App Engine how to run your app.

Create [Link] in the same folder:

runtime: python311 # or python310, depending on what you use

entrypoint: gunicorn -b :$PORT main:app

handlers:
- url: /.*
script: auto

Explanation:

• runtime: specifies the Python runtime (e.g., python311).


• entrypoint: how to start the app in production (using gunicorn).
• handlers: route all URLs (/.*) to the app.

Update [Link] to add gunicorn too:

Flask==3.0.0
gunicorn==21.2.0

Run to install:

pip install gunicorn

5. Initialize gcloud and Set Project


5.1 Login to gcloud
gcloud auth login

This opens a browser to authenticate.

5.2 Set your project


gcloud config set project YOUR_PROJECT_ID

Replace YOUR_PROJECT_ID with your GCP project ID.

5.3 Enable App Engine and choose region


gcloud app create

• It will ask you to select a region (e.g., asia-south1 for Mumbai).

6. Deploy the Python App to App Engine


In your project folder (my-python-app), run:

gcloud app deploy

What this does:

• Uploads your code.


• Builds the image using the specified runtime.
• Deploys to App Engine Standard.
• Creates a default service (default) and version.

You’ll be asked to confirm; type Y.


7. Access the Deployed Application
After deployment, run:

gcloud app browse

This opens your app in a browser.

The URL will generally be:

[Link]

(Region code may differ.)

You should see:

Hello, App Engine! This is a simple Python app.

8. Updating the Application


If you change [Link] (e.g., update the message), redeploy:

gcloud app deploy

Each deployment creates a new version. You can:

• Set traffic split between versions.


• Roll back to a previous version from the console.

9. Basic Directory Structure


Your folder will typically look like:

my-python-app/
├── [Link]
├── [Link]
├── [Link]
└── venv/ # (optional, not deployed)
10. Common Issues and Tips
10.1 ModuleNotFoundError (Flask/gunicorn not found)

• Ensure they are in [Link].


• Reinstall locally for testing: pip install -r [Link].

10.2 Wrong runtime

• Check [Link], e.g.:

runtime: python311

Make sure App Engine supports that version.

10.3 Port issues locally

• When running locally, use:

python [Link]

Or:

gunicorn -b :8080 main:app

11. Why Use App Engine for a Simple Python App?


• No need to manage VMs, OS, or containers.
• Auto-scaling based on traffic.
• Integrated logging and monitoring.
• Easy versioning and rollback.
• Good for student demos, small APIs, websites, and microservices.

You might also like