AppConfig helps store configuration safely in the cloud. The service also supports continuous fetching of changes so they can be applied during runtime.
Spring Cloud AWS adds support for loading configuration from AppConfig through the Spring Boot config import feature.
Maven coordinates, using Spring Cloud AWS BOM:
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-app-config</artifactId>
</dependency>To fetch configuration from AppConfig and add it to Spring’s environment properties, add the spring.config.import property to application.properties:
When specifying the key, all of the following values must be set:
-
ApplicationName or ApplicationIdentifier
-
ConfigurationProfileName or ConfigurationProfileIdentifier
-
EnvironmentName or EnvironmentIdentifier
Spring Cloud AWS AppConfig supports the following file types:
-
YAML
-
Plain text (properties)
-
JSON
For example, assuming that the ApplicationName in AppConfig is cool-spring-application, ConfigurationProfileName is ProductionConfig, and EnvironmentName is prod:
spring.config.import=aws-app-config:cool-spring-application#ProductionConfig#prodIf a config with the given values does not exist in AppConfig, the application will fail to start. If the config file is not required for the application and it should continue to start even when the file is missing, add optional before the prefix:
spring.config.import=optional:aws-app-config:cool-spring-application#ProductionConfig#prodTo load multiple configurations, separate their names with ;:
spring.config.import=aws-app-config:cool-spring-application#ProductionConfig#prod;new-spring-application#ProdConfig#prod2If some config files are required and others are optional, list them as separate entries in the spring.config.import property:
spring.config.import[0]=optional:aws-app-config:new-spring-application#ProdConfig#prod
spring.config.import[1]=aws-app-config:cool-spring-application#ProductionConfig#prod2Fetched config file properties can be referenced with @Value, bound to @ConfigurationProperties classes, or referenced in the application.properties file.
@Value("${username}")
private String username;
@Value("${password}")
private String password;The starter automatically configures and registers a AppConfigDataClient bean in the Spring application context. The AppConfigDataClient bean can be used to retrieve configuration files imperatively.
To use a custom AppConfigDataClient in spring.config.import, provide an implementation of BootstrapRegistryInitializer. For example:
package com.app;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistryInitializer;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.appconfigdata.AppConfigDataClient;
public class AppConfigBootstrapConfiguration implements BootstrapRegistryInitializer {
@Override
public void initialize(BootstrapRegistry registry) {
registry.register(AppConfigDataClient.class, context -> {
AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create("yourAccessKey", "yourSecretKey"));
return AppConfigDataClient.builder().credentialsProvider(awsCredentialsProvider).region(Region.EU_WEST_2).build();
});
}
}Note that this class must be listed under the org.springframework.boot.BootstrapRegistryInitializer key in META-INF/spring.factories:
org.springframework.boot.BootstrapRegistryInitializer=com.app.AppConfigBootstrapConfigurationIf you want to use autoconfigured AppConfigDataClient but change underlying SDKClient or ClientOverrideConfiguration you will need to register bean of type AppConfigClientCustomizer:
Autoconfiguration will configure AppConfigDataClient Bean with provided values after that, for example:
package com.app;
import io.awspring.cloud.autoconfigure.config.appconfig.AppConfigClientCustomizer;
import java.time.Duration;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistryInitializer;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
class AppConfigBootstrapConfiguration implements BootstrapRegistryInitializer {
@Override
public void initialize(BootstrapRegistry registry) {
registry.register(AppConfigClientCustomizer.class, context -> (builder -> {
builder.overrideConfiguration(builder.overrideConfiguration().copy(c -> {
c.apiCallTimeout(Duration.ofMillis(2001));
}));
}));
}
}The Spring Boot Starter for AppConfig provides the following configuration options:
Name |
Description |
Required |
Default value |
|
Enables the AppConfig integration. |
No |
|
|
Configures the endpoint used by |
No |
|
|
Configures the region used by |
No |
|
|
Configures the separator used to split the import key into application, profile, and environment. |
No |
|
The following IAM permissions are required by Spring Cloud AWS:
Start session |
|
Get configuration |
|
Sample IAM policy granting access to AppConfig:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"appconfig:GetLatestConfiguration",
"appconfig:StartConfigurationSession"
],
"Resource": "yourARN"
}
]
}