Use Python to get a secret from AWS Secrets Manager and cache it.
IntermediateHands-onPython for DevOps
Answer
import functools
import json
import boto3
sm = boto3.client("secretsmanager")
@functools.lru_cache(maxsize=32)
def get_secret(name: str) -> dict:
value = sm.get_secret_value(SecretId=name)["SecretString"]
return json.loads(value)
db = get_secret("prod/app/db")
print(db["username"])
Key trick → Caching avoids per-request API calls (cost and throttling). In production add a TTL (or use AWS's caching client) so rotated secrets are picked up.
More on Python for DevOps
Write a Python script using boto3 that lists all running EC2 instances with their Name tag.BeginnerWrite a script that finds unattached EBS volumes and deletes them (with a dry-run flag).IntermediateCall a REST API with `requests`, handle timeouts and non-2xx responses properly.BeginnerAdd retry with exponential backoff to HTTP calls without writing the loop yourself.IntermediateWrite your own retry decorator with exponential backoff and jitter.AdvancedParse a YAML file and print every container image in a multi-document Kubernetes manifest.Intermediate