Over the past few days I've set up various experimental projects to explore writing AWS serverless code in Kotlin. And I haven't had much success. My goal is to be able to write idiomatic, clean Kotlin code, which will make it easy to create and deploy infrastructure and code on AWS.

AWS offers a number of paths to serverless infrastructure as code, such as AWS SAM, AWS Cloudformation and AWS CDK.

  • SAM is a command-line tool, which isn't what I'm aiming for.
  • Cloudformation is more promising, but it is driven by Yaml or JSON files and is not programmatic. It's a bit like Terraform in that regard.
  • CDK is a programmatic wrapper around Cloudformation. In its Java variant, it works, and I managed to get some test code written in Kotlin and running via gradle. The Java API is very Builder-heavy, and not at all Kotlin-like. I found a CDK DSL which provides a Kotlin DSL wrapper around CDK, but it's not available in a maven repo, hasn't been touched in three years, and I immediately found a missing function that I had been using in the CDK. An alternative, AWS-CDK-Kotlin-DSL, is a Japanese project which automatically generates a DSL from the CDK source code. Clever but I struggled to understand how to include it in my project, and it only supports the older CDK version 1, not version 2.

It does appear that there is nothing out there which meets my requirements, and to build something myself would be a massive undertaking requiring more time and skill that I can devote to it. The best, most fully featured options are Kotless for a pure web/HTTP-REST server, or the CDK, for the complete AWS Cloudformation stack.

I did a search on Github - there are only seven projects tagged "cloudformation" written in Kotlin, none updated since 2020, and only the DSL mentioned above is remotely feature complete. Perhaps what I want to do just isn't what others want. I know that most people would recommend using Javascript or Typescript - there are plenty of libraries and frameworks out there - but to me, that's another think to learn, another language, and one I don't like nearly as much as Kotlin.