Services >> Cloud >> GCP >> Services >> App Engine >> How to deploy a sample Python application to App Engine.

 

(1) From your machine with Google Cloud SDK installed and configured


## Create the project to host your app

c:\home\bin\gcloud>gcloud projects create myfaqbase-gae-002 --name=myfaqbase-gae-002
Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/myfaqbase-gae-002].
Waiting for [operations/cp.8891267884033889765] to finish...done.
Enabling service [cloudapis.googleapis.com] on project [myfaqbase-gae-002]...
Operation "operations/acat.p2-216586345359-4068de65-259c-4077-978c-dbdb69248da8" finished successfully.



## Set this new project as your active project

c:\home\bin\gcloud>gcloud config set project myfaqbase-gae-002
Updated property [core/project].


## Confirm

c:\home\bin\gcloud>gcloud config list project
[core]
project = myfaqbase-gae-002

Your active configuration is: [default]



## Find out your billing account using the gcloud beta billing command, if prompted to install gcloud beta commands, accept it
## The actual billing account_id have been masked out in the output below

c:\home\bin\gcloud>gcloud beta billing accounts list
ACCOUNT_ID            NAME                                 OPEN  MASTER_ACCOUNT_ID
XXXXXX-XXXXXX-XXXXXX  Billing Account for XXXXXXXXXXXXXXX  True


## Link the newly created project to that billing account, use the ACCOUNT_ID value from the above command output

c:\home\bin\gcloud>gcloud beta billing projects link myfaqbase-gae-002 --billing-account=XXXXXX-XXXXXX-XXXXXX
billingAccountName: billingAccounts/XXXXXX-XXXXXX-XXXXXX
billingEnabled: true
name: projects/myfaqbase-gae-002/billingInfo
projectId: myfaqbase-gae-002



## Enable the App Engine and Cloud Build APIs

c:\home\bin\gcloud>gcloud services enable appengine.googleapis.com
Operation "operations/acat.p2-216586345359-abd45d94-b57e-49ac-956d-18642456ef63" finished successfully.

c:\home\bin\gcloud>gcloud services enable cloudbuild.googleapis.com
Operation "operations/acf.p2-216586345359-1bb174ab-88aa-42a1-a122-555c0ee57078" finished successfully.



## Create a project directory locally to host your code(s), any directory name to suit your needs

c:\home\bin\gcloud>mkdir myfaqbase-gae-002

c:\home\bin\gcloud>cd myfaqbase-gae-002


## Copy the sample Python code from Gcloud Storage bucket

c:\home\bin\gcloud\myfaqbase-gae-002>gsutil -m cp -r gs://spls/gsp067/python-docs-samples .
Copying gs://spls/gsp067/python-docs-samples/appengine/standard_python3/hello_world/main.py...
Copying gs://spls/gsp067/python-docs-samples/appengine/standard_python3/hello_world/requirements-test.txt...
Copying gs://spls/gsp067/python-docs-samples/appengine/standard_python3/hello_world/requirements.txt...
Copying gs://spls/gsp067/python-docs-samples/appengine/standard_python3/hello_world/main_test.py...
Copying gs://spls/gsp067/python-docs-samples/appengine/standard_python3/hello_world/app.yaml...
- [5/6 files][  2.0 KiB/  2.0 KiB]  99% Done



## Change to the directory containing app.yaml

c:\home\bin\gcloud\myfaqbase-gae-002>cd python-docs-samples/appengine/standard_python3/hello_world


## List the contents for your info

c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world>dir
 Volume in drive C has no label.
 Volume Serial Number is 5A42-6539

 Directory of c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world

04/02/2022  11:27 am    <DIR>          .
04/02/2022  11:27 am    <DIR>          ..
04/02/2022  11:27 am                18 app.yaml
04/02/2022  11:27 am             1,178 main.py
04/02/2022  11:27 am               801 main_test.py
04/02/2022  11:27 am                14 requirements-test.txt
04/02/2022  11:27 am                13 requirements.txt
               5 File(s)          2,024 bytes
               2 Dir(s)  269,245,865,984 bytes free


## View contents of app.yaml & main.py

c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world>type app.yaml
runtime: python37

c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world>type main.py
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gae_python38_app]
from flask import Flask


# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)


@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    return 'Hello World!'


if __name__ == '__main__':
    # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. This
    # can be configured by adding an `entrypoint` to app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)
# [END gae_python38_app]


## Create the app engine app for this project and select the region to hosts the app engine resources

c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world>gcloud app create --proj
ect=myfaqbase-gae-002 --region=asia-southeast1

You are creating an app for project [myfaqbase-gae-002].
WARNING: Creating an App Engine application for a project is irreversible and the region
cannot be changed. More information about regions is at
<https://cloud.google.com/appengine/docs/locations>.

Creating App Engine application in project [myfaqbase-gae-002] and region [asia-southeast1]....done.
Success! The app is now created. Please use `gcloud app deploy` to deploy your first app.



## Deploy the app, type Y when prompted to continue

c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world>gcloud app deploy
Services to deploy:

descriptor:                  [c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world\app.yaml]
source:                      [c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world]
target project:              [myfaqbase-gae-002]
target service:              [default]
target version:              [20220204t113142]
target url:                  [https://myfaqbase-gae-002.as.r.appspot.com]
target service account:      [App Engine default service account]


Do you want to continue (Y/n)?
  Y

Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
#============================================================#
#= Uploading 5 files to Google Cloud Storage                =#
#============================================================#
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://myfaqbase-gae-002.as.r.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse



## Browse your app

c:\home\bin\gcloud\myfaqbase-gae-002\python-docs-samples\appengine\standard_python3\hello_world>gcloud app browse
Opening [https://myfaqbase-gae-002.as.r.appspot.com] in a new tab in your default browser.





 

 

Clean up


## If you want to clean up, disable the app in Cloud console



Google Cloud Console -> App Engine -> Settings -> Click "Disable application"





## And delete/shutdown the project

c:\home\bin\gcloud>gcloud projects delete myfaqbase-gae-002
Your project will be deleted.

Do you want to continue (Y/n)?
  Y