Expert · Cloud security and compliance · topic 44 of 50 · 30 min
Encryption, keys and secrets
Explanation
Why encrypt in the cloud?
Encryption turns readable data (plaintext) into scrambled data (ciphertext) that only someone with the right key can read back. Encryption at rest protects stored data: if a disk is lost or a snapshot is shared by mistake, the data is useless without the key. Encryption in transit protects data moving over a network, so nobody in between can read or change it. Many laws and standards, such as PCI DSS for card data, expect both.
Envelope encryption
Encrypting gigabytes directly with a master key held in a central service would be slow, because every byte would travel to that service. Instead, the cloud uses envelope encryption. A data key encrypts your data locally and quickly. Then the master key, which never leaves the key service, encrypts the data key. You store the encrypted data key next to the data. To read, you ask the key service to decrypt the small data key, then decrypt the data locally.
# Ask KMS for a data key: you get it in plaintext (use, then forget) and encrypted (store it)
aws kms generate-data-key --key-id alias/payments-key --key-spec AES_256
# Customer managed key with yearly automatic rotation
aws kms create-key --description "Payments data key"
aws kms create-alias --alias-name alias/payments-key --target-key-id <key-id>
aws kms enable-key-rotation --key-id <key-id>KMS keys: AWS managed or customer managed
| AWS managed key | Customer managed key | |
|---|---|---|
| Example name | aws/s3, aws/ebs | alias/payments-key |
| Who controls the key policy | AWS | You |
| Rotation | Automatic, every year | Optional automatic rotation (yearly by default) or on demand |
| Cost | No monthly key fee | A small monthly fee per key plus requests (check pricing) |
| Use when | Simple default encryption | You must control who can use or disable the key, or need audit separation |
A key policy is the resource policy on a KMS key. It decides who can administer the key and who can use it to encrypt and decrypt. With a customer managed key, a developer who has s3:GetObject but no kms:Decrypt still cannot read the encrypted object. That separation is why regulated companies prefer customer managed keys. Rotation creates new key material; old material is kept so old data can still be decrypted.
Defaults help: since January 2023, Amazon S3 encrypts every new object at rest (SSE-S3). You can turn on EBS encryption by default for each region, and RDS encryption is chosen when you create the database. Azure Key Vault and Google Cloud KMS play the same role on the other clouds.
Encryption in transit: TLS and ACM
TLS (Transport Layer Security) is the protocol behind HTTPS. Use TLS 1.2 or newer everywhere, including between your load balancer and your servers when the data is sensitive. AWS Certificate Manager (ACM) issues free public TLS certificates for services it integrates with, such as load balancers, CloudFront and API Gateway, and renews them automatically. A certificate for CloudFront must be created in us-east-1. You can force HTTPS on a bucket with a policy like this:
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "DenyPlainHTTP",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": ["arn:aws:s3:::upi-reports", "arn:aws:s3:::upi-reports/*"],
"Condition": { "Bool": { "aws:SecureTransport": "false" } }
}]
}Secrets management
A secret is any value that grants access: a database password, an API token, a private key. Never hard-code secrets in source code, Docker images or environment files committed to Git. Put them in a secrets manager, and let the app fetch them at runtime through its role.
- AWS Secrets Manager: stores secrets, encrypts them with KMS, and can rotate them automatically (managed rotation for RDS, or a Lambda function you write). Charged per secret per month.
- AWS Systems Manager Parameter Store: SecureString parameters encrypted with KMS. Standard parameters have no extra charge, but rotation is your job.
- Azure Key Vault: secrets, keys and certificates in one service.
- Google Cloud Secret Manager: versioned secrets with IAM per secret.
aws secretsmanager get-secret-value --secret-id prod/payments/db --query SecretString --output text
aws ssm get-parameter --name /payments/api-token --with-decryptionData classification and backups
Data classification means labelling data by sensitivity, for example Public, Internal, Confidential and Restricted. Payment data, Aadhaar numbers and health records are Restricted. The label decides the controls: Restricted data gets customer managed keys, strict access, extra logging and no copies in test environments. Tag buckets and databases with their class. Backups must be encrypted too, and copies in a separate account protect you if the main account is compromised.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Encryption, keys and secrets · 1/7
Encryption scrambles data so only someone with the key can read it. We encrypt data at rest and data in transit.
Prefer a video by a teacher? Find free videos on “AWS KMS envelope encryption secrets manager explained” on YouTube ↗
In real life: A hospital chain in Hyderabad protects patient records
A hospital chain stores scanned reports and lab results in S3 and patient details in RDS PostgreSQL in ap-south-2 (Hyderabad). Records are classified as Restricted. Each data store uses a customer managed KMS key whose key policy allows decrypt only for the application role and two named admins. The database password lives in Secrets Manager with automatic 30-day rotation. All traffic uses HTTPS with an ACM certificate on the load balancer. Nightly backups are copied to a separate backup account, encrypted with that account's own key. When a developer accidentally pushed a test API token to a public repository, a secret scanner in the CI pipeline flagged it within minutes and the token was revoked the same day.
Practice (0/3 done)
Where should a Lambda function get its database password from?
Write find_secrets(text) that scans a file's text line by line (line numbers start at 1) and returns a list of (line_number, kind) tuples. Kinds, checked in this order on each line: "aws-access-key" if the line contains AKIA followed by exactly 16 capital letters or digits; "password" if it contains password, then optional spaces, "=", optional spaces and a value (any case: PASSWORD= also counts); "private-key" if it contains a header like -----BEGIN PRIVATE KEY----- or -----BEGIN RSA PRIVATE KEY-----. Use the re module.
A developer has s3:GetObject on a bucket encrypted with a customer managed KMS key, but no kms:Decrypt on that key. What happens when they download an object?
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Logging, threat detection and incident responseCloud security and compliance · same module
- Compliance in the cloud, with Indian rulesCloud security and compliance · same module
- Secrets managementDevOps: Git, Docker, Kubernetes and CI/CD · DevSecOps
- Keeping secrets safeBackend Development · Backend security
- HTTP, HTTPS and TLSCyber Security: from zero to SOC analyst · Networking for security
- Symmetric and asymmetric encryptionCyber Security: from zero to SOC analyst · Systems and cryptography
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
In envelope encryption, what does the master key in KMS encrypt?
Answer: The data key
The data key encrypts the data; the master key encrypts the data key and never leaves KMS.
Which statement about Amazon S3 encryption is true for new objects today?
Answer: They are encrypted at rest by default with SSE-S3
Since January 2023, S3 applies SSE-S3 to all new objects by default.
You need a free public TLS certificate for a CloudFront distribution. Where do you request it in ACM?
Answer: us-east-1
CloudFront uses ACM certificates from the us-east-1 region (N. Virginia).
A database password was committed to a public GitHub repository an hour ago. What is the first thing to do?
Answer: Rotate (change) the password immediately
Assume it is already copied. Rotate it first, then clean the history and add secret scanning.
Which service can rotate an RDS database password automatically?
Answer: AWS Secrets Manager
Secrets Manager supports managed rotation for RDS and Lambda-based rotation for other secrets.
You need an audit trail of every use of the encryption key for a bucket. Which option fits?Related
Answer: SSE-KMS
With SSE-KMS, key use is logged in CloudTrail and controlled by a key policy.
Which tool lets you keep an encrypted Kubernetes Secret in Git that only the cluster can decrypt?Related
Answer: Sealed Secrets
Sealed Secrets encrypts with the controller's public key; only the controller in the cluster has the private key.
From Secrets management
You committed a key, then deleted it in the next commit. Is it safe now?Related
Answer: No, git history still contains it, so revoke the key
Git keeps every old version. Treat any committed secret as leaked and revoke it.
From Keeping secrets safe
Which TLS versions should a server allow today?Related
Answer: TLS 1.2 and TLS 1.3
SSL and TLS 1.0/1.1 are deprecated. Use 1.2 and 1.3.
From HTTP, HTTPS and TLS
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Cloud security and compliance” before its exam: Logging, threat detection and incident response, Compliance in the cloud, with Indian rules.
- Module 11 exam: Cloud security and compliance · 14 questions · 20 min, without help. 4 topics of this module still to finish first.
- Then the next module, Certifications and cloud careers. 50 topics left in the course before the final exam.