Azure App Service
This tutorial shows how to build, configure and deploy your Ktor application to Azure App Service.
Prerequisites
Before starting this tutorial, you will need the following:
An Azure account (Free trial here).
The Azure CLI installed on your machine.
Create a sample application
Create a sample application as described in Create, open and run a new Ktor project. This example shows code and commands based on the following projects: embedded-server and engine-main.
Set up the application
Step 1: Set up the port
In Azure App Service, the environment variable PORT contains the port number open for incoming requests. Depending on how you created the app in configure a Ktor server, you'll need to update your code to read this environment variable in one of two places:
If you used the example with port configuration in code, the
PORTenvironment variable can be read withSystem.getenv()and parsed to an integer with.toIntOrNull(). Open the fileApplication.ktand change the port number as shown below:fun runBasicServer() { val port = System.getenv("PORT")?.toIntOrNull() ?: 8080 embeddedServer(Netty, port = port) { // ... }.start(wait = true) }If the server configuration is defined in the config file
application.conf, update it to read thePORTenvironment variable as in the following example:ktor { deployment { port = ${PORT:8080} } }
Step 2: Add plugins
Open the build.gradle.kts file and add the following lines to the plugins section:
The io.ktor.plugin will provide the task used to create a fat JAR and the Azure WebApp Plugin for Gradle will be used to create all the required resources in Azure with ease.
Make sure a mainClass is defined in the application section, so that there's a well-defined entry point for your fat JAR:
If you created the project with the engine-main template, the main class will look like the following:
Step 3: Configuration
If you already created a Java web app in App Service that you want to deploy to, you can skip this step.
Otherwise, add the following entries to build.gradle.kts at the end of the file so that the Azure Webapp Plugin creates one for you:
For detailed descriptions of the available configuration properties, see the Webapp Configuration documentation.
The values for
pricingTier(Service Plan) can be found for Linux and for Windows.The list of values for
regioncan be obtained with the following Azure CLI command:az account list-locations --query "[].name" --output tsvor by searching for "App Service" in the Product Availability table.
Deploy the application
To a new web app
The authentication method used by the Azure Web App Deploy plugin uses the Azure CLI. If you haven't already, log in once with az login and follow the instructions.
Finally, deploy the application by running the azureWebAppDeploy task, which is set to build the fat JAR first and then deploy:
This task will create the resource group, plan, and web app, and then deploy the fat JAR. When the deployment succeeds, you should see output like the following:
When the deployment completes, you should be able to see your new web app running at the URL shown above.
To an existing web app
If you already have an existing Java web app in Azure App Service, first build the fat JAR by executing the buildFatJar task provided by the Ktor plugin:
Then, deploy the fat JAR created earlier using the following command of the Azure CLI:
This command will upload the JAR file and restart your web app. After a while, you should see the result of the deployment: