问题
Im using Api Gateway and AWS Lambda and AWS RDS to build an API. My Lambda Function Code is Java. Currently im using the AWS Systems Manager Parameter Store successfully to connect to my Database. Therefore I created a parameter called "connection" which has the type String and holds my complete connection url. In Lambda functions i can access this parameter successfully this way:
GetParameterRequest parameterRequest = new GetParameterRequest().withName("connection").withWithDecryption(false);
AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder.defaultClient();
GetParameterResult parameterResult = ssmclient.getParameter(parameterRequest);
String url = parameterResult.getParameter().getValue();
Now my question: In other lambda functions I want to send mails. Therefor I want to save the SMTP-Server and the username and the password and a default sender mail and so on.
Would it be possible to save this information as Type StringList like Key/Value Pairs (Map)? That something like this could be possible:
//Get StringList
GetParameterRequest parameterRequest = new GetParameterRequest().withName("mailInfo").withWithDecryption(false);
AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder.defaultClient();
GetParameterResult parameterResult = ssmclient.getParameter(parameterRequest);
//Get values from the list
String smtp_server = parameterResult.getParameter("smtp").getValue();
String to_mail = parameterResult.getParameter("defaultToMail").getValue();
...
Thanks in advance.
回答1:
Fundamentally SSM parameters are always strings (docs). Whether they're just a string, or an encrypted string, or a "list" which is really a string where you've agreed to use a comma as a separator for items.
Luckily strings are incredibly flexible and the trick will be to marshal your data to/from a string representation.
Probably the most obvious is to use the SSM StringList
type and require that the string list is ordered, for example mailInfo = smpt,username,password,defaultToMail
. At which point you can do your own marshalling:
GetParameterRequest parameterRequest = new GetParameterRequest().withName("mailInfo").withWithDecryption(false);
AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder.defaultClient();
GetParameterResult parameterResult = ssmclient.getParameter(parameterRequest);
String mailInfo = parameterResult.getParameter().getValue();
String[] params = mailInfo.split(",");
String stmp = params[0];
String username = params[1];
String password = params[2];
String defaultToMail = params[3];
Marshalling a StringList is probably prefered against other options (for example serialising a class and saving the result), as it's user-editable in the interface. You might, however, want to extend the format to be explicit about the ordering, e.g. smtp=smtp_value,username=username_value...
and then split each list item by =
and assign accordingly.
来源:https://stackoverflow.com/questions/53044174/aws-systems-manager-parameter-store-using-stringlist-as-key-value-pairs-in-java