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.
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 the ktor-gradle-sample project.
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 usingSystem.getenv
or use the default 5000 value in a case an environment variable is not specified. Open theApplication.kt
file placed in thesrc/main/kotlin/com/example
folder and change theport
parameter value of theembeddedServer
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 theapplication.conf
file, you can assign the environment variable to theport
parameter by using the${ENV}
syntax. Open theapplication.conf
file placed insrc/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:
Then, add the shadowJar
task:
Build a Fat JAR
To build a Fat JAR, open the terminal and execute the shadowJar
task created in this step:
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:
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