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:
Clone a Ktor documentation repository and open the codeSnippets project.
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.getenvor use the default 5000 value in a case an environment variable is not specified. Open theApplication.ktfile placed in thesrc/main/kotlin/com/examplefolder and change theportparameter value of theembeddedServerfunction 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.conffile, you can assign the environment variable to theportparameter by using the${ENV}syntax. Open theapplication.conffile placed insrc/main/resourcesand 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:
Then, make sure that the main application class is configured:
Build a Fat JAR
To build a Fat JAR, open the terminal and execute the buildFatJar task provided by the Ktor plugin:
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:
Open the Elastic Beanstalk service in the AWS services group.
On the opened page, click Create Application.
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.
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