Lambda examples using SDK for Kotlin - AWS SDK for Kotlin

Lambda examples using SDK for Kotlin

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Kotlin with Lambda.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to use CreateFunction.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun createNewFunction( myFunctionName: String, s3BucketName: String, myS3Key: String, myHandler: String, myRole: String, ): String? { val functionCode = FunctionCode { s3Bucket = s3BucketName s3Key = myS3Key } val request = CreateFunctionRequest { functionName = myFunctionName code = functionCode description = "Created by the Lambda Kotlin API" handler = myHandler role = myRole runtime = Runtime.Java8 } LambdaClient { region = "us-west-2" }.use { awsLambda -> val functionResponse = awsLambda.createFunction(request) awsLambda.waitUntilFunctionActive { functionName = myFunctionName } return functionResponse.functionArn } }
  • For API details, see CreateFunction in AWS SDK for Kotlin API reference.

The following code example shows how to use DeleteFunction.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun delLambdaFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-west-2" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }
  • For API details, see DeleteFunction in AWS SDK for Kotlin API reference.

The following code example shows how to use Invoke.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun invokeFunction(functionNameVal: String) { val json = """{"inputValue":"1000"}""" val byteArray = json.trimIndent().encodeToByteArray() val request = InvokeRequest { functionName = functionNameVal logType = LogType.Tail payload = byteArray } LambdaClient { region = "us-west-2" }.use { awsLambda -> val res = awsLambda.invoke(request) println("${res.payload?.toString(Charsets.UTF_8)}") println("The log result is ${res.logResult}") } }
  • For API details, see Invoke in AWS SDK for Kotlin API reference.

Scenarios

The following code example shows how to create a serverless application that lets users manage photos using labels.

SDK for Kotlin

Shows how to develop a photo asset management application that detects labels in images using Amazon Rekognition and stores them for later retrieval.

For complete source code and instructions on how to set up and run, see the full example on GitHub.

For a deep dive into the origin of this example see the post on AWS Community.

Services used in this example
  • API Gateway

  • DynamoDB

  • Lambda

  • Amazon Rekognition

  • Amazon S3

  • Amazon SNS

The following code example shows how to:

  • Create an IAM role and Lambda function, then upload handler code.

  • Invoke the function with a single parameter and get results.

  • Update the function code and configure with an environment variable.

  • Invoke the function with new parameters and get results. Display the returned execution log.

  • List the functions for your account, then clean up resources.

For more information, see Create a Lambda function with the console.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun main(args: Array<String>) { val usage = """ Usage: <functionName> <role> <handler> <bucketName> <updatedBucketName> <key> Where: functionName - The name of the AWS Lambda function. role - The AWS Identity and Access Management (IAM) service role that has AWS Lambda permissions. handler - The fully qualified method name (for example, example.Handler::handleRequest). bucketName - The Amazon Simple Storage Service (Amazon S3) bucket name that contains the ZIP or JAR used for the Lambda function's code. updatedBucketName - The Amazon S3 bucket name that contains the .zip or .jar used to update the Lambda function's code. key - The Amazon S3 key name that represents the .zip or .jar file (for example, LambdaHello-1.0-SNAPSHOT.jar). """ if (args.size != 6) { println(usage) exitProcess(1) } val functionName = args[0] val role = args[1] val handler = args[2] val bucketName = args[3] val updatedBucketName = args[4] val key = args[5] println("Creating a Lambda function named $functionName.") val funArn = createScFunction(functionName, bucketName, key, handler, role) println("The AWS Lambda ARN is $funArn") // Get a specific Lambda function. println("Getting the $functionName AWS Lambda function.") getFunction(functionName) // List the Lambda functions. println("Listing all AWS Lambda functions.") listFunctionsSc() // Invoke the Lambda function. println("*** Invoke the Lambda function.") invokeFunctionSc(functionName) // Update the AWS Lambda function code. println("*** Update the Lambda function code.") updateFunctionCode(functionName, updatedBucketName, key) // println("*** Invoke the function again after updating the code.") invokeFunctionSc(functionName) // Update the AWS Lambda function configuration. println("Update the run time of the function.") updateFunctionConfiguration(functionName, handler) // Delete the AWS Lambda function. println("Delete the AWS Lambda function.") delFunction(functionName) } suspend fun createScFunction( myFunctionName: String, s3BucketName: String, myS3Key: String, myHandler: String, myRole: String, ): String { val functionCode = FunctionCode { s3Bucket = s3BucketName s3Key = myS3Key } val request = CreateFunctionRequest { functionName = myFunctionName code = functionCode description = "Created by the Lambda Kotlin API" handler = myHandler role = myRole runtime = Runtime.Java8 } // Create a Lambda function using a waiter LambdaClient { region = "us-west-2" }.use { awsLambda -> val functionResponse = awsLambda.createFunction(request) awsLambda.waitUntilFunctionActive { functionName = myFunctionName } return functionResponse.functionArn.toString() } } suspend fun getFunction(functionNameVal: String) { val functionRequest = GetFunctionRequest { functionName = functionNameVal } LambdaClient { region = "us-west-2" }.use { awsLambda -> val response = awsLambda.getFunction(functionRequest) println("The runtime of this Lambda function is ${response.configuration?.runtime}") } } suspend fun listFunctionsSc() { val request = ListFunctionsRequest { maxItems = 10 } LambdaClient { region = "us-west-2" }.use { awsLambda -> val response = awsLambda.listFunctions(request) response.functions?.forEach { function -> println("The function name is ${function.functionName}") } } } suspend fun invokeFunctionSc(functionNameVal: String) { val json = """{"inputValue":"1000"}""" val byteArray = json.trimIndent().encodeToByteArray() val request = InvokeRequest { functionName = functionNameVal payload = byteArray logType = LogType.Tail } LambdaClient { region = "us-west-2" }.use { awsLambda -> val res = awsLambda.invoke(request) println("The function payload is ${res.payload?.toString(Charsets.UTF_8)}") } } suspend fun updateFunctionCode( functionNameVal: String?, bucketName: String?, key: String?, ) { val functionCodeRequest = UpdateFunctionCodeRequest { functionName = functionNameVal publish = true s3Bucket = bucketName s3Key = key } LambdaClient { region = "us-west-2" }.use { awsLambda -> val response = awsLambda.updateFunctionCode(functionCodeRequest) awsLambda.waitUntilFunctionUpdated { functionName = functionNameVal } println("The last modified value is " + response.lastModified) } } suspend fun updateFunctionConfiguration( functionNameVal: String?, handlerVal: String?, ) { val configurationRequest = UpdateFunctionConfigurationRequest { functionName = functionNameVal handler = handlerVal runtime = Runtime.Java11 } LambdaClient { region = "us-west-2" }.use { awsLambda -> awsLambda.updateFunctionConfiguration(configurationRequest) } } suspend fun delFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-west-2" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }