Created
July 5, 2024 02:37
-
-
Save jackylamhk/ade53ee825b92d4cef6759a8ae086143 to your computer and use it in GitHub Desktop.
AWS Secrets Manager - Python
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 logging | |
import os | |
import json | |
import httpx | |
logger = logging.getLogger(__name__) | |
class AWSSecretsConfig: | |
_TOKEN = os.environ.get("AWS_SESSION_TOKEN") | |
ENABLED = os.environ.get("SECRETS_MANAGER_ENABLED", "false").lower() == "true" | |
SECRET_ID = os.environ.get("SECRETS_MANAGER_SECRET_ID") | |
PORT = os.environ.get("PARAMETERS_SECRETS_EXTENSION_HTTP_PORT", 2773) | |
URL = f"http://localhost:{PORT}" | |
def get_secret_config(): | |
with httpx.Client( | |
base_url=AWSSecretsConfig.URL, | |
headers={ | |
"X-Aws-Parameters-Secrets-Token": AWSSecretsConfig._TOKEN, | |
}, | |
) as client: | |
resp = client.get( | |
"/secretsmanager/get", params={"secretId": AWSSecretsConfig.SECRET_ID} | |
) | |
resp.raise_for_status() | |
logger.info("Retrieved config from AWS Secrets Manager") | |
return json.loads(resp.json()["SecretString"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment