2021-05-13 01:16:52 +00:00
When initializing an SDK client or database connection outside of the Lambda function, you optimize your chances to benefit from context reuse, when the same container is reused for multiple function invocations.
2021-05-04 01:15:14 +00:00
2021-05-09 01:17:04 +00:00
This rule reports an issue when the SDK client or the database connection is initialized locally inside a Lambda function.
2021-05-04 01:15:14 +00:00
== Noncompliant Code Example
----
public class App {
public void doSomething() {
S3Client s3Client = S3Client.builder().region(region).build();
s3Client.listBuckets();
// ...
}
}
----
== Compliant Solution
----
public class App {
private final S3Client s3Client;
public App() {
s3Client = DependencyFactory.s3Client();
}
public void doSomething() {
s3Client.listBuckets();
// ...
}
}
----
== See
* https://aws.amazon.com/fr/blogs/developer/tuning-the-aws-java-sdk-2-x-to-reduce-startup-time/[Tuning the AWS Java SDK 2.x to reduce startup time]
* https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html[Best practices for working with AWS Lambda functions]
* https://aws.amazon.com/fr/blogs/compute/container-reuse-in-lambda/[Understanding Container Reuse in AWS Lambda]