Deploy a Ktor app to Google App Engine
In this tutorial, you will learn how to prepare and deploy a Ktor project to a Google App Engine standard environment. The tutorial uses the engine-main sample project as a starting point.
Prerequisites
Before starting this tutorial, complete the steps below:
Register on 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:
Clone the Ktor documentation repository and open the
codeSnippetsproject.Open the engine-main module.
Prepare an application
Step 1: Apply the Shadow plugin
This tutorial shows how to deploy your application to Google App Engine using a fat JAR. To generate fat JARs, you must apply the Shadow plugin.
First, open your gradle/libs.versions.toml file and define the Shadow plugin version and its alias:
Then, open the build.gradle.kts file and add the plugin to the plugins block:
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:
Open the settings.gradle.kt file and use the following code to reference a plugin from the Central Maven repository:
pluginManagement { repositories { if (!providers.gradleProperty("build_snapshot_train").orNull.isNullOrEmpty()) { mavenLocal() maven("https://redirector.kotlinlang.org/maven/dev") } gradlePluginPortal() mavenCentral() maven("https://redirector.kotlinlang.org/maven/ktor-eap") } resolutionStrategy { eachPlugin { if (requested.id.id.startsWith("com.google.cloud.tools.appengine")) { useModule("com.google.cloud.tools:appengine-gradle-plugin:${requested.version}")Open the build.gradle.kts file and apply the plugin in the
pluginsblock:plugins { alias(libs.plugins.appengine) }In the same build.gradle.kts file, add the
appengineblock with the following settings:import com.google.cloud.tools.gradle.appengine.appyaml.AppEngineAppYamlExtension configure<AppEngineAppYamlExtension> { stage { setArtifact("build/libs/${project.name}-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:
Create the appengine directory inside src/main.
Inside this directory, create the app.yaml file and add the following content:
runtime: java21 entrypoint: 'java -jar google-appengine-standard-all.jar'Replace
google-appengine-standardwith your project's name.The
entrypointoption contains a command used to run a fat JAR generated for the application.
Deploy an application
To deploy your application, open a new terminal window and follow the steps below:
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-enginename:gcloud projects create ktor-sample-app-engine --set-as-defaultCreate an App Engine application for the Cloud project:
gcloud app createTo deploy the application, run the
appengineDeployGradle task:./gradlew appengineDeployWait until Google Cloud builds and publishes the application:
...done. Deployed service [default] to [https://ktor-sample-app-engine.ew.r.appspot.com]