Skip to content

AWS DynamoDB table as a source of your configuration

Eugene Strokin edited this page Feb 3, 2021 · 4 revisions

If you run your application in AWS environment you could manage your configuration properties by adding or changing records into a DynamoDB table.

AWS DynamoDB configuration source is not in the default list of configuration sources provided by Dynocon Core, you need to add the dependecy into your project:

<dependency>
	<groupId>com.comcast</groupId>
	<artifactId>dynocon-dynamodb</artifactId>
	<version>LATEST</version>
</dependency>

Let Dynocon know which configuration source to use

Dynocon needs to be told which configuration sources to use. The comma-separated list of source names should be provided as a system or environment variable:

dynocon.sources=env,sys,dynamodb

You should register DynamoDB configuration source before you start doing anything:

import com.comcast.dynocon.SourcesFactory;
import com.comcast.dynocon.dynamodb.DynamodbPropertiesSource;

public class MyApplication {

  static {
    SourcesFactory.instance.registerSourceAlias("dynamodb", new DynamodbPropertiesSource());
  }

  public static void main(String... args) {
    // the applicattion logic
  }
}

Table name and polling interval

If you don't specify your own table name nor polling interval, Dynocon will be scanning the table with the name config every 30 seconds and updating values of the properties if any change is detected.

You could provide a custom DynamoDB table name and adjust the polling interval by setting up the system variables:

java -Ddynocon.sources=env,sys,dynamodb -Ddynocon.dynamodb.table=my_config_table -Ddynocon.dynamodb.delay=15 -jar my-app.jar

Or envirement variables:

dynocon.sources=env,sys,dynamodb
dynocon.dynamodb.table=my_config_table
dynocon.dynamodb.delay=15
java -jar my-app.jar

Table records

The table should have a record with fields named key and value which would contain a property name and the property value. The value could be JSON if the property is not primitive.

Table records:

key value
myPropertyName value1
myComplexProperty { "prop1": "Hello world", "prop2": 777 }

Java example:

public static final Property<String> MY_PROPERTY = new Property<>("myPropertyName", String.class);

... SNIP ...

Assert.assertEquals("value1", MY_PROPERTY.get());
public class MyComplexConfig {
  private String prop1;
  private Integer prop2;

... GETTERS AND SETTERS ...
}

... SNIP ...

public static final Property<MyComplexConfig> MY_COMPLEX_PROPERTY = new Property<>("myComplexProperty", MyComplexConfig.class);

... SNIP ...

Assert.assertEquals("Hello world", MY_COMPLEX_PROPERTY.get().getProp1());
Assert.assertEquals(777, MY_COMPLEX_PROPERTY.get().getProp2());

Please note, that once a property is added to the table, and the application is started, you cannot just remove the record from the table. Dynocon will be keeping the old property value, even if you remove the record. You should update the record with the default value instead of removing it.