Skip to content

Commit ee767ee

Browse files
authored
fix: disable idempotency doesn't disable dynamodb client creation in persistent store (#796)
* do not create DynamoDbClient when idempotency is disabled * ignoring case for idempotency disable env
1 parent eae5fff commit ee767ee

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotentAspect.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Object around(ProceedingJoinPoint pjp,
4747
Idempotent idempotent) throws Throwable {
4848

4949
String idempotencyDisabledEnv = System.getenv().get(Constants.IDEMPOTENCY_DISABLED_ENV);
50-
if (idempotencyDisabledEnv != null && !idempotencyDisabledEnv.equals("false")) {
50+
if (idempotencyDisabledEnv != null && !idempotencyDisabledEnv.equalsIgnoreCase("false")) {
5151
return pjp.proceed(pjp.getArgs());
5252
}
5353

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/DynamoDBPersistenceStore.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ private DynamoDBPersistenceStore(String tableName,
7878
if (client != null) {
7979
this.dynamoDbClient = client;
8080
} else {
81-
DynamoDbClientBuilder ddbBuilder = DynamoDbClient.builder()
82-
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
83-
.httpClient(UrlConnectionHttpClient.builder().build())
84-
.region(Region.of(System.getenv(AWS_REGION_ENV)));
85-
this.dynamoDbClient = ddbBuilder.build();
81+
String idempotencyDisabledEnv = System.getenv().get(Constants.IDEMPOTENCY_DISABLED_ENV);
82+
if (idempotencyDisabledEnv == null || idempotencyDisabledEnv.equalsIgnoreCase("false")) {
83+
DynamoDbClientBuilder ddbBuilder = DynamoDbClient.builder()
84+
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
85+
.httpClient(UrlConnectionHttpClient.builder().build())
86+
.region(Region.of(System.getenv(AWS_REGION_ENV)));
87+
this.dynamoDbClient = ddbBuilder.build();
88+
} else {
89+
// we do not want to create a DynamoDbClient if idempotency is disabled
90+
// null is ok as idempotency won't be called
91+
this.dynamoDbClient = null;
92+
}
8693
}
8794
}
8895

powertools-idempotency/src/test/java/software/amazon/lambda/powertools/idempotency/persistence/DynamoDBPersistenceStoreTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.junit.jupiter.api.AfterEach;
1717
import org.junit.jupiter.api.BeforeEach;
1818
import org.junit.jupiter.api.Test;
19+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
1920
import software.amazon.awssdk.services.dynamodb.model.*;
21+
import software.amazon.lambda.powertools.idempotency.Constants;
2022
import software.amazon.lambda.powertools.idempotency.DynamoDBConfig;
2123
import software.amazon.lambda.powertools.idempotency.IdempotencyConfig;
2224
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyItemAlreadyExistsException;
@@ -260,6 +262,13 @@ public void endToEndWithCustomAttrNamesAndSortKey() throws IdempotencyItemNotFou
260262
}
261263
}
262264

265+
@Test
266+
@SetEnvironmentVariable(key = Constants.IDEMPOTENCY_DISABLED_ENV, value = "true")
267+
public void idempotencyDisabled_noClientShouldBeCreated() {
268+
DynamoDBPersistenceStore store = DynamoDBPersistenceStore.builder().withTableName(TABLE_NAME).build();
269+
assertThatThrownBy(() -> store.getRecord("fake")).isInstanceOf(NullPointerException.class);
270+
}
271+
263272
@BeforeEach
264273
public void setup() {
265274
dynamoDBPersistenceStore = DynamoDBPersistenceStore.builder()

0 commit comments

Comments
 (0)