AWS Learning Journey

Where Should That Password Live? Encryption and Secrets on AWS

Database passwords in environment variables. API keys in config files. Here's how AWS KMS, Secrets Manager, and Parameter Store keep secrets — and your data — actually safe.

11 min read · 2005 words
AWSCloud SecurityEncryptionDevSecOpsSecrets Management
Where Should That Password Live? Encryption and Secrets on AWS cover image

AWS from Zero to Production — Phase 2, Part 4 of 6 · See the full series roadmap


Search any large codebase for password= and you’ll find something you didn’t want to find.

A database password in a .env file that was committed “temporarily.” An API key hard-coded in a test. A connection string in a container image that anyone with pull access can read. These aren’t exotic attacks. They’re the most common way secrets leak.

AWS gives you two things to fix this:

This article covers both — and the key service underneath them: AWS Key Management Service (KMS).

What you’ll walk away with


Two kinds of encryption

Encryption at rest

Protects data where it’s stored: disks, databases, object storage, backups, and snapshots. If someone gets the raw storage, they can’t read it without the key.

On AWS:

Encryption in transit

Protects data as it moves between clients and services, or between services, using TLS.

{
  "Sid": "DenyInsecureTransport",
  "Effect": "Deny",
  "Principal": "*",
  "Action": "s3:*",
  "Resource": [
    "arn:aws:s3:::my-bucket",
    "arn:aws:s3:::my-bucket/*"
  ],
  "Condition": {
    "Bool": { "aws:SecureTransport": "false" }
  }
}

Engineer’s note: Encryption at rest protects against stolen storage. It does not protect against someone with valid permissions reading your data through the API. That’s what IAM and key policies are for.


AWS Key Management Service (KMS)

AWS KMS creates and manages cryptographic keys and controls their use across AWS services.

The key idea: KMS keys never leave KMS unencrypted. You don’t download the key. You ask KMS to encrypt, decrypt, or generate data keys — and KMS checks permissions on every request and logs it in CloudTrail.

Three kinds of KMS keys

When to use a customer managed key

For many learning and non-sensitive workloads, AWS managed keys or service defaults are fine.


Key policies: the gatekeeper for every key

Every KMS key has a key policy — a resource-based policy that’s the primary control over who can use the key.

Here’s what makes KMS different: IAM policies alone can’t grant access to a KMS key. The key policy must allow it — either directly, or by delegating to IAM.

The default key policy includes a statement like this:

{
  "Sid": "EnableIAMPolicies",
  "Effect": "Allow",
  "Principal": { "AWS": "arn:aws:iam::111122223333:root" },
  "Action": "kms:*",
  "Resource": "*"
}

Despite the word root, this doesn’t mean “only the root user.” It means the account — and it allows IAM policies in that account to grant access to the key. Without it, only principals named directly in the key policy can use the key.

Watch out: If you edit a key policy and remove all administrators, you can lock yourself out of the key. Recovering requires contacting AWS Support. Always keep a statement that lets your account or an admin role manage the key.


Envelope encryption

KMS can encrypt small pieces of data directly — up to 4 KB. For anything larger, AWS uses envelope encryption.

How it works

1. Ask KMS for a data key (GenerateDataKey)
      KMS returns: plaintext data key + encrypted copy of the data key
2. Encrypt your data locally with the plaintext data key
3. Throw away the plaintext data key
4. Store the encrypted data next to the encrypted data key

To decrypt:
5. Send the encrypted data key to KMS (Decrypt)
6. Use the returned plaintext data key to decrypt the data locally

Why bother?

You rarely implement this yourself. Services like S3, EBS, and RDS use envelope encryption under the hood whenever you choose a KMS key.


Key rotation

Rotation replaces a key’s cryptographic material over time.

Important: Rotating a KMS key does not re-encrypt existing data. It changes the key material used for new encryption operations.

Deleting keys — carefully

Deleting a KMS key is irreversible and makes everything encrypted under it unreadable. KMS enforces a waiting period of 7 to 30 days before deletion. Prefer disabling a key if you’re unsure.


Where secrets should live

A secret is any credential your application needs: database passwords, API keys, OAuth client secrets, private tokens.

The rule: secrets live in a managed store, are fetched at runtime, and never appear in code, images, or logs.

AWS offers two main options.

AWS Secrets Manager

Purpose-built for secrets:

aws secretsmanager create-secret \
  --name prod/orders-db \
  --secret-string '{"username":"app","password":"REPLACE_ME"}'

aws secretsmanager get-secret-value \
  --secret-id prod/orders-db \
  --query SecretString \
  --output text

Fetching it from Python:

import json
import boto3

client = boto3.client("secretsmanager")
secret = json.loads(
    client.get_secret_value(SecretId="prod/orders-db")["SecretString"]
)

db_user = secret["username"]
db_password = secret["password"]

AWS Systems Manager Parameter Store

A general-purpose configuration store that also handles secrets:

aws ssm put-parameter \
  --name /myapp/prod/api-key \
  --type SecureString \
  --value "REPLACE_ME"

aws ssm get-parameter \
  --name /myapp/prod/api-key \
  --with-decryption \
  --query Parameter.Value \
  --output text

Which one should you use?

Engineer’s note: Many teams use both: Parameter Store for configuration such as feature flags and endpoints, and Secrets Manager for credentials. We’ll connect an ECS service to RDS using Secrets Manager in Phase 5.

Avoid these secret anti-patterns

Command-line caution: Passing a real secret as a command argument, like the examples above, can leave it in your shell history. For real values, read from a file or use an interactive prompt — the REPLACE_ME placeholders here are only for illustration.


Secrets rotation

Rotation changes a secret on a schedule, so a leaked value stops working.

With Secrets Manager:

  1. You enable rotation and set a schedule.
  2. Secrets Manager runs a rotation process — managed rotation, or your Lambda function.
  3. The process creates a new credential, updates it in the database or service, tests it, and marks it AWSCURRENT.
  4. Applications that fetch the secret at runtime automatically pick up the new value.

Make it work: Rotation only helps if your application fetches the secret at runtime and handles a credential change, for example by caching briefly and refreshing on authentication failure. A password copied into a config file at deploy time won’t rotate.


AWS Certificate Manager (ACM) and TLS

TLS certificates prove a server’s identity and enable encrypted HTTPS connections.

AWS Certificate Manager (ACM) provisions and manages them:

Watch out: To use an ACM certificate with CloudFront, request it in the us-east-1 Region, regardless of where your other resources are.

We’ll put an ACM certificate on a load balancer in Phase 3.


Common mistakes


Key takeaways


Quick self-check

Test yourself before moving on:

  1. Can an IAM policy alone grant access to a customer managed KMS key?
  2. Does rotating a KMS key re-encrypt your existing data?
  3. You need a database password that rotates every 30 days. Secrets Manager or Parameter Store?
  4. In which Region must you request an ACM certificate for CloudFront?

Answers

  1. No. The key policy must allow it — directly, or by enabling IAM policies for the account.
  2. No. It changes the key material for new encryption; KMS keeps old material to decrypt existing data.
  3. Secrets Manager, which has built-in rotation.
  4. us-east-1.

What’s next

In Part 5, we zoom out to governance: CloudTrail, AWS Config, GuardDuty, Security Hub, and the tools that tell you who changed what — and whether your accounts are still secure.

Where do your team’s secrets live today? Be honest — share it in the comments.


Share this article

LinkedIn post draft