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:
- Encryption, so data is useless to anyone who steals the storage or intercepts the traffic.
- Secrets management, so credentials live in one controlled place, are fetched at runtime, and can be rotated without redeploying.
This article covers both — and the key service underneath them: AWS Key Management Service (KMS).
What you’ll walk away with
- 🔐 Encryption at rest vs. in transit — and what AWS encrypts by default
- 🗝️ How AWS KMS works: AWS owned, AWS managed, and customer managed keys
- 📜 Key policies, and why they’re different from every other AWS policy
- ✉️ Envelope encryption, explained simply
- 🔄 Key rotation and secret rotation
- 🧰 Secrets Manager vs. Parameter Store — which one to use
- 📄 AWS Certificate Manager and free TLS certificates
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:
- Amazon S3 encrypts all new objects by default with S3-managed keys (SSE-S3). You can choose KMS keys (SSE-KMS) for more control.
- Amazon EBS volumes can be encrypted, and you can turn on EBS encryption by default per Region in your account.
- Amazon RDS encryption is chosen when you create the database. You can’t simply switch it on for an existing unencrypted instance — you’d copy a snapshot with encryption and restore it.
- DynamoDB encrypts all tables at rest.
Encryption in transit
Protects data as it moves between clients and services, or between services, using TLS.
- AWS service endpoints support HTTPS.
- You’re responsible for TLS on your own endpoints, such as load balancers and APIs.
- You can require it. This bucket policy denies any request that doesn’t use HTTPS:
{
"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
- AWS owned keys: used by AWS services internally, across many accounts. You can’t see, manage, or audit them. Many services use them by default.
- AWS managed keys: created in your account by a service on your behalf, with aliases like
aws/s3oraws/rds. You can see and audit them, but you can’t change their policies, and AWS rotates them every year. - Customer managed keys: created and controlled by you. You set the key policy, enable rotation, grant cross-account access, and schedule deletion. Each has a monthly fee plus request charges.
When to use a customer managed key
- You need to control who can use the key, separately from who can access the data.
- You need cross-account access to encrypted data.
- You need to audit key usage in detail, or disable the key in an emergency.
- Compliance requires you to manage key rotation and lifecycle.
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?
- Performance: large data is encrypted locally, not sent to KMS.
- Security: the KMS key, which encrypts the data keys, never leaves KMS.
- Control: disable the KMS key, and every data key it protects becomes unusable.
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.
- AWS managed keys are rotated automatically every year. You can’t change this.
- Customer managed keys support optional automatic rotation. The default period is 365 days, and you can choose a custom period.
- You can also trigger on-demand rotation for supported keys.
- Rotation keeps the same key ID and ARN. KMS keeps old key material so it can still decrypt older data.
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:
- Automatic rotation: managed rotation for supported databases, or a Lambda function you provide.
- Native integrations: Amazon RDS can manage a database’s master password in Secrets Manager for you.
- Cross-account access through resource-based policies.
- Versioning with staging labels like
AWSCURRENTandAWSPREVIOUS, so rotation doesn’t break running apps. - Pricing: a monthly charge per secret plus API calls. Check current pricing.
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:
- Hierarchical names like
/myapp/prod/db-hostand/myapp/prod/api-key. SecureStringparameters are encrypted with KMS.- Standard parameters have no additional charge; advanced parameters and higher throughput are charged. Check current pricing.
- No built-in rotation — you’d build it yourself.
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?
- Database credentials and anything that must rotate: Secrets Manager.
- Secrets shared across accounts: Secrets Manager.
- App configuration plus a few low-risk secrets: Parameter Store with
SecureString. - Cost-sensitive learning projects: Parameter Store standard parameters.
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
- Secrets in source code, even private repositories
- Secrets baked into container images
- Secrets in plaintext environment variables that show up in console screens and logs
- Secrets printed in CI logs during debugging
- One shared secret used by many apps, so it can never be rotated
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_MEplaceholders here are only for illustration.
Secrets rotation
Rotation changes a secret on a schedule, so a leaked value stops working.
With Secrets Manager:
- You enable rotation and set a schedule.
- Secrets Manager runs a rotation process — managed rotation, or your Lambda function.
- The process creates a new credential, updates it in the database or service, tests it, and marks it
AWSCURRENT. - 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:
- Public certificates for use with integrated services — such as Elastic Load Balancing, Amazon CloudFront, and Amazon API Gateway — are available at no additional charge.
- DNS validation proves you own the domain. With DNS validation, ACM renews certificates automatically before they expire.
- You generally can’t install standard ACM public certificates directly on your own servers; they’re designed to be used through integrated AWS services.
- Private certificates for internal services are available through AWS Private Certificate Authority, at additional cost.
Watch out: To use an ACM certificate with CloudFront, request it in the
us-east-1Region, regardless of where your other resources are.
We’ll put an ACM certificate on a load balancer in Phase 3.
Common mistakes
- Assuming encryption at rest means data is protected from everyone. IAM still decides who can read it.
- Locking yourself out of a KMS key by editing its key policy.
- Scheduling key deletion instead of disabling a key you’re unsure about.
- Storing secrets in code, images, or environment variables.
- Enabling rotation without updating the app to fetch secrets at runtime.
- Requesting a CloudFront certificate outside
us-east-1.
Key takeaways
- Encrypt at rest and in transit; many AWS services now encrypt at rest by default.
- KMS keys never leave KMS; key policies are the primary control over who can use them.
- Envelope encryption lets AWS encrypt large data efficiently and safely.
- Use Secrets Manager for credentials that rotate; Parameter Store for configuration and simple secrets.
- ACM provides free, auto-renewing public certificates for integrated AWS services.
Quick self-check
Test yourself before moving on:
- Can an IAM policy alone grant access to a customer managed KMS key?
- Does rotating a KMS key re-encrypt your existing data?
- You need a database password that rotates every 30 days. Secrets Manager or Parameter Store?
- In which Region must you request an ACM certificate for CloudFront?
Answers
- No. The key policy must allow it — directly, or by enabling IAM policies for the account.
- No. It changes the key material for new encryption; KMS keeps old material to decrypt existing data.
- Secrets Manager, which has built-in rotation.
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.