Pilih preferensi cookie Anda

Kami menggunakan cookie penting serta alat serupa yang diperlukan untuk menyediakan situs dan layanan. Kami menggunakan cookie performa untuk mengumpulkan statistik anonim sehingga kami dapat memahami cara pelanggan menggunakan situs dan melakukan perbaikan. Cookie penting tidak dapat dinonaktifkan, tetapi Anda dapat mengklik “Kustom” atau “Tolak” untuk menolak cookie performa.

Jika Anda setuju, AWS dan pihak ketiga yang disetujui juga akan menggunakan cookie untuk menyediakan fitur situs yang berguna, mengingat preferensi Anda, dan menampilkan konten yang relevan, termasuk iklan yang relevan. Untuk menerima atau menolak semua cookie yang tidak penting, klik “Terima” atau “Tolak”. Untuk membuat pilihan yang lebih detail, klik “Kustomisasi”.

Contoh Lambda menggunakan SDK untuk Kotlin

Mode fokus
Contoh Lambda menggunakan SDK untuk Kotlin - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK untuk Kotlin dengan Lambda.

Dasar-dasar adalah contoh kode yang menunjukkan kepada Anda bagaimana melakukan operasi penting dalam suatu layanan.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Skenario adalah contoh kode yang menunjukkan kepada Anda bagaimana menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan atau dikombinasikan dengan yang lain Layanan AWS.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Hal-hal mendasar

Contoh kode berikut ini menunjukkan cara:

  • Buat peran IAM dan fungsi Lambda, lalu unggah kode handler.

  • Panggil fungsi dengan satu parameter dan dapatkan hasil.

  • Perbarui kode fungsi dan konfigurasikan dengan variabel lingkungan.

  • Panggil fungsi dengan parameter baru dan dapatkan hasil. Tampilkan log eksekusi yang dikembalikan.

  • Buat daftar fungsi untuk akun Anda, lalu bersihkan sumber daya.

Untuk informasi selengkapnya, lihat Membuat fungsi Lambda dengan konsol.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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.Java17 } // Create a Lambda function using a waiter LambdaClient { region = "us-east-1" }.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-east-1" }.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-east-1" }.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-east-1" }.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-east-1" }.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.Java17 } LambdaClient { region = "us-east-1" }.use { awsLambda -> awsLambda.updateFunctionConfiguration(configurationRequest) } } suspend fun delFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-east-1" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }

Contoh kode berikut ini menunjukkan cara:

  • Buat peran IAM dan fungsi Lambda, lalu unggah kode handler.

  • Panggil fungsi dengan satu parameter dan dapatkan hasil.

  • Perbarui kode fungsi dan konfigurasikan dengan variabel lingkungan.

  • Panggil fungsi dengan parameter baru dan dapatkan hasil. Tampilkan log eksekusi yang dikembalikan.

  • Buat daftar fungsi untuk akun Anda, lalu bersihkan sumber daya.

Untuk informasi selengkapnya, lihat Membuat fungsi Lambda dengan konsol.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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.Java17 } // Create a Lambda function using a waiter LambdaClient { region = "us-east-1" }.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-east-1" }.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-east-1" }.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-east-1" }.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-east-1" }.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.Java17 } LambdaClient { region = "us-east-1" }.use { awsLambda -> awsLambda.updateFunctionConfiguration(configurationRequest) } } suspend fun delFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-east-1" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }

Tindakan

Contoh kode berikut menunjukkan cara menggunakanCreateFunction.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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.Java17 } LambdaClient { region = "us-east-1" }.use { awsLambda -> val functionResponse = awsLambda.createFunction(request) awsLambda.waitUntilFunctionActive { functionName = myFunctionName } return functionResponse.functionArn } }
  • Untuk detail API, lihat CreateFunctiondi AWS SDK untuk referensi API Kotlin.

Contoh kode berikut menunjukkan cara menggunakanCreateFunction.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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.Java17 } LambdaClient { region = "us-east-1" }.use { awsLambda -> val functionResponse = awsLambda.createFunction(request) awsLambda.waitUntilFunctionActive { functionName = myFunctionName } return functionResponse.functionArn } }
  • Untuk detail API, lihat CreateFunctiondi AWS SDK untuk referensi API Kotlin.

Contoh kode berikut menunjukkan cara menggunakanDeleteFunction.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun delLambdaFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-east-1" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }
  • Untuk detail API, lihat DeleteFunctiondi AWS SDK untuk referensi API Kotlin.

Contoh kode berikut menunjukkan cara menggunakanDeleteFunction.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun delLambdaFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-east-1" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }
  • Untuk detail API, lihat DeleteFunctiondi AWS SDK untuk referensi API Kotlin.

Contoh kode berikut menunjukkan cara menggunakanInvoke.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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}") } }
  • Untuk detail API, lihat Memanggil di AWS SDK untuk referensi API Kotlin.

Contoh kode berikut menunjukkan cara menggunakanInvoke.

SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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}") } }
  • Untuk detail API, lihat Memanggil di AWS SDK untuk referensi API Kotlin.

Skenario

Contoh kode berikut menunjukkan cara membuat aplikasi tanpa server yang memungkinkan pengguna mengelola foto menggunakan label.

SDK untuk Kotlin

Menunjukkan cara mengembangkan aplikasi manajemen aset foto yang mendeteksi label dalam gambar menggunakan Amazon Rekognition dan menyimpannya untuk pengambilan nanti.

Untuk kode sumber lengkap dan instruksi tentang cara mengatur dan menjalankan, lihat contoh lengkapnya di GitHub.

Untuk mendalami tentang asal usul contoh ini, lihat postingan di Komunitas AWS.

Layanan yang digunakan dalam contoh ini
  • API Gateway

  • DynamoDB

  • Lambda

  • Amazon Rekognition

  • Amazon S3

  • Amazon SNS

Contoh kode berikut menunjukkan cara membuat aplikasi tanpa server yang memungkinkan pengguna mengelola foto menggunakan label.

SDK untuk Kotlin

Menunjukkan cara mengembangkan aplikasi manajemen aset foto yang mendeteksi label dalam gambar menggunakan Amazon Rekognition dan menyimpannya untuk pengambilan nanti.

Untuk kode sumber lengkap dan instruksi tentang cara mengatur dan menjalankan, lihat contoh lengkapnya di GitHub.

Untuk mendalami tentang asal usul contoh ini, lihat postingan di Komunitas AWS.

Layanan yang digunakan dalam contoh ini
  • API Gateway

  • DynamoDB

  • Lambda

  • Amazon Rekognition

  • Amazon S3

  • Amazon SNS

Topik berikutnya:

MediaConvert

Topik sebelumnya:

AWS KMS

Di halaman ini

PrivasiSyarat situsPreferensi cookie
© 2025, Amazon Web Services, Inc. atau afiliasinya. Semua hak dilindungi undang-undang.