Initializing Kotlin libraries in lambda

0

Typically, to reduce code execution times, we'll initialize an AWS library outside of the function. In the Kotlin AWS SDK, this is done with something like EventBridgeClient.fromEnvironment(). However, this is a suspended call, so it's awkward to call in this context, and I'd have to wrap it with a runBlocking or something similar. Is this really how I'd need to initialize a service client in lambda using the Kotlin SDK?

private val eventBridge = runBlocking { EventBridgeClient.fromEnvironment() }
已提问 2 年前416 查看次数
1 回答
0

Hello,

If you’re trying to make cold starts quicker or prevent cold starts by running code in your handler’s initialization code during the provisioned concurrency boot the code looks fine.

For your handler, since all methods of the client can be suspended, you’ll need a runBlocking around the entire handler logic. The handler should be synchronous because anything that has not finished executing before the handler returns will be frozen until the Lambda is re-triggered (or may never re-run if the lambda shuts down before being retriggered since it’s never guaranteed you’ll re-invoke the same lambda execution environment).

If you peek at this link, AWS Lambda execution environment - Shutdown phase - https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html#runtimes-lifecycle-shutdown , it mentions the following:

• Background processes or callbacks that were initiated by your Lambda function and did not complete when the function ended resume if Lambda reuses the execution environment. Make sure that any background processes or callbacks in your code are complete before the code exits.

As far as any Kotlin specific questions, I’d recommend getting consults from the Kotlin SDK Team on GitHub: https://github.com/awslabs/aws-sdk-kotlin . You’ll get the information directly from the source there!

It’s currently in developer preview and not intended for production workloads, so if you’re running into issues that don’t operate with exact parity as other AWS SDKs, this could be why and we’d certainly like to hear about it by having a GitHub issue raised: https://github.com/awslabs/aws-sdk-kotlin/issues

AWS
支持工程师
Tim_P
已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则