Skip to content

Commit d15f988

Browse files
Have Lambda example cache data keys in a static field.
1 parent f4973ec commit d15f988

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/examples/java/com/amazonaws/crypto/examples/datakeycaching/LambdaDecryptAndWriteExample.java

+45-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,46 @@
4040
public class LambdaDecryptAndWriteExample implements RequestHandler<KinesisEvent, Void> {
4141
private static final long MAX_ENTRY_AGE_MILLISECONDS = 600000;
4242
private static final int MAX_CACHE_ENTRIES = 100;
43+
44+
// For best caching performance in Lambda, we want our cache to be a static final field
45+
// configured by environment variables.
46+
// However, to make this example easier for people to experiment with, we also provide a non-static
47+
// version with simpler configuration.
48+
private static final CachingCryptoMaterialsManager CACHING_CRYPTO_MATERIALS_MANAGER;
49+
private static final String TABLE_NAME = System.getProperty("TABLE_NAME");
50+
51+
static {
52+
final String cmkArn = System.getProperty("CMK_ARN");
53+
CACHING_CRYPTO_MATERIALS_MANAGER = CachingCryptoMaterialsManager.newBuilder()
54+
.withKeyring(StandardKeyrings.awsKms(AwsKmsCmkId.fromString(cmkArn)))
55+
.withCache(new LocalCryptoMaterialsCache(MAX_CACHE_ENTRIES))
56+
.withMaxAge(MAX_ENTRY_AGE_MILLISECONDS, TimeUnit.MILLISECONDS)
57+
.build();
58+
}
59+
4360
private final CachingCryptoMaterialsManager cachingMaterialsManager_;
4461
private final AwsCrypto crypto_;
4562
private final Table table_;
4663

64+
/**
65+
* No-argument constructor for use with Lambda.
66+
*
67+
* This is almost equivalent to calling {@link #LambdaDecryptAndWriteExample(String, String)} with
68+
* {@code cmkArn = System.getProperty("CMK_ARN")}
69+
* and
70+
* {@code tableName = System.getProperty("TABLE_NAME")}
71+
* respectively.
72+
* The only difference is that this constructor will re-use the underlying cache across all instances
73+
* for better cache performance.
74+
*
75+
* @see #LambdaDecryptAndWriteExample(String, String)
76+
* @see #CACHING_CRYPTO_MATERIALS_MANAGER
77+
* @see #TABLE_NAME
78+
*/
79+
public LambdaDecryptAndWriteExample() {
80+
this(CACHING_CRYPTO_MATERIALS_MANAGER, TABLE_NAME);
81+
}
82+
4783
/**
4884
* This code doesn't set the max bytes or max message security thresholds that are enforced
4985
* only on data keys used for encryption.
@@ -52,15 +88,21 @@ public class LambdaDecryptAndWriteExample implements RequestHandler<KinesisEvent
5288
* @param tableName The name of the DynamoDB table name that stores decrypted messages
5389
*/
5490
public LambdaDecryptAndWriteExample(final String cmkArn, final String tableName) {
55-
cachingMaterialsManager_ = CachingCryptoMaterialsManager.newBuilder()
91+
this(
92+
CachingCryptoMaterialsManager.newBuilder()
5693
.withKeyring(StandardKeyrings.awsKms(AwsKmsCmkId.fromString(cmkArn)))
5794
.withCache(new LocalCryptoMaterialsCache(MAX_CACHE_ENTRIES))
5895
.withMaxAge(MAX_ENTRY_AGE_MILLISECONDS, TimeUnit.MILLISECONDS)
59-
.build();
96+
.build(),
97+
tableName);
98+
}
99+
100+
public LambdaDecryptAndWriteExample(CachingCryptoMaterialsManager cachingMatherialsManager, String tableName) {
101+
cachingMaterialsManager_ = cachingMatherialsManager;
60102
crypto_ = new AwsCrypto();
61103
table_ = new DynamoDB(AmazonDynamoDBClientBuilder.defaultClient()).getTable(tableName);
62104
}
63-
105+
64106
/**
65107
* Decrypts Kinesis events and writes the data to DynamoDB
66108
*

0 commit comments

Comments
 (0)