Ktor 1.6.7 Help

Google App Engine

In this tutorial, we'll show you how to prepare and deploy a Ktor project to a Google App Engine standard environment. This tutorial uses a Ktor application created in the Adding Ktor to an existing Gradle project topic.

Prerequisites

Before starting this tutorial, you need to perform the steps below:

  • Register in Google Cloud Platform.

  • Install and initialize the Google Cloud SDK.

  • Install the App Engine extension for Java with the following command:

    gcloud components install app-engine-java

Clone a sample application

To open a sample application, follow the steps below:

  1. Clone the ktor-gradle-sample project.

  2. Switch a branch from main to one of the following:

    git checkout embedded-server # a server is configured in code # or git checkout engine-main # a server is configured in 'application.conf'

    These branches demonstrate different approaches to creating and configuring a Ktor server: in code or by using the application.conf configuration file. In this tutorial, deploying process is the same for both approaches.

Prepare an application

Step 1: Apply the Shadow plugin

This tutorial shows how to deploy the application to Google App Engine using a fat JAR. To generate fat JARs, you need to apply the Shadow plugin. Open the build.gradle file and add the plugin to the plugins block:

plugins { id 'com.github.johnrengelman.shadow' version '7.0.0' }

Step 2: Configure the App Engine plugin

The Google App Engine Gradle plugin provides tasks to build and deploy Google App Engine applications. To use this plugin, follow the steps below:

  1. Open the settings.gradle file and insert the following code to reference a plugin from the Central Maven repository:

    pluginManagement { repositories { gradlePluginPortal() mavenCentral() } resolutionStrategy { eachPlugin { if (requested.id.id.startsWith('com.google.cloud.tools.appengine')) { useModule("com.google.cloud.tools:appengine-gradle-plugin:${requested.version}") } } } }
  2. Open build.gradle and apply the plugin in the plugins block:

    plugins { id 'com.google.cloud.tools.appengine' version '2.4.2' }
  3. Add the appengine block with the following settings in the build.gradle file:

    appengine { stage { artifact = "build/libs/${project.name}-${project.version}-all.jar" } deploy { version = "GCLOUD_CONFIG" projectId = "GCLOUD_CONFIG" } }

Step 3: Configure App Engine settings

You configure App Engine settings for your application in the app.yaml file:

  1. Create the appengine directory inside src/main/appengine.

  2. Inside this directory, create the app.yaml file and add the following content:

    runtime: java11 entrypoint: 'java -jar google-appengine-standard-1.0-SNAPSHOT-all.jar'

    The entrypoint option contains a command used to run a fat JAR generated for the application.

Deploy an application

To deploy the application, open the terminal and follow the steps below:

  1. First, create a Google Cloud project, which is a top-level container holding application resources. For example, the command below creates a project with the ktor-sample-app-engine name:

    gcloud projects create ktor-sample-app-engine --set-as-default
  2. Create an App Engine application for the Cloud project:

    gcloud app create
  3. To deploy the application, execute the appengineDeploy Gradle task...

    ./gradlew appengineDeploy

    ... and wait until Google Cloud builds and publishes the application:

    ...done. Deployed service [default] to [https://ktor-sample-app-engine.ew.r.appspot.com]

You can find the completed example here: google-appengine-standard.

Last modified: 16 August 2021