Skip to content

AWS Secrets Manager: best practices for storing and rotating secrets

Hardcoded credentials are the flaw that leaks most often. See how to centralize secrets, rotate them and restrict who can read them.

Reading time
9 min
Published

Credentials hardcoded in code, environment variables or container images are among the most common and most expensive flaws. They leak through repositories, logs and image layers, and they give persistent access that is hard to detect. AWS Secrets Manager exists to get these secrets out of the code: it stores, encrypts and rotates credentials and controls who can access them through IAM.

Using Secrets Manager well involves rotation, KMS encryption, least-privilege access and auditing. You also need to migrate the hardcoded credentials that already exist without breaking the application. Once that is done, secrets stop being fixed text scattered around the environment and become managed assets, with every access recorded.

Rotation and encryption

Rotation is what sets Secrets Manager apart the most. With automatic rotation configured, especially for database credentials, a leaked secret stays valid for much less time. Because the application fetches the secret at runtime instead of loading a fixed value, the change happens without the application noticing. Secrets are encrypted with KMS, and using a dedicated key gives you more control over who can decrypt them.

Avoid the anti-pattern of copying the secret into a fixed environment variable at deploy time, which recreates exactly the problem you wanted to solve. Fetch the value at runtime and keep it in memory for as long as needed, with a controlled cache so you do not exceed the API call limits.

Least-privilege access and auditing

Use IAM to restrict who reads each secret, with least privilege: each application accesses only the secrets it uses. Write the permission against the specific ARN of the secret, without a wildcard that covers all of them. A compromised workload then cannot reach the entire vault.

Read policy restricted to a single secret
{  "Effect": "Allow",  "Action": "secretsmanager:GetSecretValue",  "Resource": "arn:aws:secretsmanager:REGION:ACCOUNT:secret:prod/app/db-*"}

The secret's own resource policy adds another layer. With it, you can deny any read that does not come through the expected VPC endpoint, which keeps access to the secret inside the private network. And every access is recorded in CloudTrail, so during an investigation you can find out who read a given secret.

Secret resource policy that denies access from outside the VPC endpoint
{  "Effect": "Deny",  "Principal": "*",  "Action": "secretsmanager:GetSecretValue",  "Resource": "*",  "Condition": {    "StringNotEquals": { "aws:sourceVpce": "vpce-0a1b2c3d" }  }}
  • Configure automatic rotation, especially for database credentials
  • Fetch the secret at runtime instead of fixing it in a variable
  • Use a dedicated KMS key for sensitive secrets
  • Limit reads to the secret's ARN, with no broad wildcard
  • Contain access with a resource policy and a VPC endpoint
  • Audit access to secrets in CloudTrail

Migrating away from hardcoded credentials

Start by finding where hardcoded secrets live: repositories, pipelines, configuration files and images. Move them to Secrets Manager and adjust the application to fetch them at runtime. And do not skip the most important step, which is rotating every secret that has ever been exposed, because moving a secret somewhere else does not undo an earlier leak. Consider any secret that ended up in the repository history compromised.

Detecting leaked secrets

Migrating secrets once does not stop new ones from showing up later. The ongoing defense is to scan commits for credentials before they reach the remote repository and to make the pipeline fail when something slips through. Scanning tools such as gitleaks run locally, in a pre-commit hook, and also in CI, which creates two barriers.

Secret scanning in the pipeline, failing the build when something is found
# CI step: scan the history for secrets and fail if any are foundgitleaks detect --source . --redact --exit-code 1

When the scan finds something, treat the secret as compromised and rotate the value. Removing the line from the code is not enough. Rewriting the history helps clean up the repository, but the value that was already published has to be changed regardless, because someone may have read a copy before it was removed.

Secrets Manager and Parameter Store

Not every piece of configuration data is a secret, and choosing the right service saves money and simplifies control. Parameter Store, part of Systems Manager, stores configuration parameters and, with the SecureString type, also sensitive values encrypted with KMS, without charging per secret. Secrets Manager charges per secret, but it offers native managed rotation, rotation integration with databases and its own resource policy.

A split that works well: configuration parameters and simple, static secrets go in Parameter Store, and credentials that require automatic rotation (database credentials, above all) go in Secrets Manager. In both cases, access over the private route, through a VPC endpoint, keeps secret retrieval off the internet.

