# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to use the AWS SDK for Python (Boto3) to manage secrets in AWS Secrets Manager. """ import logging logger = logging.getLogger(__name__) # snippet-start:[python.example_code.python.GetSecretValue.full] # snippet-start:[python.example_code.python.GetSecretValue.decl] class GetSecretWrapper: def __init__(self, secretsmanager_client): self.client = secretsmanager_client # snippet-end:[python.example_code.python.GetSecretValue.decl] def get_secret(self, secret_name): """ Retrieve individual secrets from AWS Secrets Manager using the get_secret_value API. This function assumes the stack mentioned in the source code README has been successfully deployed. This stack includes 7 secrets, all of which have names beginning with "mySecret". :param secret_name: The name of the secret fetched. :type secret_name: str """ try: get_secret_value_response = self.client.get_secret_value( SecretId=secret_name ) logging.info("Secret retrieved successfully.") return get_secret_value_response["SecretString"] except self.client.exceptions.ResourceNotFoundException: msg = f"The requested secret {secret_name} was not found." logger.info(msg) return msg except Exception as e: logger.error(f"An unknown error occurred: {str(e)}.") raise # snippet-end:[python.example_code.python.GetSecretValue.full]