Skip to content

AWS S3 bucket as a source of your configuration

Eugene Strokin edited this page Apr 30, 2021 · 2 revisions

If you run your application in AWS environment you could manage your configuration properties by adding or changing files into an S3 Bucket.

AWS S3 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-s3</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,s3

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

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

public class MyApplication {

  static {
    SourcesFactory.instance.registerSourceAlias("s3", new S3PropertiesSource());
  }

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

Bucket name and polling interval

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

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

java -Ddynocon.sources=env,sys,s3 -Ddynocon.s3.bucket=my_config_bucket -Ddynocon.s3.delay=15 -jar my-app.jar

Or envirement variables:

dynocon.sources=env,sys,s3
dynocon.s3.bucket=my_config_bucket
dynocon.s3.delay=15
java -jar my-app.jar

Files in the bucket

The name of a file in the bucket without extension will be your property's name. The content of the file will be the value of the property. The value could be JSON if the property is not primitive.

File: myPropertyName.json:

my property value

Java example:

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

... SNIP ...

Assert.assertEquals("value1", MY_PROPERTY.get());

File: myComplexProperty.json:

{
  "prop1": "value1",
  "prop2": 123
}
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("my property value", MY_COMPLEX_PROPERTY.get().getProp1());
Assert.assertEquals(123, MY_COMPLEX_PROPERTY.get().getProp2());

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