Invoke AWS Lambda from a Kotlin Client

Upasana | August 12, 2019 | 2 min read | 208 views | AWS Tutorials


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. AWS Lambda in Kotlin using Spring Cloud Function
  3. Java AWS Lambda using Spring Cloud Function
  4. S3 File upload & download with AWS Java SDK v2
  5. Feign Client Logging and connection timeout
  6. How does Session handling works in Servlet environment
  7. Slack Webhook Message from Spring Boot

Recommended books for interview preparation:

Find more on this topic: