-
-
Save KyMidd/a45a7116e735d393f05b117c0c18ba84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_secret_value(SecretName, Region): | |
| secret_name = SecretName | |
| region_name = Region | |
| session = boto3.session.Session() | |
| client = session.client( | |
| service_name='secretsmanager', | |
| region_name=region_name, | |
| ) | |
| try: | |
| get_secret_value_response = client.get_secret_value( | |
| SecretId=secret_name | |
| ) | |
| except ClientError as e: | |
| if e.response['Error']['Code'] == 'ResourceNotFoundException': | |
| print("The requested secret " + secret_name + " was not found") | |
| elif e.response['Error']['Code'] == 'InvalidRequestException': | |
| print("The request was invalid due to:", e) | |
| elif e.response['Error']['Code'] == 'InvalidParameterException': | |
| print("The request had invalid params:", e) | |
| elif e.response['Error']['Code'] == 'DecryptionFailure': | |
| print("The requested secret can't be decrypted using the provided KMS key:", e) | |
| elif e.response['Error']['Code'] == 'InternalServiceError': | |
| print("An error occurred on service side:", e) | |
| else: | |
| # Secrets Manager decrypts the secret value using the associated KMS CMK | |
| # Depending on whether the secret was a string or binary, only one of these fields will be populated | |
| if 'SecretString' in get_secret_value_response: | |
| SecretValue = get_secret_value_response['SecretString'] | |
| return(SecretValue) | |
| else: | |
| SecretValue = get_secret_value_response['SecretBinary'] | |
| return(SecretValue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment