Last active
November 28, 2022 20:22
-
-
Save nmagee/c63e0d5ab38c5108d75adfa22a675d4c to your computer and use it in GitHub Desktop.
Decrypt an AWS Secrets Manager secret
This file contains 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
import boto3 | |
from botocore.exceptions import ClientError | |
def get_secret(secret_name,region_name): | |
# Create a Secrets Manager client | |
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: | |
# For a list of exceptions thrown, see | |
# https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html | |
raise e | |
# Decrypts secret using the associated KMS key. | |
secret = get_secret_value_response['SecretString'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment