<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hussain Mukadam</title>
    <description>The latest articles on DEV Community by Hussain Mukadam (@hsm59).</description>
    <link>https://dev.to/hsm59</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F40391%2Fa980a25c-adc1-4faa-8ac2-2e2964d9f410.png</url>
      <title>DEV Community: Hussain Mukadam</title>
      <link>https://dev.to/hsm59</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hsm59"/>
    <language>en</language>
    <item>
      <title>Deploying Ktor Web Service on AWS EC2 Instance</title>
      <dc:creator>Hussain Mukadam</dc:creator>
      <pubDate>Sat, 19 Jun 2021 22:00:04 +0000</pubDate>
      <link>https://dev.to/hsm59/deploying-ktor-web-service-on-aws-ec2-instance-209h</link>
      <guid>https://dev.to/hsm59/deploying-ktor-web-service-on-aws-ec2-instance-209h</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1fsps7i476wa4vkoxg1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1fsps7i476wa4vkoxg1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article basically talks about how do we go about deploying a Ktor web service on an AWS EC2 instance running Ubuntu / any linux distro. &lt;/p&gt;

&lt;p&gt;At the time of writing this article, I am using Ktor version 1.5 to build the project and the EC2 instance we are going to be deploying on will be a T2 micro instance.&lt;/p&gt;

&lt;p&gt;In order to learn more about creating a new Ktor web service you can follow this &lt;a href="https://himanshoe.com/kotlin-for-backend" rel="noopener noreferrer"&gt;series&lt;/a&gt; it's extremely well written and at the time of writing this, it's still a WIP or there are tons of good articles along with the official docs that help you get started with Ktor on the Backend. &lt;/p&gt;

&lt;p&gt;I'll assume that your Ktor web app is up and running locally, and we will start with launching our instance and getting started with the deployment process.&lt;/p&gt;

&lt;p&gt;Let's start by adding a security group to the instance if you don't already have kept it open for all IP address, this is done so that we are able to test our web service from the browser / mobile app.&lt;/p&gt;

&lt;p&gt;Login to your AWS Dashboard -&amp;gt; EC2 -&amp;gt; Security Groups section and edit the inbound rules.&lt;/p&gt;

&lt;p&gt;We will be adding two new rules here - &lt;/p&gt;

&lt;p&gt;First Rule &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type - &lt;code&gt;Custom TCP&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Protocol - &lt;code&gt;TCP&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Port Range - &lt;code&gt;8080&lt;/code&gt; - we are using &lt;code&gt;8080&lt;/code&gt; port number in this example this has to be the port number you are running our web service on.&lt;/li&gt;
&lt;li&gt;Source - &lt;code&gt;0.0.0.0/0&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Second Rule &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type - &lt;code&gt;Custom TCP&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Protocol - &lt;code&gt;TCP&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Port Range - &lt;code&gt;8080&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Source - &lt;code&gt;::/0&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's create a Jar file for our web service, we will be using the Gradle Shadow Plugin to create this, the reason for that is this plugin allows us to create a Fat Jar which is shadowed (obfuscated), a very good explanation for the same is &lt;a href="https://stackoverflow.com/a/59378158/1992823" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If we don't create a fat jar, and start our deployment process when running it on the server it will throw an error - &lt;code&gt;no main manifest attribute, in projectname.jar&lt;/code&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While you will see the Manifest file in the jar file if you extract it and check under the Manifest -&amp;gt; META-INF directory, you will notice that it doesn't have the &lt;code&gt;mainClass&lt;/code&gt; attribute, that points to the Class in our project that has the main function, in our case it will be the ApplicationKt.class, we need to make some changes in our &lt;code&gt;build.gradle&lt;/code&gt; file in order for it to be added in our Manifest and that we don't get the above error.&lt;/p&gt;

&lt;p&gt;First, we need to make sure that in our &lt;code&gt;build.gradle.kts&lt;/code&gt; file, we have the correct Class mentioned for &lt;code&gt;mainClassName&lt;/code&gt; - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

