Skip to content

Instantly share code, notes, and snippets.

@duttonw
Created May 29, 2019 00:32
Show Gist options
  • Save duttonw/21ee35af523f3d072f1e68237c5ca9b3 to your computer and use it in GitHub Desktop.
Save duttonw/21ee35af523f3d072f1e68237c5ca9b3 to your computer and use it in GitHub Desktop.
SSM Parameter Store as properties for Lambda
public class Config {
private SsmParamUtil ssmParamUtil;
public void init() {
//read environment and setup property collector
ssmParamUtil = new SsmParamUtil(prefix, AWSSimpleSystemsManagementClientBuilder.defaultClient());
}
public String getValue(String key) {
if(ssmParamUtil != null) {
String ssmValue = (String)ssmParamUtil.getProperty(key);
if (!StringUtils.isNullOrEmpty(ssmValue)){
return ssmValue;
}
}
return null;
}
}
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathRequest;
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathResult;
import com.amazonaws.services.simplesystemsmanagement.model.Parameter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class SsmParamUtil {
private String context;
private Map<String, Object> properties = new LinkedHashMap<>();
private AWSSimpleSystemsManagement ssmClient;
public SsmParamUtil(String context, AWSSimpleSystemsManagement ssmClient) {
this.context = context;
this.ssmClient = ssmClient;
}
public void init() {
GetParametersByPathRequest paramsRequest = new GetParametersByPathRequest()
.withPath(context).withRecursive(true).withWithDecryption(true);
getParameters(paramsRequest);
}
public String[] getPropertyNames() {
Set<String> strings = properties.keySet();
return strings.toArray(new String[strings.size()]);
}
public Object getProperty(String name) {
return properties.get(name);
}
private void getParameters(GetParametersByPathRequest paramsRequest) {
GetParametersByPathResult paramsResult = ssmClient
.getParametersByPath(paramsRequest);
for (Parameter parameter : paramsResult.getParameters()) {
String key = parameter.getName().replace(context, "").replace('/', '.');
properties.put(key, parameter.getValue());
}
if (paramsResult.getNextToken() != null) {
getParameters(paramsRequest.withNextToken(paramsResult.getNextToken()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment