Invoke AWS Lambda from a Kotlin Client

Carvia Tech | May 05, 2019 | 2 min read | 25 views


In this tutorial, we will invoke an existing lambda function from a Java Client with aws-java-sdk. This approach can be used to invoke a lambda from another lambda or a java based microservice running as full fledged server app.

We have already created a lambda in previous article, we will invoke the same using Java SDK. Refer to Creating a aws lambda using Spring Cloud Function

Creating a Gradle based project

Lets create a simple gradle based project for our implementation.

build.gradle
plugins {
	id 'org.springframework.boot' version '2.1.4.RELEASE'
	id 'org.jetbrains.kotlin.jvm' version '1.3.31'
	id 'org.jetbrains.kotlin.plugin.spring' version '1.3.31'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.shunya'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
	implementation 'org.jetbrains.kotlin:kotlin-reflect'
	implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

	// https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-lambda
	implementation group: 'com.amazonaws', name: 'aws-java-sdk-lambda', version: '1.11.546'

	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

compileKotlin {
	kotlinOptions {
		freeCompilerArgs = ['-Xjsr305=strict']
		jvmTarget = '1.8'
	}
}

compileTestKotlin {
	kotlinOptions {
		freeCompilerArgs = ['-Xjsr305=strict']
		jvmTarget = '1.8'
	}
}

Lets define the domain models:

Domain models
data class LambdaInput(
        val input: Long?
)

data class LambdaOutput(
        val result: Long? = null
)

Now let’s define a service interface for our lambda function. This service defines the contract for input and output of lambda function.

NumberService.kt
import com.amazonaws.services.lambda.invoke.LambdaFunction

interface MathService {

    @LambdaFunction(functionName = "number-reversal-java")
    fun reverse(input: LambdaInput): LambdaOutput
}

Finally, we will create a client that invokes the lambda using domain models and interface defined above:

LambdaClient.kt
import com.amazonaws.services.lambda.AWSLambdaClientBuilder
import com.amazonaws.services.lambda.invoke.LambdaInvokerFactory
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.stereotype.Component

@SpringBootApplication
class ClientApp

fun main(args: Array<String>) {
	runApplication<ClientApp>(*args)
}

@Component
class LambdaClient : ApplicationRunner {
	override fun run(args: ApplicationArguments?) {
		val uppercaseService = LambdaInvokerFactory.builder()
				.lambdaClient(AWSLambdaClientBuilder.defaultClient())
				.build(MathService::class.java)

		val request = LambdaInput(12345)
		println("Invoking aws lambda")
		val response = uppercaseService.reverse(request)
		println("Lambda Response = ${response.result}")
	}

}
Program output
lambda response = 54321

That’s all.

Grab the source code

You can download the source code for this article from Github:


Top articles in this category:
  1. Invoking AWS Lambda from a Java Client
  2. Creating AWS Lambda in Kotlin using Spring Cloud Function
  3. Creating AWS Lambda in Java using Spring Cloud Function
  4. Self Signed Certificate in Spring 5 WebClient
  5. What is new in Spring Boot 2
  6. Prevent Lost Updates in Database Transaction using Spring Hibernate
  7. Spring DI - Singleton beans with prototype-bean dependencies



Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info