By using AWS re:Post, you agree to the Terms of Use
/Initializing Kotlin libraries in lambda/

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() }
asked 19 days ago5 views
1 Answers
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

SUPPORT ENGINEER
answered 13 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions