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.
{ "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.
{ "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.
# CI step: scan the history for secrets and fail if any are foundgitleaks detect --source . --redact --exit-code 1When 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.