Getting Started with Google App Engine

    Posted by AppScale Staff on 5/15/18 1:36 PM

     In 2008, Google introduced App Engine, one of the first serverless computing services. Google App Engine, or GAE for short, lets you deploy applications directly to Google Cloud, bypassing the need to provision or manage servers yourself. You simply upload your code and GAE dynamically handles the allocation of infrastructure resources for you. Since then, Google has continued to add more serverless offerings that improve time-to-market and reduce infrastructure and ops costs.

    1-85317316

    Timeline of Google's serverless products

    This post is the first in a two-part series on deploying applications using App Engine and AppScale. Like GAE, AppScale is a serverless platform that makes application development and deployment incredibly easy. The unique difference with AppScale is that it’s open source, enabling you to combine the easy-to-use development model of App Engine with the flexibility to deploy your app to any cloud platform, including Google Compute Engine, Microsoft Azure, AWS, Alibaba Cloud, or any other infrastructure, including on-prem. This post demonstrates how you can deploy Java, PHP, and Python code to a standard App Engine environment using the Eclipse IDE and Google Cloud SDK. GAE standard supports Java, Python, Go and PHP applications. In the next post, we will compare and contrast using GAE vs. AppScale.

    Before you begin, create a Google Cloud Platform account.

    Deploying a Java App

    We'll demonstrate deploying a Java app using Eclipse, an open source Java IDE that is popular with enterprise users. Google provides a plugin that lets you initialize, deploy, and manage App Engine projects from within Eclipse. Before starting, make sure you install the Eclipse IDE for Java EE Developers (version 4.6.2 or higher).

    Step 1: Create a New App Engine Project

    We'll start by creating a new App Engine project. After logging into your Google Cloud account, go to the Google Cloud Platform Console and open the projects drop-down on the top bar. Click the Create Project button. In the New Project screen, set the name of the project, an optional ID, and an optional folder to categorize the project under. When you're done, click Create.

    2

    Step 2: Configure Eclipse

    The Cloud Tools for Eclipse plugin is the easiest way to deploy applications from Eclipse to GCP. After opening Eclipse, select Help > Eclipse Marketplace… and search for Google Cloud. Find Google Cloud Tools for Eclipse, then click Install. Alternatively, drag and drop the install button shown on the Quickstart page to your Eclipse workspace.

    3

    Restart Eclipse when prompted. You can verify that the plugin is installed correctly by clicking Window > Preferences and selecting Google Cloud Tools in the sidebar. This is also where you can update the Google Cloud SDK that the plugin uses, configure Cloud Dataflow, and set other options. For now, you can simply close the Preferences window.

    Step 3: Create a New Eclipse Project

    Click File > New > Other and select Google App Engine Standard Java Project. Enter a name for the project. You can leave the other fields as their defaults (this article provides more information on what each field does). Click Finish to create the project.

    4
    This creates a new Java Servlet project that displays "Hello App Engine!" in the browser. You can test the project on your local machine by clicking the Run button in the toolbar.

    Step 4: Deploy Your Project to App Engine

    After your project is created, right-click the project in the Package Explorer and select Deploy to App Engine Standard. In the Account box, select Sign into another account. This opens a browser page prompting you to grant Cloud Tools for Eclipse access to your Google Cloud account. Once you've authorized the plugin, return to Eclipse and select your account from the drop-down.

    The project you created in the Google Cloud Platform Console appears in the Project list. Select the project. You can specify any additional deployment options such as the application version, whether to divert all traffic to this version, and whether to stop the previous version. For now, leave the defaults as they are.

    At the moment, we have a Google Cloud Platform project, but no App Engine instance to run it in. This causes Eclipse to prompt you to create an App Engine application in the Google Cloud Console, displayed as a link just below the Project list. Click on the link to open the Google Cloud Console in your browser. Select the region that you want to host your application in and click Select. Once the instance is done initializing, return to Eclipse, click the refresh button next to the Project list, then select your project. Verify that the link disappears, then click Deploy.

    5

    The status of the deployment appears in the Eclipse Console. Once the deployment is finished, a browser window opens and displays the output of your Servlet.

    6

     

    Deploying a PHP App

    Next, we'll deploy a PHP application using the Google Cloud SDK, a command-line based tool for managing the entire Google Cloud suite. You can find an example of a ready-to-deploy PHP app in the How to Run Hello World with PHP documentation page.

    Before you begin, download and install git, Composer, and the Google Cloud SDK. Alternatively, you can use the Google Cloud Shell to deploy your code directly from your browser. You should also create a new GCP project as shown in step 1 of the Java instructions.

     Step 1: Initialize the Google Cloud SDK

     After installing the Google Cloud SDK, initialize the SDK by running google-cloud-sdk/bin/gcloud init. This prompts you to give the SDK access to your Google Cloud account as well as set a project as the current project. Select the project you just created. This causes commands executed in the SDK to modify your new project instead of any other projects in your Google Cloud account.

     Step 2: Configure Your PHP App

     Each App Engine project requires an app.yaml file, which specifies the behavior of your app in App Engine. For example, an app.yaml file for a PHP application contains routing information, sets the environment's PHP version, or a list of files that shouldn't be uploaded to App Engine. At a minimum, your app.yaml file should define the runtime and at least one handler for incoming requests. The app.yaml file is stored in the base directory of your PHP app, and you can find a sample app.yaml file in Google's example PHP application.

    # Runs the app using PHP 5.5.
    runtime: php55
    
    handlers:
    # Routes requests to index script.
    - url: /
      Script: index.php
    
    # Routes other *.php requests to the appropriate script file.
    - url: /(.+\.php)$
      script: \1
    

     Step 3: Deploy Your App to App Engine

    Once you've configured your app.yaml file, you can deploy your app to App Engine. From your project directory, run gcloud app deploy. The SDK will package, deploy, and start your application. Once the deployment is complete, you can view your app by opening the URL shown in the console, or by running gcloud app browse.

    Deploying a Python App

    Deploying a Python app is almost identical to deploying a PHP app, with minor changes to your app.yaml file. As with PHP, we'll use the Google Cloud SDK to deploy our Python code and access it using the browser.

    Before continuing, install and initialize the Google Cloud SDK as shown in step 2 of the PHP instructions.

    Step 1: Configure Your Python App

     As with a PHP app, Python App Engine projects are configured using an app.yaml file. This file defines the version of Python to run the app in, as well as handlers for routing requests to script files. Each handler's script: directive can either point to a Python script file or a Python module.

    runtime: python27
    api_version: 1
    threadsafe: true
    
    handlers:
    # Route all requests to main.app
    - url: /.*
      script: main.app
    

    The Cloud SDK includes a local development server for testing Python apps. The server initializes your project and hosts it locally on port 8080. From your project directory,

    run dev_appserver.py app.yaml
    and open your browser to http://localhost:8080.

     

     Step 2: Deploy Your App to App Engine

    Before deploying, make sure the current App Engine project is set to your Python project. You can switch your current project in the SDK using:

    gcloud config set project <project name>
    When you're ready to deploy, run:
    gcloud app deploy
    Once your project is deployed, you can access it via web browser using the URL provided, or by running:
    gcloud app browse

    Next Steps

     Now that you know how to deploy to App Engine, you can start customizing your projects. Try adding new functionality, communicating with other projects, and interacting with the Google Cloud Platform programmatically using the Google API Client Libraries. You can learn more about running Java, PHP, and Python applications in App Engine in each of their respective documentation pages.

     In the next post, we'll show how to take your project beyond GCP using AppScale. AppScale lets you deploy your code to any infrastructure including Google Compute Engine, Azure, AWS, Alibaba, or on-premises. If you'd like to know more about AppScale, contact us for more information.

     

    Subscribe to Email Updates

    Most Popular

    Recent Posts