In this post I will talk about creating and running a Kotlin Ktor project on an Amazon Web Service. For the creation of the project I will be using JetBrains IntelliJ IDEA. Here is the GitHub repository of the project: https://github.com/ilya-shevtsov/AWS-Elastic-Beanstalk-Ktor-Sample-Project
Setting up a project
First thing you have to do is create a project and setup the Gradle File.
To make a project click "File" - "New" - "Project".
The settings are:
Language – Kotlin
Build system – Gradle
Gradle DSL – Kotlin Should look something like this:
Setting up build.gradle.kts
Now you have to setup your Gradle File. Firstly add this code to the repositories block:
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") }
It should look like this:
Add the following code to the dependencies block:
implementation("io.ktor:ktor-server-core:2.0.3")
implementation("io.ktor:ktor-server-netty:2.0.3")
implementation("ch.qos.logback:logback-classic:1.2.1")
It should look like this:
Add this line to your plugins block:
id("com.github.johnrengelman.shadow") version "7.1.2"
It should look like this:
Last thing you need to do is to replace the mainClass.set in the application block to the following:
mainClass.set("io.ktor.server.netty.EngineMain")
It should look like this:
Adding "application.conf" file
You also need to create a "application.conf" file in the "resources" folder of your project. The file should contain the following code:
ktor {
deployment {
port = 5000
port = ${?PORT}
}
application {
modules = [ ApplicationKt.module ]
}
}
Making a Ktor server
Now you have to setup the server itself. For the propose of demonstration I will be using the bare minimum of the code you need to get it working.
First thing you need to do is to make a "Application.kt" file and past the following code into it:
fun main(args: Array<String>): Unit = EngineMain.main(args)
fun Application.module() {
routing {
get("/haha"){
val response = "keke"
call.respond(response)
}
}
}
Here the "/haha" - is the path that will be added to the url and response - is the thing that the server will output of the server.
Creating a FatJar file
Now you have to make a FatJar file. This is your project that will be uploaded to the AWS. You need to go to the terminal and type the following code:
./gradlew shadowJar
The file should appear in the project-name\build\libs folder.
Uploading the project to AWS Beanstalk Services
Now you have to upload your project of the FatJar file to the AWS Beanstalk services.
To do so, firstly you have to make an AWS account and then do the following steps.
Create an application
Type "Elastic Beanstalk into the search bar:
Click "Create Application":
Name your application:
Setup the platform settings. For this project they should be such:
Now click the "Upload your code" box and choose the FatJar file that we have created before:
Click create and wait for it for finish creating the environment (it might take a while). If it takes more than 10 minutes, then click “Services” then “Elastic Beanstalk” if the environment was created the page should look something like this:
URL of the server and server status
The server page looks like this:
The “Health” might be indicated as "Severe", but don't worry the server works. Now click the environment name, here you can see the causes of the "Health" status, update your project and most important here is where the URL of your server is shown. It’s the link at the top that starts with the name of your project.
You can copy the URL, add the path (/haha in our case) and you will get the response. And that is it, now you have a working Ktor project on the AWS Elastic Beanstalk service that you can use as a REST API.
Comments