2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-05-04 01:15:14 +00:00
If the region is not specified when creating a new AwsClient with an https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/awscore/client/builder/AwsClientBuilder.html[AwsClientBuilder], the AWS SDK will execute some logic to identify the endpoint automatically.
2022-06-24 15:40:34 +02:00
While it will probably identify the correct one, this extra logic will slow down startup time, already known to be a hotspot.
2021-05-04 01:15:14 +00:00
2022-06-24 15:40:34 +02:00
You should therefore always define the logic to set the region yourself. This is typically done by retrieving the region from the Lambda provided AWS_REGION environment variable.
2021-05-04 01:15:14 +00:00
This will make the code more explicit and spare initialization time.
This rule reports an issue when the region is not set when creating an AwsClient.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-05-04 01:15:14 +00:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-05-04 01:15:14 +00:00
----
S3Client.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-05-04 01:15:14 +00:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-05-04 01:15:14 +00:00
----
S3Client.builder()
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable()))
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();
----
2023-05-03 11:06:20 +02:00
== Resources
2021-05-04 01:15:14 +00:00
* 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/sdk-for-java/latest/developer-guide/lambda-optimize-starttime.html[Optimizing cold start performance for AWS Lambda]
* https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html[Environment variable configuration]
2021-05-05 08:56:28 +00:00
* https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-region-selection.html#automatically-determine-the-aws-region-from-the-environment[Automatically Determine the AWS Region]
2021-05-04 01:15:14 +00:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Set the region explicitly on this builder.
=== Highlighting
AwsClient builder.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== is related to: S6242
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]