Custom manual retries of AWS SDK calls Medium

Calling the AWS SDK APIs multiple times is inefficient. Improve the efficiency of your code by using the AWS SDK, which supports both automatic handling and customization of retries.

Detector ID
java/aws-custom-retries@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1public InvokeResult invokeLambdaNoncompliant() {
2    AWSLambda awsLambdaClient = AWSLambdaClientBuilder.standard().build();
3    final InvokeRequest request = new InvokeRequest();
4    // Noncompliant: manual retry if a service exception is thrown.
5    for(int i=0; i <= 5; i++) {
6        try {
7            return awsLambdaClient.invoke(request);
8        } catch (AmazonServiceException e) {
9            log.error("Exception: " + e);
10        }
11    }
12    return null;
13}

Compliant example

1public InvokeResult invokeLambdaCompliant() {
2    // Compliant: uses retry policy.
3    ClientConfiguration clientConfiguration = new ClientConfiguration()
4            .withRetryPolicy(RetryPolicy.builder()
5                    .withMaxErrorRetry(5)
6                    .build());
7    AWSLambda awsLambdaClient = AWSLambdaClientBuilder.standard()
8            .withClientConfiguration(clientConfiguration)
9            .build();
10    final InvokeRequest request = new InvokeRequest();
11    return awsLambdaClient.invoke(request);
12}