Skip to content

Getting Started

Vladislav Tankov edited this page Feb 8, 2020 · 17 revisions

In this short tutorial we will overview the steps of creating a Kotless-based application.

We will use Kotless DSL for demonstration purposes. Still, Ktor DSL differences will be mentioned.

Preliminaries

To use Kotless in your project you need:

  • Kotlin version 1.3.50+

To deploy application you need:

  • Administrative access to AWS account (e.g. profile in .aws/.credentials)
  • S3 bucket to store kotless-related artifacts

If you also need a non-technical name for your application:

  • Route53 DNS zone to create DNS name for the application
  • ACM certificate for DNS name at US-EAST-1 (N. Virginia)

Setting up Gradle project

First of all, you need to set up Gradle for your project. Kotless uses Gradle tasks to prepare deployment environment, generate deployment code, prepare a jar and deploy it to cloud provider.

You need to apply a plugin to project at build.gradle.kts:

id("io.kotless") version "0.1.2" apply true

To use Kotless DSL in a project you need to set up a maven repository with DSL and add the necessary dependency:

repositories {
    jcenter()
}
dependencies {
    implementation("io.kotless", "lang", "0.1.2")
    //or for Ktor
    //implementation("io.kotless", "ktor-lang", "0.1.2")
}

If you don't have AWS account -- stop here. Now you can use local task to run application locally and debug it.

If you have AWS account and want to perform real deploy -- let's set up everything for it! It's rather simple:

kotless {
    config {
        bucket = "kotless.s3.example.com"

        terraform {
            profile = "example"
            region = "us-east-1"
        }
    }

    webapp {
        //Optional parameter, by default technical name will be generated
        route53 = Route53("kotless", "example.com")

        //configuration of lambda created
        lambda {            
            //needed only for Kotless DSL
            kotless {
                //Define packages in which scan for routes should be performed
                //By default, will be set to gradle module group
                packages = setOf("io.kotless.examples")
            }
        }
    }
}

Writing dynamic route

Let's write your first dynamic route.

In Kotless terminology, "dynamic route" is an HTTP route processed by a lambda. On the contrary, "static route" is an HTTP route mapped to an S3 object.

Creating dynamic route is relatively simple. This code snippet will create a route at HTTP path /, which will print "Hello world!"

@Get("/")
fun root(): String {
    return "Hello world!"
}

Note that dynamic route function may have parameters. Parameters will be taken from the URL and body (if presented) and deserialized into the parameters of function. Moreover, the result of function will also be serialized to HTTP response automatically.

In case you need a full control of the HTTP response you may return HttpResponse object, which will be passed to ApiGateway without any changes.

For Ktor dynamic route equivalent is routing. Here is a simple example:

class Server : Kotless() {
    override fun prepare(app: Application) {
        app.routing {
            get("/") {
                call.respondText { "Hello World!" }
            }
        }
    }
}

Adding statics to your application

Most of the real-world applications contain static resources, e.g. CSS and JS files. Serving static resources with lambdas is a very wasteful spending of resources, so Kotless proposes a solution — static resources mapped directly to S3.

This code snippet will create route at HTTP path /file.css with file example.css

@StaticGet("/file.css", MimeType.CSS)
val exampleCss = File("example.css")

Note that the file will be resolved against workingDir set in Gradle configuration.

For Ktor static route equivalent is static feature in routing. Here is a simple example:

class Server : Kotless() {
    override fun prepare(app: Application) {
        app.routing {
            static {
                file("file.css", "example.css")
            }
        }
    }
}

Running application locally

You may check, that application works as you expect or debug it -- just run it locally.

Execute local task in Gradle and Kotless will start HTTP server, AWS emulation (if enabled) and cron scheduler to execute your application same as it would be in AWS.

Deploying application to AWS

Finally, you need to deploy your Kotless-based application to AWS. Check that fulfilled all the preliminary conditions and execute deploy task in Gradle.

Kotless will download Terraform, generate deployment code, pack lambda, and, at the end, deploy the application to AWS.

As soon as the deployment is completed, your application will start serving requests at the Route53 DNS name you have assigned to it.