3

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.

mjd
  • 109
  • 1
  • 1
  • 11

2 Answers2

6

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.

thomasmichaelwallace
  • 7,926
  • 1
  • 27
  • 33
  • 1
    Ok I get it. But wouldn't it be better to save the data as org.json.JSONObject then? This way I wouldn't have a problem when my data contains a ','... – mjd Oct 29 '18 at 11:53
  • Well- as I mentioned, you can de/serialise it to/from a string however you want. The only advantage to using a comma separated list is that the SSM web console understands that StringList is a thing, so it is easier to edit in the AWS SSM Console, rather than using json &c which will just appear as string of json. – thomasmichaelwallace Oct 29 '18 at 11:56
  • n.b. for the comma-in-value problem specifically, CSV is a proper format and there are java serialisers for it that will handle the use of quotes &c to escape internal commas as per the spec. – thomasmichaelwallace Oct 29 '18 at 12:02
0

Checkout GetParametersByPath in java sdk
[doco:] (https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/ssm/SsmClient.html#getParametersByPath(software.amazon.awssdk.services.ssm.model.GetParametersByPathRequest))

They are specifically designed for grouping together sets of data.

Gogu CelMare
  • 699
  • 6
  • 15