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.
Asked
Active
Viewed 1.6k times
16
-
Have you checked [this](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-rds.html) – Jun 10 '20 at 02:16
-
looks promising. so this requires injecting RDS params as environment variables while creating EC2/ECS task .am i correct? – uman dev Jun 10 '20 at 04:20
-
Very precisely yes. Do you require something else too? – Jun 10 '20 at 04:29
3 Answers
12
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);
}
}
}

smac2020
- 9,637
- 4
- 24
- 38
-
Thanks for the hint! I was getting crazy because of not finding the classers in the libraries I was using... Had to do with me using version 1 libraries. – raspayu Nov 29 '20 at 06:31
-
1Amazon is strongly encouraging all AWS Java users use V2. V1 is no longer maintained. as stated in this Readme: https://github.com/awsdocs/aws-doc-sdk-examples. – smac2020 Dec 18 '20 at 23:52
-
I ran this program in my local. i have configured ~/.aws/credentials. Getting error : The security token included in the request is expired, when I run this program in my local. – Raghu Jul 03 '23 at 10:08
8
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);

F_SO_K
- 13,640
- 5
- 54
- 83
4
I think GitHub may be of help. I searched for SsmClient getParameter language:java and some of the results seem promising.
public static String getDiscordToken(SsmClient ssmClient) {
GetParameterRequest request = GetParameterRequest.builder().
name("/discord/token").
withDecryption(Boolean.TRUE).
build();
GetParameterResponse response = ssmClient.getParameter(request);
return response.parameter().value();
}

stefansundin
- 2,826
- 1
- 20
- 28
-
thanks. i believe the new SDK has deprecated all these classes. i get class not found. error: package software.amazon.awscdk.services.ssm does not exist – uman dev Jun 10 '20 at 04:15
-
I don't think the CDK is what you want to use. That's the AWS Cloud Development Kit and it related to creating infrastructure. You want to use the SDK, and the class should be under com.amazonaws.services.simplesystemsmanagement. – stefansundin Jun 10 '20 at 19:47
-
You want to use V2 APIs - not V1. The V2 client is software.amazon.awssdk.services.ssm.SsmClient; – smac2020 Sep 10 '20 at 18:49