Skip to content

Instantly share code, notes, and snippets.

@jackylamhk
Created July 5, 2024 02:37
Show Gist options
  • Save jackylamhk/ade53ee825b92d4cef6759a8ae086143 to your computer and use it in GitHub Desktop.
Save jackylamhk/ade53ee825b92d4cef6759a8ae086143 to your computer and use it in GitHub Desktop.
AWS Secrets Manager - Python
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