Security python Intermediate

Secrets Rotation Script

Rotate an AWS Secrets Manager secret and update the consuming service automatically.

python
import boto3, secrets, string

client = boto3.client('secretsmanager')

def rotate_secret(secret_id: str):
    alphabet = string.ascii_letters + string.digits + '!@#'
    new_value = ''.join(secrets.choice(alphabet) for _ in range(32))
    client.put_secret_value(SecretId=secret_id, SecretString=new_value)
    print(f'Rotated {secret_id}')

Prerequisites

  • boto3
  • AWS Secrets Manager

Pro Tips

  • → Use Lambda + EventBridge for scheduled rotation.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
python
Level
Intermediate
Read time
4 min
Published
Feb 2025
License
Commercial License

Newsletter

New modules every week, directly to your inbox.