Ktor 1.6.7 Help

AWS Elastic Beanstalk

In this tutorial, we'll show you how to prepare and deploy a Ktor application to AWS Elastic Beanstalk. This tutorial uses a Ktor application created in the Adding Ktor to an existing Gradle project topic.


Before starting this tutorial, you need to create an AWS account.

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. The only difference in deploying these projects is how to specify a port used to listen for incoming requests.

Prepare an application

Step 1: Configure a port

First, you need to specify a port used to listen for incoming requests. Elastic Beanstalk forwards requests to your application on port 5000. Optionally, you can override the default port by setting the PORT environment variable. Depending on the way used to configure a Ktor server, you can configure a port in one of the following ways:

  • If you've chosen the embedded-server branch with server configuration specified in code, you can obtain the environment variable value using System.getenv or use the default 5000 value in a case an environment variable is not specified. Open the Application.kt file placed in the src/main/kotlin/com/example folder and change the port parameter value of the embeddedServer function as shown below:

    fun main() { embeddedServer(Netty, port = (System.getenv("PORT")?:"5000").toInt()) { // ... }.start(wait = true) }
  • If you've chosen the engine-main branch with server configuration specified in the application.conf file, you can assign the environment variable to the port parameter by using the ${ENV} syntax. Open the application.conf file placed in src/main/resources and update it as shown below:

    ktor { deployment { port = 5000 port = ${?PORT} } }

Step 2: Apply the Shadow plugin

This tutorial shows how to deploy the application to Elastic Beanstalk 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' }

Then, add the shadowJar task:

shadowJar { manifest { attributes 'Main-Class': 'com.example.ApplicationKt' } }

Build a Fat JAR

To build a Fat JAR, open the terminal and execute the shadowJar task created in this step:

./gradlew shadowJar
gradlew.bat shadowJar

When this build completes, you should see the ktor-gradle-sample-1.0-SNAPSHOT-all.jar file in the build/libs directory.

Deploy an application

To deploy the application, sign in to AWS Management Console and follow the steps below:

  1. Open the Elastic Beanstalk service in the AWS services group.

  2. On the opened page, click Create Application.

  3. Specify the following application settings:

    • Application name: Specify the application name (for example, Sample Ktor app).

    • Platform: Choose Java from the list.

    • Platform branch: Choose Corretto 11 running on 64bit Amazon Linux 2.

    • Application code: Select Upload your code.

    • Source code origin: Choose Local file. Then, click the Choose file button and choose the Fat JAR generated in the previous step. Wait until the file is uploaded.

  4. Click the Create application button and wait several minutes until Beanstalk creates the environment and publishes the application:

    INFO Instance deployment completed successfully. INFO Application available at Samplektorapp-env.eba-bnye2kpu.us-east-2.elasticbeanstalk.com. INFO Successfully launched environment: Samplektorapp-env
Last modified: 06 September 2021