Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
Below are concise, step-by-step AWS Console instructions to remediate “Secrets Manager Secrets Should Be Rotated Frequently” for secrets encrypted with AWS KMS.
Repeat for all flagged secrets until all have automatic rotation enabled at the desired frequency.
1. Identify Non-Rotating Secrets
- Sign in to the AWS Management Console.
- Go to Secrets Manager:
- Services → Secrets Manager.
- In Secrets, look for secrets where:
- Rotation configuration column is “Disabled” or rotation interval is longer than your policy (e.g., >30 days).
2. Enable Rotation for a Secret
Perform this for each non-compliant secret.- In Secrets Manager, click the secret name.
- In the secret’s detail page, choose Rotate secret (or Edit rotation if already enabled but not compliant).
2.1 Choose Rotation Strategy
- On the Rotation configuration page:
- Check “Enable automatic rotation”.
- Set Rotation schedule:
- Choose “Every X days” and enter your required frequency
(e.g., 30 for monthly rotation).
- Choose “Every X days” and enter your required frequency
2.2 Choose or Create Rotation Lambda Function
-
Under Rotation function, choose one:
- If you already have a rotation Lambda for this type (e.g., RDS, DocumentDB, Redshift, etc.):
- Select “Use an existing Lambda function” and pick the function.
- If you don’t have one:
- Select “Create a new Lambda function”.
- Choose the secret type / database type (e.g., RDS, other database, custom).
- Follow the wizard to create the Lambda:
- Select the VPC, subnets, and security groups if the target is in a private network.
- Secrets Manager will create a template Lambda function with the correct rotation logic for that backend.
- If you already have a rotation Lambda for this type (e.g., RDS, DocumentDB, Redshift, etc.):
- Click Next, then Save / Enable rotation.
- Use your KMS key (already associated with the secret) to encrypt the new versions.
- Automatically invoke the Lambda on your schedule to rotate the secret.
3. Verify KMS Key Permissions (If Rotation Fails)
If rotation errors occur, you may need to adjust the KMS CMK policy:- Go to AWS KMS:
- Services → Key Management Service → Customer managed keys.
- Click the CMK used by the secret (shown on the secret’s detail page under Encryption key).
- Under Key policy:
- Ensure the IAM role used by the rotation Lambda function has:
kms:Encryptkms:Decryptkms:GenerateDataKeykms:DescribeKey
- Ensure the IAM role used by the rotation Lambda function has:
- Save the key policy if edited.
4. Confirm Rotation Is Working
- Back in Secrets Manager, open the secret.
- Confirm:
- Rotation configuration shows Enabled and the correct interval.
- Secret versions show multiple versions over time (after the first rotation occurs).
- Optionally, click Rotate secret now to test the configuration immediately.
Repeat for all flagged secrets until all have automatic rotation enabled at the desired frequency.
Using CLI
Using CLI
Below is how to remediate “Secrets Manager Secrets Should Be Rotated Frequently” for AWS using the AWS CLI. This covers enabling automatic rotation for an AWS Secrets Manager secret (which is encrypted with KMS).
Or filter by tag / name pattern as needed.
Create role:Attach basic Lambda logging and Secrets Manager permissions (adjust ARNs/permissions as needed):Your Then create the Lambda function (ZIP file must contain your rotation code):You can reuse this Lambda for multiple secrets of the same type.
Enable rotation:If rotation Lambda is already attached and you only need to adjust frequency:
You should see
Check status:
This configuration ensures Secrets Manager secrets are automatically rotated on the schedule you define, with encryption handled by KMS.
1. Identify the secret(s) that need rotation
2. (One-time) Create a rotation Lambda function
If you don’t already have a rotation Lambda, you need one. For RDS or other standard engines, use an AWS-provided template from the console or AWS docs; for CLI-only, this is the basic outline:- Create an IAM role for the Lambda with permissions to:
secretsmanager:GetSecretValuesecretsmanager:PutSecretValuesecretsmanager:UpdateSecretVersionStage- Any permissions needed to update the target (e.g., RDS, API key, etc.)
lambda-trust-policy.json:secrets-rotation-inline-policy.json should at minimum include:3. Enable automatic rotation on a secret
Pick a rotation interval (e.g., 30 days). Example formy-app-secret:4. Verify rotation configuration
RotationEnabled: true and AutomaticallyAfterDays: 30 (or your chosen value).5. (Optional) Enable KMS key rotation (separate from secret rotation)
If your secret uses a customer-managed KMS key and you also want that key to rotate annually:This configuration ensures Secrets Manager secrets are automatically rotated on the schedule you define, with encryption handled by KMS.
Using Python
Using Python
Below is a concise step‑by‑step guide to remediate “Secrets Manager secrets should be rotated frequently” for an AWS KMS‑encrypted secret using Python. This covers:
Replace the commented
This:
Or via boto3:This rotates the key’s cryptographic material annually, complementing secret rotation.
If you share what type of credential is stored in the secret (RDS, API key, etc.), I can adapt the
- What you need set up
- Creating a Python rotation function (Lambda)
- Attaching it to the secret with a rotation schedule
1. Prerequisites
- Existing secret in AWS Secrets Manager, encrypted with a KMS CMK (customer‑managed key or AWS managed key).
- IAM role for Lambda with at least:
secretsmanager:GetSecretValuesecretsmanager:PutSecretValuesecretsmanager:UpdateSecretVersionStage- Any permissions required to change the underlying credentials (e.g., RDS, user store, etc.).
- Python 3.x Lambda runtime.
2. Rotation Model to Use
Decide how new credentials are generated. Example scenarios:- RDS or other DB password: You call the DB to change password and then update the secret.
- API keys: You call the provider to issue a new key, then update the secret.
- KMS key material itself is not “rotated” by Secrets Manager. Instead, you rotate:
- Either the credentials that are stored in the secret (such as DB creds, API keys that are encrypted with KMS),
- Or you enable KMS key rotation directly on CMKs separately (outside of Secrets Manager):
3. Python Rotation Lambda – Core Template
AWS Secrets Manager expects the Lambda to implement alambda_handler that supports these steps:createSecretsetSecrettestSecretfinishSecret
3.1. Basic Lambda Skeleton (Python)
Create a new Lambda function with Python 3.x and paste:update_database_password / test_database_connection bits with the actual logic for your use case (API, DB, etc.).4. Configure the Lambda for Rotation
- Create the Lambda (console or CLI) with the above code.
- Attach an IAM role allowing:
secretsmanager:GetSecretValue,PutSecretValue,DescribeSecret,UpdateSecretVersionStage- Any service‑specific actions (e.g.,
rds:ModifyDBInstance, or API client permissions).
- Ensure Lambda environment has necessary config values (e.g., DB endpoint, user, etc.) if not stored in the secret.
5. Enable Rotation on the Secret (via Console or Python)
5.1. Using AWS Console
- Go to Secrets Manager → select the secret.
- Click Rotate secret.
- Choose Use an existing Lambda function and select the Lambda created above.
- Set rotation schedule (e.g., every 30 days).
- Save.
5.2. Using Python (boto3)
- Attaches the rotation Lambda.
- Sets rotation interval (e.g., 30 days).
- Triggers an immediate rotation cycle if
RotateImmediately=True.
6. Ensure KMS Key Rotation (Optional but Recommended)
If you use a customer‑managed KMS key to encrypt the secret, enable key rotation:If you share what type of credential is stored in the secret (RDS, API key, etc.), I can adapt the
setSecret and testSecret functions with concrete code.Using Terraform
Using Terraform
SECRET_KMS_KEYwith the Terraform name you use for the KMS key that encrypts your Secrets Manager secrets.
terraform plan should show an aws_kms_key_rotation resource being created (or updated) with enabled = true for the target key and no replacement of the aws_kms_key itself.
