Knowledge
  • Read Me
  • Programming
    • ASP.NET
      • .NET Libraries
      • ASP.NET Core
        • Helper
          • Encryption
          • CSV Helper
          • String Helper
        • Logging
          • Simple Serilog
        • Middlewares
          • IP Restrictions
          • Request Throttling
          • Request Logging
        • Console
          • Command Line with arguments
        • JSON
      • ASP.NET Framework
      • Testing
        • Resources
        • xUnit.net
      • Naming Conventions
      • REST API Guidelines
    • Database
      • SQL Style Guide
      • MSSQL
        • Installation
          • Install MSSQL on MacOS M1 (ARM64)
        • Looping
        • Table Valued Functions
        • Session State
        • SQL Cheat Sheet
        • Export Pipe Delimited CSV With cmdshell
      • Redis
        • Redis Installation on Mac OS
        • Redis Installation on Docker
    • Java
      • AWS SDK - SSM
      • mTLS HTTP Connection
      • Read Resource Files
    • Javascript
      • Javascript Libraries
    • Python
    • OpenSSL
      • One Way SSL & Two Way SSL
      • Common OpenSSL Commands
      • Create Self-Signed Certificate
    • Misc
      • Git Commands
      • Windows Commands
      • PowerShell Commands
      • Vulnerabilities Dependency Check
      • Replace Filename Command
      • JSON Web Token (JWT)
      • Rabbit MQ Message-Broker
      • Pandoc Convert Document
  • DevOps
    • What is DevOps
    • CI & CD
    • Azure DevOps
  • Tools
    • Development Tools
Powered by GitBook
On this page

Was this helpful?

  1. Programming
  2. Java

AWS SDK - SSM

Example usage

SsmClient ssmClient = AwsUtils.GetSsmClient(Region.AP_SOUTHEAST_1);

try {	
  String value1 = AwsUtils.getSsmParamValue(ssmClient, "normal_string_without_encryption");
  String value2 = AwsUtils.getSsmParamValueWithDecryption(ssmClient, "secure_string_encrypted_with_kms");
} catch (SsmException e) {
  LOG.error("failed to get ssm param value", e);
  throw e;
} finally {
  ssmClient.close();
}

Maven dependencies

//pom.xml
  <dependencies>
    <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/ssm -->
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>ssm</artifactId>
      <version>2.17.229</version>
    </dependency>

AWS Utilities

//AwsUtils.java
package com.example.awsssm;

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;

/**
 * <h1>AWS utilities.</h1>
 * Reference:
 * <ul>
 *   <li>https://docs.aws.amazon.com/code-samples/latest/catalog/javav2-ssm-src-main-java-com-example-ssm-GetParameter.java.html</li>
 * </ul>
 */
public class AwsUtils {
  public static SsmClient GetSsmClient() {		
    return GetSsmClient(Region.AP_SOUTHEAST_1);
  }

  public static SsmClient GetSsmClient(Region region) {		
    SsmClient ssmClient = SsmClient.builder()
      .region(region)
      .build();

    return ssmClient;
  }

  public static String getSsmParamValueWithDecryption(SsmClient client, String paramName) throws SsmException {
    GetParameterRequest request = GetParameterRequest.builder()
      .name(paramName)
      .withDecryption(Boolean.TRUE)
      .build();

    GetParameterResponse response = client.getParameter(request);

    return response.parameter().value();
  }

  public static String getSsmParamValue(SsmClient client, String paramName) throws SsmException {
    GetParameterRequest request = GetParameterRequest.builder()
      .name(paramName)
      .build();

    GetParameterResponse response = client.getParameter(request);

    return response.parameter().value();
  }
}

Reference

PreviousJavaNextmTLS HTTP Connection

Last updated 2 years ago

Was this helpful?

AWS Systems Manager Parameter Store
Amazon SSM - GetParameter.java