Created
January 31, 2022 15:20
-
-
Save jazzyF/fd6475e6a53f0fcff186705f05ff8943 to your computer and use it in GitHub Desktop.
Access AWS Parameter store variable (Secure String)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| SPDX-License-Identifier: Apache-2.0 | |
| */ | |
| package com.example.ssm; | |
| 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; | |
| /** | |
| * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials. | |
| * | |
| * For information, see this documentation topic: | |
| * | |
| * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | |
| */ | |
| public class GetParameter { | |
| public static void main(String[] args) { | |
| // final String USAGE = "\n" + | |
| // "Usage:\n" + | |
| // " <paraName>\n\n" + | |
| // "Where:\n" + | |
| // " paraName - the name of the parameter.\n"; | |
| // | |
| // if (args.length != 1) { | |
| // System.out.println(USAGE); | |
| // System.exit(1); | |
| // } | |
| //String paraName = args[0]; | |
| String paraName = "folastestparam"; | |
| Region region = Region.US_EAST_1; | |
| SsmClient ssmClient = SsmClient.builder() | |
| .region(region) | |
| .build(); | |
| getParaValue(ssmClient, paraName); | |
| ssmClient.close(); | |
| } | |
| public static void getParaValue(SsmClient ssmClient, String paraName) { | |
| 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); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <groupId>com.example.myapp</groupId> <artifactId>myapp</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>myapp</name> <dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.16.60</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>ssm</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>