Home / AWS

AWSRunbookAdvanced

Sev1: Secrets Manager secret deleted and apps fail to fetch credentials

Apps, ECS tasks or Lambda functions fail because a secret was scheduled for deletion. Restore it in one command during the recovery window, or rebuild it and reset the credential if it was force-deleted.

Deepak Bhardwaj6 min read

Applies toAWS Secrets Manager, Amazon ECS, AWS Lambda, Amazon RDS, AWS CLI v2

Symptoms

  • GetSecretValue fails with InvalidRequestException: You can't perform this operation on the secret because it was marked for deletion.
  • Or ResourceNotFoundException: Secrets Manager can't find the specified secret. if it was force-deleted.
  • ECS tasks stop with ResourceInitializationError: unable to pull secrets or registry auth.
  • Lambda functions or pods fail at startup on config load; apps that cached the value keep working until they restart, then fail.
  • Often follows a cleanup script, an IaC change that renamed or removed the secret, or a stack deletion.

A secret deleted normally is scheduled for deletion with a recovery window of 7 to 30 days (30 by default). During that window you can restore it instantly with the same name, ARN, value and versions.

Step 1: find out what state the secret is in (2 minutes)

SECRET=prod/orders/db
aws secretsmanager describe-secret --secret-id $SECRET \
  --query "{arn:ARN,deleted:DeletedDate,rotation:RotationEnabled,kms:KmsKeyId}"

If describe-secret by name returns ResourceNotFoundException, list secrets including those pending deletion:

aws secretsmanager list-secrets --include-planned-deletion \
  --filters Key=name,Values=prod/orders \
  --query "SecretList[].[Name,ARN,DeletedDate]" --output table

Find who deleted it and how:

aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=DeleteSecret \
  --max-results 10 --query "Events[].CloudTrailEvent" --output text \
  | grep -o '"requestParameters":{[^}]*}'

forceDeleteWithoutRecovery: true in the request means it is gone for good. Go to Step 3.

What you see Meaning Go to
DeletedDate set Scheduled for deletion, recoverable Step 2
Not found, CloudTrail shows forceDeleteWithoutRecovery Permanently deleted Step 3
Not found, CloudTrail shows recoveryWindowInDays and the window has passed Permanently deleted Step 3
Secret exists, error is AccessDeniedException Not a deletion: IAM, resource policy or KMS Check the KMS key state and the secret's resource policy
Secret exists, DecryptionFailure KMS key disabled or pending deletion Re-enable the KMS key

If the deletion came from IaC (CloudFormation, Terraform, CDK), pause that pipeline before you restore. Otherwise the next run may delete it again.

Step 2: restore during the recovery window

aws secretsmanager restore-secret --secret-id $SECRET
aws secretsmanager get-secret-value --secret-id $SECRET --query "VersionIdsToStages"

Restoring brings back the same ARN, all versions and staging labels, rotation settings and the resource policy. Most callers recover on their next fetch.

Then force the consumers to fetch again:

# ECS: start fresh tasks so the agent re-reads the secret
aws ecs update-service --cluster prod --service orders-api --force-new-deployment

# Lambda: a config change recycles execution environments
aws lambda update-function-configuration --function-name orders-api \
  --description "secret restore $(date -u +%FT%TZ)"

# Kubernetes with External Secrets Operator or the Secrets Store CSI driver:
kubectl rollout restart deployment/orders-api -n orders

If you use the External Secrets Operator, the ExternalSecret resource may show a sync error until its next refresh interval; check kubectl get externalsecret -n orders.

Step 3: rebuild after permanent deletion

The value is gone. You need to recreate the secret and set a new credential on the system it protects, because nobody should have the old value written down.

Caution

Resetting a database master password breaks every client still using the old one. Change it only once the new secret is ready, and restart consumers right after.

3a: recreate the secret with the same name

NEWPASS=$(aws secretsmanager get-random-password --password-length 32 \
  --exclude-punctuation --query RandomPassword --output text)

aws secretsmanager create-secret --name $SECRET \
  --kms-key-id alias/prod-secrets \
  --secret-string "{\"username\":\"app_user\",\"password\":\"$NEWPASS\",\"host\":\"orders-db.xxxx.eu-west-1.rds.amazonaws.com\",\"port\":5432,\"dbname\":\"orders\"}"

You cannot reuse a name while a secret with that name is still pending deletion. Restore it instead, or wait.

3b: set the credential on the target

For an RDS master user:

aws rds modify-db-instance --db-instance-identifier orders-db \
  --master-user-password "$NEWPASS" --apply-immediately

For an application user, connect as an admin and run ALTER USER app_user WITH PASSWORD '...'; (PostgreSQL) or ALTER USER 'app_user'@'%' IDENTIFIED BY '...'; (MySQL). For API keys or third-party tokens, issue a new key in that system and revoke the old one.

3c: fix references to the old ARN

A recreated secret has the same name but a new ARN. The last six characters are a random suffix. Anything that used the full ARN breaks:

NEWARN=$(aws secretsmanager describe-secret --secret-id $SECRET --query ARN --output text)
echo $NEWARN
# ECS task definitions referencing the old ARN
aws ecs describe-task-definition --task-definition orders-api \
  --query "taskDefinition.containerDefinitions[].secrets"

Update task definitions, Lambda environment variables, IAM policies and CloudFormation dynamic references that use the full ARN. IAM policies that use the -?????? wildcard pattern (arn:aws:secretsmanager:eu-west-1:111122223333:secret:prod/orders/db-??????) keep working.

3d: put rotation and replicas back

aws secretsmanager rotate-secret --secret-id $SECRET \
  --rotation-lambda-arn arn:aws:lambda:eu-west-1:111122223333:function:SecretsManagerRDSPostgreSQLRotation \
  --rotation-rules AutomaticallyAfterDays=30
aws secretsmanager replicate-secret-to-regions --secret-id $SECRET \
  --add-replica-regions Region=eu-central-1,KmsKeyId=alias/prod-secrets
aws secretsmanager put-resource-policy --secret-id $SECRET --resource-policy file://policy.json

rotate-secret with rotation settings starts a rotation straight away. Run it after consumers have picked up the new value and you have confirmed logins work.

Step 4: verify

  • get-secret-value succeeds from the application's role, not just yours.
  • ECS services reach steady state: aws ecs wait services-stable --cluster prod --services orders-api.
  • Database connection errors stop; for RDS check DatabaseConnections recovers.

Prevent it next time

Block force deletion and short recovery windows with an SCP. Secrets Manager provides condition keys for both:

{
  "Effect": "Deny",
  "Action": "secretsmanager:DeleteSecret",
  "Resource": "*",
  "Condition": {
    "Bool": { "secretsmanager:ForceDeleteWithoutRecovery": "true" }
  }
}
  • Add a resource policy on critical secrets that denies DeleteSecret to everyone except a break-glass role.
  • In CloudFormation set DeletionPolicy: Retain on AWS::SecretsManager::Secret; in Terraform use lifecycle { prevent_destroy = true } and leave recovery_window_in_days at its default.
  • Alert on DeleteSecret with an EventBridge rule on CloudTrail events, routed to on-call, so you hear about it while it is still recoverable.
  • Reference secrets by name or with the -?????? pattern in IAM policies, not by full ARN.
  • Use client-side caching (the AWS secrets caching libraries or the Lambda Parameters and Secrets extension) so a short outage of a fetch does not crash running workloads.