Kotlin/Native AWS Lambda

I’m new to Kotlin/Native and I wanted to explore it as a replacement for Kotlin/JVM for AWS Lambda. Before I dig to deep I wanted to make sure that Kotlin/Native supports my use cases of networking clients and database drivers.

I have been looking at Ktor Client for networking. I see support for iOS, but not support for Linux. Does anybody know reference that point to examples? Are there any other network clients for Linux?

As for database, I haven’t found anything. I was think this could be done with C database drivers with a Kotlin/C interop. Is this a good approach or are there database drivers for Kotlin that I’m not seeing?

I would love to see the Kotlin/Native team solve these problems. I think it would make Kotlin an exciting alternative to GoLang. If I could get pointed in the correct direction, I might be able to help some.

Thanks,
Jeffrey Kleiss

2 Likes

Hey Jeffrey,

I want to preface this response by stating that Kotlin/Native is currently in beta status. Keep that in mind before you use it in production because I believe the Kotlin team is still trying to work things out with it, especially speed improvements with the standard library.

Now, on to your question. The Ktor client is built on top of Kotlin Multiplatform, and according to the release notes for Ktor 1.1.2 the client now works on Kotlin Native using libcurl in the background.

I don’t know of any Kotlin Native database drivers, but I’m sure if there’s a native library for the driver in question you can just do interop with it assuming you point the Kotlin Native compiler at the right header files and native libraries.

Kotlin Native should be pretty easy to use and has a lot of nice features like the memScoped block, which will automatically clean up any dynamic memory you allocate within it at the end of the block. I think once it matures more it will be a great alternative to golang, especially once the Kotlin Native coroutines API gets fully stable.

Hope that helps!

I would also point out that developing in Kotlin Native may not be as free as using it on the JVM or Javascript. There are free versions of IntelliJ that will let you build and debug. If you want to debug Kotlin Native, the only tool for doing so is CLion, which has no free version.

1 Like

Since Amazon announced custom runtimes it is possible to use K/N for AWS Lambda.

Take a look at an example AWS Lambda authored in K/N here. To understand how custom runtimes work, take a look at this tutorial.

As of March 2019 using K/N for Lambdas has, IMHO, more cons than cons:

  • The interaction between custom runtime and your code is done via REST API that is way less convenient than using language SDKs.
  • You cannot use other AWS APIs via SDKs, only via REST APU
  • Lack of libraries in general: you’ll need to use cinterop literally for every library you want to use!

Right now it seems that Golang is a better alternative for low-footprint Lambdas.

Answering your questions:

Does anybody know reference that point to examples?

See the example above.

Are there any other network clients for Linux?

As of 1.1.2 Ktor supports cURL. The example uses this API.

this could be done with C database drivers with a Kotlin/C interop.

AFAIK, that’s the only option for now.

I’ve wrote an article about simple K/N AWS Lambda function here.

1 Like

Lambda supports multiple languages through the utilization of runtimes. For a function defined as a container image, you select a runtime and therefore the Linux distribution once you create the container image. to vary the runtime, you create a replacement container image.

When you use a .zip file archive for the deployment package, you select a runtime once you create the function. to vary the runtime, you’ll update your function’s configuration. The runtime is paired with one among the Amazon Linux distributions. The underlying execution environment provides additional libraries and environment variables that you simply can access from your function code. More to know about code join CETPA

Amazon Linux

Image – amzn-ami-hvm-2018.03.0.20181129-x86_64-gp2

Linux kernel – 4.14.171-105.231.amzn1.x86_64

Amazon Linux 2

Image – Custom

Linux kernel – 4.14.165-102.205.amzn2.x86_64

When your function is invoked, Lambda attempts to re-use the execution environment from a previous invocation if one is out there . this protects time preparing the execution environment, and it allows you to save lots of resources like database connections and temporary files within the execution environment to avoid creating them whenever your function runs.

A runtime can support one version of a language, multiple versions of a language, or multiple languages. Runtimes specific to a language or framework version are deprecated when the version reaches end of life.