Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looking for a sample code to read parameter value from aws parameter store

looking for a sample java code to read parameter store values like RDS connection string from aws parameter store. appreicate code or any reference links. thanks.

like image 554
uman dev Avatar asked Jun 10 '20 02:06

uman dev


People also ask

How do you find the value of parameter store?

To read a value from the Systems Manager parameter store at synthesis time, use the valueFromLookup method (Python: value_from_lookup ). This method returns the actual value of the parameter as a Runtime context value. If the value is not already cached in cdk.

How do I get AWS parameter store Arn?

You can locate the Amazon Resource Name (ARN) of the default key in the AWS KMS console on the AWS managed keys page. The default key is the one identified with aws/ssm in the Alias column.

Where are SSM parameters stored?

We can store these parameters in SSM, as encrypted secure strings, under a common path: /app/production/db/{DB_NAME, DB_USERNAME, DB_PASSWORD, DB_HOST} . Naturally, different environments will get different paths — with testing, staging etc.


2 Answers

Here is the V2 (not V1) example to read a specific parameter value from the AWS parameter store:

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ssm.SsmClient;
import software.amazon.awssdk.services.ssm.model.GetParameterRequest;
import software.amazon.awssdk.services.ssm.model.GetParameterResponse;
import software.amazon.awssdk.services.ssm.model.SsmException;

public class GetParameter {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    GetParameter <paraName>\n\n" +
                "Where:\n" +
                "    paraName - the name of the parameter\n";

        if (args.length < 1) {
            System.out.println(USAGE);
            System.exit(1);
         }

        // Get args
        String paraName = args[0];

        Region region = Region.US_EAST_1;
        SsmClient ssmClient = SsmClient.builder()
                .region(region)
                .build();

        try {
            GetParameterRequest parameterRequest = GetParameterRequest.builder()
                .name(paraName)
                .build();

            GetParameterResponse parameterResponse = ssmClient.getParameter(parameterRequest);
            System.out.println("The parameter value is "+parameterResponse.parameter().value());

        } catch (SsmException e) {
        System.err.println(e.getMessage());
        System.exit(1);
        }
   }
}
like image 59
smac2020 Avatar answered Sep 27 '22 16:09

smac2020


import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder;
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersRequest;
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersResult;

...

private static AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder
            .standard().withRegion(System.getProperty("SystemsManagerRegion")).build();

...

GetParametersRequest paramRequest = new GetParametersRequest()
                .withNames(parameterName).withWithDecryption(encrypted);
        GetParametersResult paramResult = new GetParametersResult();
        paramResult = ssmclient.getParameters(paramRequest);
like image 40
F_SO_K Avatar answered Sep 27 '22 16:09

F_SO_K