AWS SSM parameter store not fetching all key/values

后端 未结 3 495
萌比男神i
萌比男神i 2021-01-21 00:07

Could someone let me know why the below code only fetching few entries from the parameter store ?

   GetParametersByPathRequest getParametersByPathRequest = new          


        
相关标签:
3条回答
  • 2021-01-21 00:49

    GetParametersByPath is a paged operation. After each call you must retrieve NextToken from the result object, and if it's not null and not empty you must make another call with it added to the request.

    Here's an example using DescribeParameters, which has the same behavior:

    DescribeParametersRequest request = new DescribeParametersRequest();
    DescribeParametersResult response;
    do
    {
        response = client.describeParameters(request);
        for (ParameterMetadata param : response.getParameters())
        {
            // do something with metadata
        }
        request.setNextToken(response.getNextToken());
    }
    while ((response.getNextToken() != null) && ! respose.getNextToken.isEmpty());
    
    0 讨论(0)
  • 2021-01-21 00:49
     private void getSsmParams() {
    
        AWSSimpleSystemsManagement client = AWSSimpleSystemsManagementClientBuilder.defaultClient();
        GetParametersByPathRequest request = new GetParametersByPathRequest();
        request.withRecursive(true);
        request.withPath('/your/path/parameterName').setWithDecryption(true);
    
        GetParametersByPathResult response;
    
        do {
    
          response = client.getParametersByPath(request);
    
          for (Parameter p : response.parameters()) {
            //do something with the values. maybe add to a list
          }
    
          request.setNextToken(response.getNextToken())
        }
    
        while (StringUtils.isNotBlank(response.getNextToken()));
    
      }
    

    Above piece of code worked for me .ssm only sends 10 parameters at a time, so if you want to fetch more than 10 parameters from ssm parameter store programatically you will have to use multiple calls to fetch them. here the token is important , if there are more values in the path (request.withPath('/your/path/parameterName')) you have given, it will send a token indicating that there are more values in the given path ,and you will have to make the following request with the token received from the previous request in order to get the rest of the values.

    0 讨论(0)
  • 2021-01-21 00:57

    Here is the code, based on the code above, for the new 2.0 version of AWS SSM manager. Notice I have set the maxResults to 1 to prove out the loop. You will want to remove that. AWS has mentioned that in the new code they wanted to emphasize immutability.

    Using this dependency:

    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>ssm</artifactId>
      <version>2.10.32</version>
    </dependency>
    

    I came up with this code:

     private void refreshCache() {
            StopWatch sw = StopWatch.createStarted();
            GetParametersByPathRequest request = GetParametersByPathRequest.builder()
                .path(prefix)
                .withDecryption(useDecryption)
                .maxResults(1)
                .build();
    
            GetParametersByPathResponse response;
            do {
              response = ssm.getParametersByPath(request);
              for (Parameter p : response.parameters()) {
                //do something with the values.
              }
              request = GetParametersByPathRequest.builder()
                  .path(prefix)
                  .withDecryption(useDecryption)
                  .nextToken(response.nextToken())
                  .maxResults(1)
                  .build();
            }
            while (StringUtils.isNotBlank(response.nextToken()));
            LOG.trace("Refreshed parameters in {}ms", sw.getTime());
          }
    
    0 讨论(0)
提交回复
热议问题