Speedup AWS CDK Updates

A great feature of the AWS CDK (inherited from AWS CloudFormation) is the ability to update a stack in place. This is accomplished by only sending changes across to CloudFormation (the changes can be viewed by calling diff instead of deploy).

Unfortunately, a number of artifacts may register as having changed and be re-deployed, even though the only difference between the artifact might be a timestamp on a file. Or the order of the metadata may have changed.

If you are using the Java CDK bindings, many Construct builders take java.util.Map instances that contain configuration information. Unfortunately the java.util.HashMap does not retain the order of items added, nor does it guarantee an stability of that order across executions (or JDK versions). java.util.LinkedHashMap does retain the order, so when creating Constructs that expect a Map of configuration values, remember to use LinkedHashMap.

That said, java.util.Map#of(...) is a very convenient convenience function for creating inline Map instances without all the boilerplate of Map<String,Object> config = new LinkedHashMap(); //etc, but Map#of returns a HashMap instance.

Map<String,Object> config = Map.of( 
  "key1," "value1",
  "key2," "value2"
);

The project clusterless-commons provides a OrderedMaps#of(...) method that duplicates the Map#of method, but returns a LinkedHashMap instead.

Map<String,Object> config = OrderedMaps.of( 
  "key1," "value1",
  "key2," "value2"
);

Additionally, the sister class OrderedSafeMaps provides an #of(...) method that will drop a key if the associated value is null.

Map<String,Object> config = OrderedSafeMaps.of( 
  "key1," null, // ignored
  "key2," OrderedSafeMaps.ifPresent(value2, String::toLowerCase), // mutate if not null
  "key3," OrderedSafeMaps.ifPresent(value3, String::toLowerCase, defaultValue),
);

You can read more about these classes here.

Links:

Chris K Wensel
Chris K Wensel
Data and Analytics Architect