Skip to content

Instantly share code, notes, and snippets.

@eidosam
Created August 19, 2022 14:59
Show Gist options
  • Save eidosam/b958519355445c8d6528674162d7a82e to your computer and use it in GitHub Desktop.
Save eidosam/b958519355445c8d6528674162d7a82e to your computer and use it in GitHub Desktop.
from boto3.session import Session
import base64
from botocore.exceptions import ClientError
def get_secret(secret_name, region_name='us-west-2'):
secret_value_response = {}
try:
secret_value_response = (
Session(region_name=region_name)
.client(service_name='secretsmanager')
.get_secret_value(SecretId=secret_name)
)
except ClientError as exception:
if exception.response['Error']['Code'] == 'DecryptionFailureException':
raise exception
elif exception.response['Error']['Code'] == 'InternalServiceErrorException':
raise exception
elif exception.response['Error']['Code'] == 'InvalidParameterException':
raise exception
elif exception.response['Error']['Code'] == 'InvalidRequestException':
raise exception
elif exception.response['Error']['Code'] == 'ResourceNotFoundException':
raise exception
else:
if 'SecretString' in secret_value_response:
secret = secret_value_response['SecretString']
else:
secret = base64.b64decode(secret_value_response['SecretBinary'])
return secret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment