You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The parameters utility provides a way to retrieve parameter values from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html), [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/) or [Amazon DynamoDB](https://aws.amazon.com/dynamodb/). It also provides a base class to create your parameter provider implementation.
7
+
The parameters utility provides high-level functions to retrieve one or multiple parameter values from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html){target="_blank"}, [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/), [AWS AppConfig](https://aws.amazon.com/appconfig/){target="_blank"}, [Amazon DynamoDB](https://aws.amazon.com/dynamodb/){target="_blank"}, or bring your own.
8
8
9
-
**Key features**
9
+
## Key features
10
10
11
11
* Retrieve one or multiple parameters from the underlying provider
12
12
* Cache parameter values for a given amount of time (defaults to 5 seconds)
13
13
* Transform parameter values from JSON or base 64 encoded strings
14
+
* Bring Your Own Parameter Store Provider
14
15
15
-
**IAM Permissions**
16
+
## Getting started
16
17
17
-
This utility requires additional permissions to work as expected. See the table below:
18
+
By default, we fetch parameters from System Manager Parameter Store, secrets from Secrets Manager, and application configuration from AppConfig.
19
+
20
+
### IAM Permissions
21
+
22
+
This utility requires additional permissions to work as expected.
23
+
24
+
!!! note "Different parameter providers require different permissions"
You can retrieve a single parameter using `get_parameter` high-level function. For multiple parameters, you can use `get_parameters` and pass a path to retrieve them recursively.
37
+
You can retrieve a single parameter using `get_parameter` high-level function.
38
+
39
+
For multiple parameters, you can use `get_parameters` and pass a path to retrieve them recursively.
31
40
32
41
=== "ssm_parameter_store.py"
33
42
34
-
```python
43
+
```python hl_lines="1 5 9"
35
44
from aws_lambda_powertools.utilities import parameters
36
45
37
46
def handler(event, context):
@@ -45,15 +54,51 @@ You can retrieve a single parameter using `get_parameter` high-level function. F
45
54
print(f"{k}: {v}")
46
55
```
47
56
48
-
### SSMProvider class
57
+
### Fetching secrets
58
+
59
+
You can fetch secrets stored in Secrets Manager using `get_secrets`.
60
+
61
+
=== "secrets_manager.py"
62
+
63
+
```python hl_lines="1 5"
64
+
from aws_lambda_powertools.utilities import parameters
65
+
66
+
def handler(event, context):
67
+
# Retrieve a single secret
68
+
value = parameters.get_secret("my-secret")
69
+
```
70
+
71
+
### Fetching app configurations
72
+
73
+
> New in 1.10.0
49
74
50
-
Alternatively, you can use the `SSMProvider` class, which gives more flexibility, such as the ability to configure the underlying SDK client.
75
+
You can fetch application configurations in AWS AppConfig using `get_app_config`.
51
76
52
-
This can be used to retrieve values from other regions, change the retry behavior, etc.
77
+
The following will retrieve the latest version and store it in the cache.
78
+
79
+
=== "appconfig.py"
80
+
81
+
```python hl_lines="1 5"
82
+
from aws_lambda_powertools.utilities import parameters
For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use their respective Provider Classes directly.
94
+
95
+
!!! tip "This can be used to retrieve values from other regions, change the retry behavior, etc."
96
+
97
+
#### SSMProvider
53
98
54
99
=== "ssm_parameter_store.py"
55
100
56
-
```python
101
+
```python hl_lines="5 9 12"
57
102
from aws_lambda_powertools.utilities import parameters
58
103
from botocore.config import Config
59
104
@@ -70,20 +115,18 @@ This can be used to retrieve values from other regions, change the retry behavio
70
115
print(f"{k}: {v}")
71
116
```
72
117
73
-
**Additional arguments**
74
-
75
118
The AWS Systems Manager Parameter Store provider supports two additional arguments for the `get()` and `get_multiple()` methods:
76
119
77
120
| Parameter | Default | Description |
78
121
|---------------|---------|-------------|
79
122
|**decrypt**|`False`| Will automatically decrypt the parameter. |
80
123
|**recursive**|`True`| For `get_multiple()` only, will fetch all parameter values recursively based on a path prefix. |
81
124
82
-
**Example:**
125
+
> **Example**
83
126
84
127
=== "ssm_parameter_store.py"
85
128
86
-
```python
129
+
```python hl_lines="6 8"
87
130
from aws_lambda_powertools.utilities import parameters
88
131
89
132
ssm_provider = parameters.SSMProvider()
@@ -94,29 +137,11 @@ The AWS Systems Manager Parameter Store provider supports two additional argumen
For secrets stored in Secrets Manager, use `get_secret`.
140
+
#### SecretsProvider
100
141
101
142
=== "secrets_manager.py"
102
143
103
-
```python
104
-
from aws_lambda_powertools.utilities import parameters
105
-
106
-
def handler(event, context):
107
-
# Retrieve a single secret
108
-
value = parameters.get_secret("my-secret")
109
-
```
110
-
111
-
### SecretsProvider class
112
-
113
-
Alternatively, you can use the `SecretsProvider` class, which give more flexibility, such as the ability to configure the underlying SDK client.
114
-
115
-
This can be used to retrieve values from other regions, change the retry behavior, etc.
116
-
117
-
=== "secrets_manager.py"
118
-
119
-
```python
144
+
```python hl_lines="5 9"
120
145
from aws_lambda_powertools.utilities import parameters
121
146
from botocore.config import Config
122
147
@@ -128,25 +153,24 @@ This can be used to retrieve values from other regions, change the retry behavio
128
153
value = secrets_provider.get("my-secret")
129
154
```
130
155
131
-
## DynamoDB
132
-
133
-
To use the DynamoDB provider, you need to import and instantiate the `DynamoDBProvider` class.
156
+
#### DynamoDBProvider
134
157
135
158
The DynamoDB Provider does not have any high-level functions, as it needs to know the name of the DynamoDB table containing the parameters.
136
159
137
-
**DynamoDB table structure**
160
+
**DynamoDB table structure for single parameters**
138
161
139
-
When using the default options, if you want to retrieve only single parameters, your table should be structured as such, assuming a parameter named **my-parameter** with a value of **my-value**. The `id`attribute should be the [partition key](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey) for that table.
162
+
For single parameters, you must use `id`as the [partition key](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey) for that table.
140
163
141
164
| id | value |
142
165
|--------------|----------|
143
166
| my-parameter | my-value |
144
167
145
-
With this table, when you do a `dynamodb_provider.get("my-param")` call, this will return `my-value`.
168
+
> **Example**
146
169
147
-
=== "dynamodb.py"
170
+
=== "app.py"
171
+
With this table, the return value of `dynamodb_provider.get("my-param")` call will be `my-value`.
148
172
149
-
```python
173
+
```python hl_lines="3 7"
150
174
from aws_lambda_powertools.utilities import parameters
@@ -156,31 +180,34 @@ With this table, when you do a `dynamodb_provider.get("my-param")` call, this wi
156
180
value = dynamodb_provider.get("my-parameter")
157
181
```
158
182
159
-
**Retrieve multiple values**
183
+
**DynamoDB table structure for multiple values parameters**
184
+
185
+
If you want to be able to retrieve multiple parameters at once sharing the same `id`, your table needs to contain a sort key name `sk`.
160
186
161
-
If you want to be able to retrieve multiple parameters at once sharing the same `id`, your table needs to contain a sort key name `sk`. For example, if you want to retrieve multiple parameters having `my-hash-key` as ID:
187
+
For example, if you want to retrieve multiple parameters having `my-hash-key` as ID:
162
188
163
189
| id | sk | value |
164
190
|-------------|---------|------------|
165
191
| my-hash-key | param-a | my-value-a |
166
192
| my-hash-key | param-b | my-value-b |
167
193
| my-hash-key | param-c | my-value-c |
168
194
169
-
With this table, when you do a `dynamodb_provider.get_multiple("my-hash-key")` call, you will receive the following dict as a response:
170
195
171
-
```
196
+
With this table, the return of `dynamodb_provider.get_multiple("my-hash-key")` call will be a dictionary like:
197
+
198
+
```json
172
199
{
173
200
"param-a": "my-value-a",
174
201
"param-b": "my-value-b",
175
202
"param-c": "my-value-c"
176
203
}
177
204
```
178
205
179
-
**Example:**
206
+
> **Example**
180
207
181
-
=== "dynamodb_multiple.py"
208
+
=== "app_multiple_parameters.py"
182
209
183
-
```python
210
+
```python hl_lines="3 8"
184
211
from aws_lambda_powertools.utilities import parameters
Alternatively, you can use the `AppConfigProvider` class, which give more flexibility, such as the ability to configure the underlying SDK client.
252
+
#### AppConfigProvider
243
253
244
-
This can be used to retrieve values from other regions, change the retry behavior, etc.
254
+
=== "app.py"
245
255
246
-
=== "appconfig.py"
247
-
248
-
```python
256
+
```python hl_lines="5 9"
249
257
from aws_lambda_powertools.utilities import parameters
250
258
from botocore.config import Config
251
259
@@ -257,7 +265,7 @@ This can be used to retrieve values from other regions, change the retry behavio
257
265
value: bytes = appconf_provider.get("my_conf")
258
266
```
259
267
260
-
## Create your own provider
268
+
###Create your own provider
261
269
262
270
You can create your own custom parameter store provider by inheriting the `BaseProvider` class, and implementing both `_get()` and `_get_multiple()` methods to retrieve a single, or multiple parameters from your custom store.
263
271
@@ -267,7 +275,7 @@ Here is an example implementation using S3 as a custom parameter store:
267
275
268
276
=== "custom_provider.py"
269
277
270
-
```python
278
+
```python hl_lines="3 6 17 27"
271
279
import copy
272
280
273
281
from aws_lambda_powertools.utilities import BaseProvider
@@ -321,13 +329,24 @@ Here is an example implementation using S3 as a custom parameter store:
321
329
322
330
```
323
331
324
-
## Transform values
332
+
### Deserializing values with transform parameter
333
+
334
+
For parameters stored in JSON or Base64 format, you can use the `transform` argument for deserialization.
325
335
326
-
For parameters stored in JSON or Base64 format, you can use the `transform` argument for deserialization - The `transform` argument is available across all providers, including the high level functions.
336
+
!!! info "The `transform` argument is available across all providers, including the high level functions"
327
337
328
-
=== "transform.py"
338
+
=== "High level functions"
329
339
330
-
```python
340
+
```python hl_lines="4"
341
+
from aws_lambda_powertools.utilities import parameters
### Partial transform failures with `get_multiple()`
362
+
#### Partial transform failures with `get_multiple()`
355
363
356
364
If you use `transform` with `get_multiple()`, you can have a single malformed parameter value. To prevent failing the entire request, the method will return a `None` value for the parameters that failed to transform.
357
365
358
-
You can override this by setting the `raise_on_transform_error` argument to `True`. If you do so, a single transform error will raise a `TransformParameterError` exception.
366
+
You can override this by setting the `raise_on_transform_error` argument to `True`. If you do so, a single transform error will raise a **`TransformParameterError`** exception.
359
367
360
-
For example, if you have three parameters (*/param/a*, */param/b* and */param/c*) but */param/c* is malformed:
368
+
For example, if you have three parameters, */param/a*, */param/b* and */param/c*, but */param/c* is malformed:
361
369
362
370
=== "partial_failures.py"
363
371
364
-
```python
372
+
```python hl_lines="9 14-15"
365
373
from aws_lambda_powertools.utilities import parameters
366
374
367
375
ssm_provider = parameters.SSMProvider()
@@ -379,13 +387,13 @@ For example, if you have three parameters (*/param/a*, */param/b* and */param/c*
0 commit comments