Debian Codegraph @

docs/source/guide/secrets-manager.rst

raw
1 .. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
3 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
4 International License (the "License"). You may not use this file except in compliance with the
5 License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.
6
7 This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
8 either express or implied. See the License for the specific language governing permissions and
9 limitations under the License.
10
11 .. _aws-boto3-secrets-manager:
12
13 ###################
14 AWS Secrets Manager
15 ###################
16
17 This Python example shows you how to retrieve the decrypted secret value from an AWS Secrets Manager secret. The secret could be created using either the Secrets Manager console or the CLI/SDK.
18
19 The code uses the AWS SDK for Python to retrieve a decrypted secret value.
20
21 For more information about using an Amazon Secrets Manager, see
22 `Tutorial: Storing and Retrieving a Secret <https://docs.aws.amazon.com/secretsmanager/latest/userguide/tutorials_basic.html>`_
23 in the *AWS Secrets Manager Developer Guide*.
24
25 All the example code for the Amazon Web Services (AWS) SDK for Python is available `here on GitHub <https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code>`_.
26
27 Prerequisite tasks
28 ==================
29
30 To set up and run this example, you must first set up the following:
31
32 * Configure your AWS credentials, as described in :doc:`quickstart`.
33 * Create a secret with the AWS Secrets Manager, as described in the `AWS Secrets Manager Developer Guide <https://docs.aws.amazon.com/secretsmanager/latest/userguide/manage_create-basic-secret.html>`_
34
35 Retrieve the secret value
36 =============================================
37
38 The following example shows how to:
39
40 * Retrieve a secret value using
41 `get_secret_value <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager.html#SecretsManager.Client.get_secret_value>`_.
42
43 Example
44 -------
45
46 .. code-block:: python
47
48 import boto3
49 from botocore.exceptions import ClientError
50
51
52 def get_secret():
53 secret_name = "MySecretName"
54 region_name = "us-west-2"
55
56 session = boto3.session.Session()
57 client = session.client(
58 service_name='secretsmanager',
59 region_name=region_name,
60 )
61
62 try:
63 get_secret_value_response = client.get_secret_value(
64 SecretId=secret_name
65 )
66 except ClientError as e:
67 if e.response['Error']['Code'] == 'ResourceNotFoundException':
68 print("The requested secret " + secret_name + " was not found")
69 elif e.response['Error']['Code'] == 'InvalidRequestException':
70 print("The request was invalid due to:", e)
71 elif e.response['Error']['Code'] == 'InvalidParameterException':
72 print("The request had invalid params:", e)
73 elif e.response['Error']['Code'] == 'DecryptionFailure':
74 print("The requested secret can't be decrypted using the provided KMS key:", e)
75 elif e.response['Error']['Code'] == 'InternalServiceError':
76 print("An error occurred on service side:", e)
77 else:
78 # Secrets Manager decrypts the secret value using the associated KMS CMK
79 # Depending on whether the secret was a string or binary, only one of these fields will be populated
80 if 'SecretString' in get_secret_value_response:
81 text_secret_data = get_secret_value_response['SecretString']
82 else:
83 binary_secret_data = get_secret_value_response['SecretBinary']
84
85 # Your code goes here.
86
87