Ktor 2.3.12 Help

AWS Elastic Beanstalk

In this tutorial, we'll show you how to prepare and deploy a Ktor application to AWS Elastic Beanstalk. You can use one of the following initial projects depending on the way used to create a Ktor server:

Prerequisites

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 a Ktor documentation repository and open the codeSnippets project.

  2. Open the embedded-server or engine-main sample. These samples demonstrate different approaches to creating and configuring a Ktor server: in code or by using a 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 sample 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 sample 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 Ktor 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 Ktor plugin. Open the build.gradle.kts file and add the plugin to the plugins block:

plugins { id("io.ktor.plugin") version "2.3.12" }

Then, make sure that the main application class is configured:

application { mainClass.set("io.ktor.server.netty.EngineMain") }

Build a Fat JAR

To build a Fat JAR, open the terminal and execute the buildFatJar task provided by the Ktor plugin:

./gradlew :aws-elastic-beanstalk:buildFatJar
gradlew.bat :aws-elastic-beanstalk:buildFatJar

When this build completes, you should see the aws-elastic-beanstalk-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: 02 April 2024