Serverless application with AWS Lambda and Kotlin. Part 4

Volodymyr Sulevskyi
Coinmonks
5 min readAug 26, 2018

--

Part 4 — Using familiar tools: Writing functions in Kotlin for for Java platform using Spring Cloud Functions on AWS Lambda

This series of articles consists of 4 parts:

  1. Intro to Serverless applications and Functions-as-a-Service
  2. First blood: Writing functions in Kotlin for Java platform on AWS Lambda
  3. Warmup optimization: Writing functions in Kotlin for Node.js platform on AWS Lambda
  4. Using familiar tools: Writing functions in Kotlin for for Java platform using Spring Cloud Functions on AWS Lambda (you are here)

Purpose of this part of series is to show how to create a lambda function on AWS Lambda based on Java platform with Kotlin programming language and Spring Framework.

All source code can be downloaded from Github

Spring Framework is a well-known tool for most Java/Kotlin developers. It simplifies application development providing number of instruments for dependency injection, aspect-oriented programming, auto-configuration, etc. Further development of Spring Framework resulted in more specific frameworks: Spring MVC is web framework, Spring Batch is a framework for building a pipeline for batch processing and many others.

Spring Cloud Function is a framework developing under Spring umbrella. Framework helps developing FaaS application with Spring features such as dependency injection, AOP, auto-configuration.

Spring has great support of Kotlin, and in recent version partially written in Kotlin.

In our third example we will build an application with the same functionality as in previous examples (receive data from API Gateway and store to S3), but now with usage of Spring Cloud Function.

  1. Setup the project

Setting-up the project is very similar to an example where we’ve built an application for Java platform. In this example we will build the fat jar with dependencies included. Spring dependencies managed with spring-boot-gradle-plugin — we need to include it to gradle build script. Additionally we need to add a Spring Cloud Function dependency. Spring has adapters for three popular FaaS providers AWS Lambda, Azure and OpenWhisk. So we will just add a dependency org.springframework.cloud:spring-cloud-function-adapter-aws:1.0.0.RELEASE to out build script.

To support Spring auto configuration we will modify shadowJar task for building fat jar.

With this build config all power of Spring’s auto configuration is now available for us!

2. Creating function

Function creation consists of:

  • creating a bean of type Function, Consumer or Supplier
  • bootstrapping Spring Boot application

3. Configuring AWS Lambda

Deployment to AWS is also done with Gradle

As you can see we’ve configured an entry point which corresponds to Spring configuration.

And we’ve configured more RAM for a function.

4. Testing on AWS

Now we can deploy and test our function.

To deploy run command gradle deployFunction (you can see configuration on Github).

On AWS console go to Lambda configuration and configure new test event, use API Gateway AWS proxy (if you want full component configuration: API Gateway and S3 — please see second article in this series).

If you trigger an event you’ll get the output.

We’ve set up Spring application on AWS Lambda written with Kotlin.

But what about startup time? This is still an issue for Java platform and Spring Cloud Functions. We’ve configured 1024MB of RAM, if we set 512MB of RAM as we did in previous examples — application won’t even start for 20 seconds.

So Spring Cloud Function requires more RAM and CPU comparing to regular Java or Node.js functions. But if you can tolerate higher warmup time and/or additional computing resources required and you want to use great Spring ecosystem Spring Cloud Function is a great solution.

We can see that out application has some lower memory and CPU bound. If we set memory to 256 MB or less application will fail to start and throw OutOfMemoryError. But subsequent invocations take much less time and suitable for most web applications.

Invocation time depends on provided resources

Conclusion:

During this series of articles we’ve made a journey of building serverless application with Kotlin. We started with Java platform and saw that Kotlin is fully suitable for usage in serverless applications. Later we’ve improved performance of the app with compiling Kotlin code to JavaScript and running function on Node.js platform. And in the last example we’ve used Spring Cloud Function as a great tool for software development. Each of these solutions has pros and cons. But if you are a Java/Kotlin developer and you want to use serverless platform you can justify your choice based on your requirements and described solutions.

References:

Get Best Software Deals Directly In Your Inbox

--

--