Deploying and running on App Engine Standard

This section will walks you through creating and deploying a very simple App Engine app on your GCP. Google App Engine helps you create applications out of the box, you simply code up the program that you want to run on GCP, use the right SDKs and the right libraries to deploy it, and you are good to go. There are no clusters to maintain and no VM instances to worry about. Here, we will create a simple Python application, test it locally, and then deploy it to App Engine and see it accept external traffic. Throughout this tutorial we shall be using Cloud Shell for convenience:

  1. Use the gcloud command-line tool to set up the default value for the zone and region that you want to use. If you are using your local machine to connect to the cloud, you don't need to keep setting these config properties. But if you are using Cloud Shell, as shown here, because these instances are ephemeral and might change once your session is disconnected and you reconnect, you might have to set these properties afresh for each session.

 

  1. Typically, you would perform app development on your local machine and simply deploy it to the server. Here, we will create the app on the cloud shell itself. We create a new directory on the Cloud Shell to hold the code for our app.
  2. Move into this directory and then use an editor to create your Python file that holds your application. All the code in your application will be present in this one file. Here, it's titled my_first_app:
  1. We use the webapp package, which is simply a lightweight Python web framework that allows you to develop web apps very quickly. It's compatible with Google App Engine and is an easy way for you to get up and running with your website.
  2. Set up a class named Homepage, which is derived from webapp2.RequestHandler and, within that, the get method holds the content of your web page. The response property of this class contains the response that we will send down to the client browser.
  3. Set the content type header to be text/html, indicating that it is an HTML response. Write out a simple hello world response message in HTML. Set up a Web Server Gateway Interface (WSGI) application that receives a request and directs it to the appropriate page. Here, we only handle the /, which is sent to the home page. We have written our very first Python application on the App Engine.

 

  1. Save the file. Exit from the editor and ensure my_first_app is present in your local directory
  2. App Engine settings are specified in a file called app.yaml. Open up an app.yaml file in an editor and specify the configuration for your first App Engine project. The API version is 1. The app.yaml files contain the handlers that contain the mapping from URL paths to static files and the request handlers. Here, we want the URL to map to my_first_app.app, which is the Python module that we just created. Save this file. Now we are ready to test this instance of our site:
  1. Kick-start the deployment instance of the application server by calling dev_appserver.py ./ Point to where the Python module is, which is in the current directory for us.
  2. Our application is now running in your development mode and you can preview this by clicking in the top-left corner of your cloud shell and choosing preview on port 8080. Any changes that you make to your App Engine will be immediately available without you having to restart your deployment server:
  1. What we saw so far is just the preview or development mode. Now, let's deploy this to production. This is done by using the gcloud command: gcloud app deploy app.yaml. This will ask you to choose the region where you want your app to be deployed. If your customers happen to be in Asia, choose the Asia region. Once this command runs through, we are done. You can view your application in the URL that is displayed on the command line in the output from the command you just ran. Copy this URL and view it in a browser to see your first App Engine app. If you want to see logs of your site, simply tail your logs using the commands that you can see on the screen gcloud app logs tail -s default:

We have deployed our App Engine project to production.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset