Skip to main content

Triage and Remediation

Remediation

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.

1. Identify Non-Rotating Secrets

  1. Sign in to the AWS Management Console.
  2. Go to Secrets Manager:
    • Services → Secrets Manager.
  3. In Secrets, look for secrets where:
    • Rotation configuration column is “Disabled” or rotation interval is longer than your policy (e.g., >30 days).
You’ll remediate each of these secrets.

2. Enable Rotation for a Secret

Perform this for each non-compliant secret.
  1. In Secrets Manager, click the secret name.
  2. In the secret’s detail page, choose Rotate secret (or Edit rotation if already enabled but not compliant).

2.1 Choose Rotation Strategy

  1. 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).

2.2 Choose or Create Rotation Lambda Function

  1. 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:
      1. Select “Create a new Lambda function”.
      2. Choose the secret type / database type (e.g., RDS, other database, custom).
      3. 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.
  2. Click Next, then Save / Enable rotation.
Secrets Manager will now:
  • 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:
  1. Go to AWS KMS:
    • Services → Key Management ServiceCustomer managed keys.
  2. Click the CMK used by the secret (shown on the secret’s detail page under Encryption key).
  3. Under Key policy:
    • Ensure the IAM role used by the rotation Lambda function has:
      • kms:Encrypt
      • kms:Decrypt
      • kms:GenerateDataKey
      • kms:DescribeKey
  4. Save the key policy if edited.

4. Confirm Rotation Is Working

  1. Back in Secrets Manager, open the secret.
  2. Confirm:
    • Rotation configuration shows Enabled and the correct interval.
    • Secret versions show multiple versions over time (after the first rotation occurs).
  3. 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.
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).

1. Identify the secret(s) that need rotation

Or filter by tag / name pattern as needed.

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:
  1. Create an IAM role for the Lambda with permissions to:
    • secretsmanager:GetSecretValue
    • secretsmanager:PutSecretValue
    • secretsmanager:UpdateSecretVersionStage
    • Any permissions needed to update the target (e.g., RDS, API key, etc.)
Example trust policy file lambda-trust-policy.json:
Create role:
Attach basic Lambda logging and Secrets Manager permissions (adjust ARNs/permissions as needed):
Your secrets-rotation-inline-policy.json should at minimum include:
Then create the Lambda function (ZIP file must contain your rotation code):
You can reuse this Lambda for multiple secrets of the same type.

3. Enable automatic rotation on a secret

Pick a rotation interval (e.g., 30 days). Example for my-app-secret:
Enable rotation:
If rotation Lambda is already attached and you only need to adjust frequency:

4. Verify rotation configuration

You should see 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:
Check status:

This configuration ensures Secrets Manager secrets are automatically rotated on the schedule you define, with encryption handled by KMS.
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:
  1. What you need set up
  2. Creating a Python rotation function (Lambda)
  3. Attaching it to the secret with a rotation schedule

1. Prerequisites

  1. Existing secret in AWS Secrets Manager, encrypted with a KMS CMK (customer‑managed key or AWS managed key).
  2. IAM role for Lambda with at least:
    • secretsmanager:GetSecretValue
    • secretsmanager:PutSecretValue
    • secretsmanager:UpdateSecretVersionStage
    • Any permissions required to change the underlying credentials (e.g., RDS, user store, etc.).
  3. 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):
Below assumes we are rotating credentials stored in the secret that are KMS‑encrypted, which is what the misconfiguration usually refers to.

3. Python Rotation Lambda – Core Template

AWS Secrets Manager expects the Lambda to implement a lambda_handler that supports these steps:
  • createSecret
  • setSecret
  • testSecret
  • finishSecret

3.1. Basic Lambda Skeleton (Python)

Create a new Lambda function with Python 3.x and paste:
Replace the commented update_database_password / test_database_connection bits with the actual logic for your use case (API, DB, etc.).

4. Configure the Lambda for Rotation

  1. Create the Lambda (console or CLI) with the above code.
  2. Attach an IAM role allowing:
    • secretsmanager:GetSecretValue, PutSecretValue, DescribeSecret, UpdateSecretVersionStage
    • Any service‑specific actions (e.g., rds:ModifyDBInstance, or API client permissions).
  3. 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

  1. Go to Secrets Manager → select the secret.
  2. Click Rotate secret.
  3. Choose Use an existing Lambda function and select the Lambda created above.
  4. Set rotation schedule (e.g., every 30 days).
  5. Save.

5.2. Using Python (boto3)

This:
  • Attaches the rotation Lambda.
  • Sets rotation interval (e.g., 30 days).
  • Triggers an immediate rotation cycle if RotateImmediately=True.

If you use a customer‑managed KMS key to encrypt the secret, enable key rotation:
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 setSecret and testSecret functions with concrete code.
Substitute:
  • SECRET_KMS_KEY with the Terraform name you use for the KMS key that encrypts your Secrets Manager secrets.
This change does not force replacement of the KMS key; it toggles rotation in place.Verification: 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.