I am trying to correctly write an aws lambda using Java that will use aws sdk SqsClient and SnsClient. I see that these clients implement close()
method, and it is generally a good practice to call this method when client is no longer required. And the best practices for lambda (https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html) advices to
Initialize SDK clients and database connections outside of the function handle
So, my question is: is aws lambda wise enough to close aws sdk clients automatically, so that I can write something like this, and do not worry about closing the client explicitly when lambda container is closed.
public class SQSProcessingHandler implements RequestHandler<String, String> {
private SqsClient sqsClient = SqsClient.create();
@Override
public String handleRequest(final String event, final Context context) {
ListQueuesResponse response = sqsClient.listQueues();
return response.queueUrls().stream().collect(Collectors.joining(","));
}
}
If explicit close is still required, could you please help me to find out how can I know that the lambda container is about to close, so that I should call close()
on the client?