Skip to content

Instantly share code, notes, and snippets.

@aleonjob
Forked from sasaken555/ParamStoreUtil.scala
Created January 30, 2024 14:43
Show Gist options
  • Save aleonjob/936bb30a9b23f6483f34494529451b1f to your computer and use it in GitHub Desktop.
Save aleonjob/936bb30a9b23f6483f34494529451b1f to your computer and use it in GitHub Desktop.
Find value from AWS SSM ParameterStore with Scala
package net.ponzmild.aws
import com.amazonaws.services.simplesystemsmanagement.model.{GetParameterRequest}
import com.amazonaws.services.simplesystemsmanagement.{AWSSimpleSystemsManagement, AWSSimpleSystemsManagementClientBuilder}
import scala.util.{Failure, Success, Try}
/**
* Find value from AWS SSM ParameterStore
*/
class ParamStoreUtil {
private def getClient: AWSSimpleSystemsManagement = AWSSimpleSystemsManagementClientBuilder.standard().build()
/**
* Find value by a key.
*
* @param key Key Name of SSM ParameterStore
* @return the value stored with the specified key
* @exception ParameterNotFoundException the value for key not found...
*/
def getParam(key: String): String = {
val request = new GetParameterRequest()
request.withName(key)
val ssmRequest = Try(getClient.getParameter(request))
ssmRequest match {
case Success(value) => value.getParameter.getValue
case Failure(exception) => throw exception
}
}
}
package net.ponzmild.aws
import com.amazonaws.services.simplesystemsmanagement.model.ParameterNotFoundException
import org.scalatest.FunSuite
/**
* Test ParamStoreUtil with ScalaTest
*/
class ParamStoreUtilTest extends FunSuite {
test(".getParam should return specified value") {
val testKey ="/Test/Common/Common/Test-Value" // Register value with this key before!
val util = new ParamStoreUtil
assert(util.getParam(testKey) == "test")
}
test(".getParam should return NotFound Exception") {
val failureKey ="/No/Such/Key/Exists"
val util = new ParamStoreUtil
assertThrows[ParameterNotFoundException] {
util.getParam(failureKey)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment