Last active
May 30, 2019 12:27
-
-
Save davidrosenstark/f3c7b0469c771e41bea4fe0f2c3a611c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.ClientConfiguration; | |
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.env.EnvironmentPostProcessor; | |
import org.springframework.core.env.ConfigurableEnvironment; | |
import org.springframework.core.env.MapPropertySource; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* This class loads SSM parameters base on region and environment | |
* Needs to be added to spring.factories class so that it will be invoked. as follow: | |
* org.springframework.boot.env.EnvironmentPostProcessor=<full package>.SSMEnvironmentPostProcessor | |
* Add the SSM propeties to other properties already set | |
*/ | |
public class SSMEnvironmentPostProcessor implements EnvironmentPostProcessor { | |
private static final String QUOTE = "\""; | |
@Override | |
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { | |
SSMClient ssmClient = new SSMClient(DefaultAWSCredentialsProviderChain.getInstance(), System.getProperty("env" + | |
".region"), new ClientConfiguration()); | |
ssmClient.init(); | |
Map<String, Object> parameters = new HashMap<>(); | |
ssmClient.getParametersByPath("/" + System.getProperty("env"), true).entrySet().stream() | |
.forEach(entry -> parameters.put(entry.getKey(), entry.getValue())); | |
MapPropertySource mapPropertySource = new MapPropertySource("ssm", parameters); | |
environment.getPropertySources().addLast(mapPropertySource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment