Parameters
The parameters utility provides a way to retrieve parameter values from
AWS Systems Manager Parameter Store,
AWS Secrets Manager, or Amazon DynamoDB.
It also provides a base class to create your parameter provider implementation.
Key features
- Retrieve one or multiple parameters from the underlying provider
- Cache parameter values for a given amount of time (defaults to 5 seconds)
- Transform parameter values from JSON or base 64 encoded strings
Install
Depending on your version of Java (either Java 1.8 or 11+), the configuration slightly changes.
IAM Permissions
This utility requires additional permissions to work as expected. See the table below:
Provider |
Function/Method |
IAM Permission |
SSM Parameter Store |
SSMProvider.get(String) SSMProvider.get(String, Class) |
ssm:GetParameter |
SSM Parameter Store |
SSMProvider.getMultiple(String) |
ssm:GetParametersByPath |
Secrets Manager |
SecretsProvider.get(String) SecretsProvider.get(String, Class) |
secretsmanager:GetSecretValue |
DynamoDB |
DynamoDBProvider.get(String) DynamoDBProvider.getMultiple(string) |
dynamodb:GetItem dynamoDB:Query |
SSM Parameter Store
You can retrieve a single parameter using SSMProvider.get() and pass the key of the parameter.
For multiple parameters, you can use SSMProvider.getMultiple() and pass the path to retrieve them all.
Alternatively, you can retrieve an instance of a provider and configure its underlying SDK client,
in order to get data from other regions or use specific credentials.
Additional arguments
The AWS Systems Manager Parameter Store provider supports two additional arguments for the get()
and getMultiple()
methods:
Option |
Default |
Description |
withDecryption() |
False |
Will automatically decrypt the parameter. |
recursive() |
False |
For getMultiple() only, will fetch all parameter values recursively based on a path prefix. |
Example:
Secrets Manager
For secrets stored in Secrets Manager, use getSecretsProvider
.
Alternatively, you can retrieve an instance of a provider and configure its underlying SDK client,
in order to get data from other regions or use specific credentials.
DynamoDB
To get secrets stored in DynamoDB, use getDynamoDbProvider
, providing the name of the table that
contains the secrets. As with the other providers, an overloaded methods allows you to retrieve
a DynamoDbProvider
providing a client if you need to configure it yourself.
AppConfig
To get parameters stored in AppConfig, use getAppConfigProvider
, providing the application and environment
name to retrieve configuration from. As with the other providers, an overloaded method allows you to retrieve
an AppConfigProvider
providing a client if you need to configure it yourself.
Advanced configuration
Caching
By default, all parameters and their corresponding values are cached for 5 seconds.
You can customize this default value using defaultMaxAge
. You can also customize this value for each parameter using
withMaxAge
.
Parameter values can be transformed using withTransformation(transformerClass)
.
Base64 and JSON transformations are provided. For more complex transformation, you need to specify how to deserialize-
SSMProvider.getMultiple()
does not support transformation and will return simple Strings.
You can write your own transformer, by implementing the Transformer
interface and the applyTransformation()
method.
For example, if you wish to deserialize XML into an object.
Fluent API
To simplify the use of the library, you can chain all method calls before a get.
Create your own provider
You can create your own custom parameter store provider by inheriting the BaseProvider
class and implementing the
String getValue(String key)
method to retrieve data from your underlying store. All transformation and caching logic is handled by the get() methods in the base class.
Annotation
You can make use of the annotation @Param
to inject a parameter value in a variable.
By default, it will use SSMProvider
to retrieve the value from AWS System Manager Parameter Store.
You could specify a different provider as long as it extends BaseProvider
and/or a Transformer
.
Install
If you want to use the @Param
annotation in your project add configuration to compile-time weave (CTW) the powertools-parameters aspects into your project.