Practical checklist

  • Inventory hardcoded secrets in code, pipelines and images
  • Move those secrets to Secrets Manager
  • Read secrets at runtime, without fixing them in environment variables
  • Configure automatic rotation where it applies
  • Encrypt sensitive secrets with a dedicated KMS key
  • Restrict reads to the ARN of each secret, with no broad wildcard
  • Use a resource policy and a VPC endpoint to keep access on the private network
  • Log and audit access to secrets in CloudTrail
  • Scan commits for secrets in pre-commit hooks and in CI
  • Choose between Secrets Manager and Parameter Store based on the need for rotation
  • Rotate every secret that has ever been exposed
  • Consider any secret present in the repository history compromised

Best practices

  • Keep every secret out of the code and the Dockerfile
  • Use rotation to shorten the useful life of a leaked secret
  • Fetch the value at runtime and use a controlled cache
  • Restrict reads to the secret's ARN, without wildcards
  • Scan commits to stop secrets before the push
  • Audit who accesses sensitive secrets
  • Rotate the secret at any suspicion of exposure

Common mistakes

Credentials hardcoded in code or a Dockerfile
They leak through the repository, logs and image layers and give persistent access to whoever finds them.
Read permission with a wildcard over every secret
A compromised workload reaches the entire vault, far beyond what it needed.
Pipeline without secret scanning
New credentials leak in commits again, and nothing stops them.
Copying the secret into a fixed environment variable
The problem Secrets Manager was supposed to solve comes back through the back door.
Broad read access to all secrets
A compromised workload can read the secrets of the entire application.
Not rotating after exposure
Moving the secret does not undo the leak, and the old value remains valid.
Access without auditing
During an investigation, nobody can say who read the secret.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

What is AWS Secrets Manager and what is it for?
AWS Secrets Manager is the AWS service that stores, encrypts and rotates secrets, such as database passwords, API keys and tokens, and controls access to them. With it you take credentials out of code and configuration, where they leak easily, and start treating them as managed assets: encrypted with KMS, accessible only to those with IAM permission and with every access recorded in CloudTrail. The result is an end to hardcoded secrets and a much lower risk of leaks.
Secrets Manager or an environment variable?
A fixed environment variable brings back the risk of a static secret, which can leak in logs and configuration. Secrets Manager stores the value encrypted, controls access through IAM, records every read and handles rotation. The ideal is to fetch the secret from Secrets Manager at runtime and keep it in memory for as long as needed, instead of fixing it in the environment.
Does automatic rotation break the application?
Not when it is configured properly. The application fetches the secret at runtime, so the change happens without it noticing. For databases, the rotation integration updates the credential in a coordinated way. What needs care is testing the rotation and checking that the application re-reads the secret, instead of keeping an already outdated value in its cache indefinitely.
Does moving a leaked secret to Secrets Manager fix the problem?
Not on its own. If the secret has already been exposed (in a repository history, for example), it must be considered compromised and rotated. Storing the value in a vault does not invalidate copies that have already leaked. For that reason, the migration has to go hand in hand with rotating every credential that has passed through an insecure location.
How do I control who accesses each secret?
With least-privilege IAM policies, each workload accesses only the secrets it uses. Combine this with the secret's own resource policy for an extra layer of control. Every access is recorded in CloudTrail, and that is how you audit who read what. What you should avoid is broad access to all secrets.
Should I use Secrets Manager or Parameter Store?
It depends on the need for rotation and on cost. Parameter Store holds configuration and, with the SecureString type, sensitive values encrypted with KMS, without charging per secret, which suits simple, static secrets well. Secrets Manager charges per secret, but it offers native managed rotation, rotation integration with databases and its own resource policy. A common arrangement is to keep configuration and static secrets in Parameter Store and reserve Secrets Manager for credentials that need automatic rotation.
How do I keep secrets from leaking in commits again?
Put secret scanning at two points in the development workflow: a pre-commit hook, which checks locally before anything is pushed, and a CI step, which fails the build if it finds credentials. Tools such as gitleaks cover both cases. When something is detected, treat the secret as compromised and rotate the value, because removing the line does not undo what may already have been read.
Does fetching the secret go over the internet?
By default, the call to Secrets Manager goes to the service's public endpoint. With a VPC endpoint, that traffic stays inside the AWS private network without crossing the internet, and you can also make access to the secret conditional, in the resource policy, on the request coming from that endpoint. For workloads running in a private subnet, this is the recommended way to fetch secrets.

When to bring in specialist help

Finding the secrets scattered across the environment and moving them to centralized management, with rotation, is part of GUARDIASEC's AWS and Cloud Security service. The goal of the work is to eliminate hardcoded credentials and set up least-privilege access to each secret.