AWS Systems Manager Parameter Store: Using StringList as Key Value Pairs in Java (Lambda)

橙三吉。 提交于 2021-02-10 13:16:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!