@@ -121,13 +121,36 @@ def _get_multiple(self, path: str, **sdk_options) -> Dict[str, str]:
121
121
raise NotImplementedError ()
122
122
123
123
def _create_secret (self , name : str , ** sdk_options ):
124
+ """
125
+ Create a secret with the given name.
126
+
127
+ Parameters:
128
+ ----------
129
+ name: str
130
+ The name of the secret.
131
+ **sdk_options:
132
+ Additional options to be passed to the create_secret method.
133
+
134
+ Raises:
135
+ SetSecretError: If there is an error setting the secret.
136
+ """
124
137
try :
125
138
sdk_options ["Name" ] = name
126
139
return self .client .create_secret (** sdk_options )
127
140
except Exception as exc :
128
141
raise SetSecretError (f"Error setting secret - { str (exc )} " ) from exc
129
142
130
143
def _update_secret (self , name : str , ** sdk_options ):
144
+ """
145
+ Update a secret with the given name.
146
+
147
+ Parameters:
148
+ ----------
149
+ name: str
150
+ The name of the secret.
151
+ **sdk_options:
152
+ Additional options to be passed to the create_secret method.
153
+ """
131
154
sdk_options ["SecretId" ] = name
132
155
return self .client .put_secret_value (** sdk_options )
133
156
@@ -140,13 +163,28 @@ def set(
140
163
** sdk_options ,
141
164
) -> SetSecretResponse :
142
165
"""
143
- Modifies the details of a secret, including metadata and the secret value.
166
+ Modify the details of a secret or create a new secret if it doesn't already exist.
167
+ It includes metadata and the secret value.
168
+
169
+ We aim to minimize API calls by assuming that the secret already exists and needs updating.
170
+ If it doesn't exist, we attempt to create a new one. Refer to the following workflow for a better understanding:
171
+
172
+
173
+ ┌────────────────────────┐ ┌─────────────────┐
174
+ ┌───────▶│Resource NotFound error?│────▶│Create Secret API│─────┐
175
+ │ └────────────────────────┘ └─────────────────┘ │
176
+ │ │
177
+ │ │
178
+ │ ▼
179
+ ┌─────────────────┐ ┌─────────────────────┐
180
+ │Update Secret API│────────────────────────────────────────────▶│ Return or Exception │
181
+ └─────────────────┘ └─────────────────────┘
144
182
145
183
Parameters
146
184
----------
147
185
name: str
148
- The ARN or name of the secret to add a new version to.
149
- value: str or bytes
186
+ The ARN or name of the secret to add a new version to or create a new one .
187
+ value: str, dict or bytes
150
188
Specifies text data that you want to encrypt and store in this new version of the secret.
151
189
client_request_token: str, optional
152
190
This value helps ensure idempotency. Recommended that you generate
@@ -156,13 +194,39 @@ def set(
156
194
sdk_options: dict, optional
157
195
Dictionary of options that will be passed to the Secrets Manager update_secret API call
158
196
197
+ Raises
198
+ ------
199
+ SetSecretError
200
+ When attempting to update or create a secret fails.
201
+
159
202
Returns:
160
203
-------
161
- Version ID of the newly created version of the secret.
204
+ SetSecretResponse:
205
+ The dict returned by boto3.
206
+
207
+ Example
208
+ -------
209
+ **Sets a secret***
210
+
211
+ >>> from aws_lambda_powertools.utilities import parameters
212
+ >>>
213
+ >>> parameters.set_secret(name="llamas-are-awesome", value="supers3cr3tllam@passw0rd")
214
+
215
+ **Sets a secret and includes an client_request_token**
216
+
217
+ >>> from aws_lambda_powertools.utilities import parameters
218
+ >>> import uuid
219
+ >>>
220
+ >>> parameters.set_secret(
221
+ name="my-secret",
222
+ value='{"password": "supers3cr3tllam@passw0rd"}',
223
+ client_request_token=str(uuid.uuid4())
224
+ )
162
225
163
226
URLs:
164
227
-------
165
228
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager/client/put_secret_value.html
229
+ https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager/client/create_secret.html
166
230
"""
167
231
168
232
if isinstance (value , dict ):
@@ -301,37 +365,46 @@ def set_secret(
301
365
** sdk_options ,
302
366
) -> str :
303
367
"""
304
- Retrieve a parameter value from AWS Secrets Manager
368
+ Modify the details of a secret or create a new secret if it doesn't already exist.
369
+ It includes metadata and the secret value.
370
+
371
+ We aim to minimize API calls by assuming that the secret already exists and needs updating.
372
+ If it doesn't exist, we attempt to create a new one. Refer to the following workflow for a better understanding:
373
+
374
+
375
+ ┌────────────────────────┐ ┌─────────────────┐
376
+ ┌───────▶│Resource NotFound error?│────▶│Create Secret API│─────┐
377
+ │ └────────────────────────┘ └─────────────────┘ │
378
+ │ │
379
+ │ │
380
+ │ ▼
381
+ ┌─────────────────┐ ┌─────────────────────┐
382
+ │Update Secret API│────────────────────────────────────────────▶│ Return or Exception │
383
+ └─────────────────┘ └─────────────────────┘
305
384
306
385
Parameters
307
386
----------
308
387
name: str
309
- Name of the parameter
310
- value: str or bytes
311
- Secret value to set
388
+ The ARN or name of the secret to add a new version to or create a new one.
389
+ value: str, dict or bytes
390
+ Specifies text data that you want to encrypt and store in this new version of the secret.
312
391
client_request_token: str, optional
313
392
This value helps ensure idempotency. Recommended that you generate
314
393
a UUID-type value to ensure uniqueness within the specified secret.
315
394
This value becomes the VersionId of the new version. This field is
316
395
autopopulated if not provided.
317
- version_stages: list[str], optional
318
- A list of staging labels that are attached to this version of the secret.
319
396
sdk_options: dict, optional
320
- Dictionary of options that will be passed to the get_secret_value call
397
+ Dictionary of options that will be passed to the Secrets Manager update_secret API call
321
398
322
399
Raises
323
400
------
324
- SetParameterError
325
- When the secrets provider fails to set a secret value or secret binary for
326
- a given name.
401
+ SetSecretError
402
+ When attempting to update or create a secret fails.
327
403
328
404
Returns:
329
405
-------
330
- Version ID of the newly created version of the secret.
331
-
332
- URLs:
333
- -------
334
- https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager/client/put_secret_value.html
406
+ SetSecretResponse:
407
+ The dict returned by boto3.
335
408
336
409
Example
337
410
-------
@@ -350,6 +423,11 @@ def set_secret(
350
423
value='{"password": "supers3cr3tllam@passw0rd"}',
351
424
client_request_token="61f2af5f-5f75-44b1-a29f-0cc37af55b11"
352
425
)
426
+
427
+ URLs:
428
+ -------
429
+ https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager/client/put_secret_value.html
430
+ https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager/client/create_secret.html
353
431
"""
354
432
355
433
# Only create the provider if this function is called at least once
0 commit comments