application {
    mainClassName = "com.starter-project.ApplicationKt"
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, we want Gradle to add the &lt;code&gt;mainClass&lt;/code&gt; attribute in the Manifest file when building the jar file, for this we will create a task - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

tasks.withType&amp;lt;Jar&amp;gt; {
    manifest {
        attributes["Main-Class"] = application.mainClassName
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let's add the Gradle Shadow Plugin in our &lt;code&gt;build.gradle&lt;/code&gt; file &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

plugins {
    id("com.github.johnrengelman.shadow") version "6.1.0"
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After this just run the Gradle build, this will create a Jar file in our project directory - &lt;code&gt;builds -&amp;gt; libs -&amp;gt; projectname-version-all.jar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We are going to need to copy this file onto our web server, for this we will use the following command, make sure you to keep your &lt;code&gt;pem&lt;/code&gt; file handy for this next step, since it's needed to access your instance via the terminal. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

scp -i "instance.pem" IdeaProjects/starter-project/build/libs/projectname-0.0.1.jar ubuntu@ec2-0.0.0.0.us-east-2.compute.amazonaws.com:/home/ubuntu


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command is &lt;code&gt;Secure Copy&lt;/code&gt; to securely transfer files from your machine to the server, further more you are giving it the directory of the jar file you want to transfer to your server along with the path on the server where you'd want to store it, you can also create a workspace directory on your server for storing this file, and give it that path instead. &lt;/p&gt;

&lt;p&gt;Now, let's SSH into our server for the next step -&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ssh -i "instance.pem" ubuntu@ec2-0.0.0.0.us-east-2.compute.amazonaws.com


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We need to check what's the version of Java installed on our server and make sure it aligns with the version we are running our web service on, we will install it if needed. &lt;/p&gt;

&lt;p&gt;For this we need to run the following command - &lt;br&gt;
&lt;code&gt;java -version&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will either return the version of Java installed on your server or an error that says &lt;code&gt;command not recognised&lt;/code&gt; and a suggestion to install it, it will also include the commands that you can run to install the same, make sure you have the correct version installed, in our case it is &lt;code&gt;Java 1.8&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If all goes well, after installation you will see the below response on hitting the &lt;code&gt;java -version&lt;/code&gt; command - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~20.04-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, let's run our web app on the server, make sure you're in the same directory where you had copied the jar file in the previous steps, run the below command to see if it's running on the port 8080.&lt;/p&gt;

&lt;p&gt;Also make sure there are no other processes running on the port number we want our web service to run on - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

kill -9 $(lsof -t -i:8080)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will kill all the processes that are running on the port number 8080. &lt;/p&gt;

&lt;p&gt;Now, let's run the jar file with the following command - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

java -jar projectname-0.0.1-all.jar


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If all goes well, the above command should return the following response - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

2021-06-16 19:31:42.194 [main] INFO  Application - Autoreload is disabled because the development mode is off.
2021-06-16 19:31:42.711 [main] INFO  Application - Responding at http://0.0.0.0:8080


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can now hit our Public v4 DNS with the port number our web service is running on and see the response in the browser -&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

http://ec2-0-0-0-0.us-east-2.compute.amazonaws.com:8080/


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But you will notice that as soon as we exit the shell, our web service stops working, in order for this to work even after existing the shell we need to create a service on our server instance that runs the web service even after exiting the shell or in case our server reboots.&lt;/p&gt;

&lt;p&gt;Let's create a web service, by running the below command - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo vim /etc/systemd/system/webapp.service


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Press i -&amp;gt; to start inserting &lt;br&gt;
Paste the following the instructions to our service - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

[Unit]
 Description=Ktor REST Service
 [Service]
 User=ubuntu
 # Change this to your workspace 
WorkingDirectory=/home/ubuntu/
# Path to executable
ExecStart=usr/bin/java -jar /home/ubuntu/projectname-0.0.1-all.jar
SuccessExitStatus=143 
TimeoutStopSec=10
 Restart=on-failure
 RestartSec=5
 [Install]
 WantedBy=multi-user.target 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Press :wq to save the above script.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ExecStart&lt;/code&gt; is the instruction that tells our system to run the command mentioned in it, here we can also mention the path to our bash script which runs the web service instead, which is much easier to maintain. &lt;/p&gt;

&lt;p&gt;After this we need to run the following commands to tell our system to run the service - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo systemctl daemon-reload 
sudo systemctl enable webapp.service


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After enabling the service it will return a symlink to our webapp.service file &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo systemctl start webapp
 sudo systemctl status webapp


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This should return the response that we had received earlier, when running the service - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Autoreload is disabled because the development mode is off.
Application - Responding at http://0.0.0.0:8080


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you face any issues in this, make sure the paths mentioned in the &lt;code&gt;webapp.service&lt;/code&gt; file under &lt;code&gt;ExecStart&lt;/code&gt; instruction are valid, and accessible.&lt;/p&gt;

&lt;p&gt;If all goes well you will be able to hit the Public V4 DNS URL / IP address / Domain with 8080 port number on the browser to see the response you were expecting. &lt;/p&gt;

&lt;p&gt;You can stop / restart the service by using the following commands - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo systemctl stop webapp
sudo systemctl restart webapp


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To get the service logs you can use the following commands, in case you want to further debug if there's an error - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo journalctl -f -n 1000 -u webapp


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I'm still very new to this, hence if I have failed to mention a way of doing this that was quite obvious, please ping me on &lt;a href="https://twitter.com/hsm59" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; so that I can improve. Thanks!&lt;/p&gt;

&lt;p&gt;Thanks to @trianton, who's article helped me completely understand the deployment process.&lt;/p&gt;

&lt;p&gt;Know more about me from the links below - &lt;br&gt;
&lt;a href="https://github.com/hsm59" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/users/1992823/hsm59" rel="noopener noreferrer"&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>ktor</category>
      <category>aws</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
