How do you read secrets from AWS Secrets Manager or SSM Parameter Store?
IntermediateConceptboto3 & AWS automation
Answer
sm = boto3.client("secretsmanager")
secret = json.loads(sm.get_secret_value(SecretId="prod/db")["SecretString"])
ssm = boto3.client("ssm")
token = ssm.get_parameter(Name="/ops/slack/token", WithDecryption=True)["Parameter"]["Value"]
Cache secrets in memory for the process lifetime (or with a TTL) rather than fetching per request; both APIs are priced and throttled. AWS provides aws-secretsmanager-caching for this. Never write secrets to disk or logs.
More on boto3 & AWS automation
What is the difference between a boto3 Session, client and resource?BeginnerHow does boto3 find credentials?BeginnerHow does boto3 decide the region, and what is the gotcha with `AWS_REGION`?AdvancedWhy do you need paginators, and how do you use them?BeginnerWhat is JMESPath `search()` on paginators?AdvancedHow do you handle errors from boto3?Beginner