Storage & databases in the cloud
The AWS chapter introduced object, block and file storage and the difference between availability and durability. This chapter goes deeper: how S3 is organised and secured, which storage class to use, lifecycle rules, backups and snapshots, and how the managed databases (RDS, Aurora and DynamoDB) differ, with the AWS CLI commands you'll use daily.
Block, file and object compared
| EBS (block) | EFS (file) | S3 (object) | |
|---|---|---|---|
| Looks like | A raw disk you format and mount | A shared NFS folder | Objects reached by an HTTP API |
| Used by | One EC2 instance at a time (with rare exceptions) | Many Linux instances, containers and Lambdas at once | Anything with credentials and network access |
| Scope | One availability zone | Regional (across AZs) | Regional, data spread across AZs |
| Size | You pick a size and pay for all of it | Grows and shrinks automatically | Practically unlimited |
| Billed for | Provisioned GB (plus extra IOPS/throughput if configured) | GB actually stored | GB stored, requests, data transfer out |
| Typical use | OS disks, databases on EC2 | Shared uploads folder, content for a fleet of web servers | Backups, logs, images, static sites, data lakes |
EBS volume types include gp3 (general-purpose SSD, the usual default), io2 (provisioned IOPS for demanding databases) and st1/sc1 (cheaper hard disks for large sequential data). A volume can only attach to instances in its own AZ; to move it, snapshot it and create a new volume elsewhere. You can grow a volume while it's in use (aws ec2 modify-volume --volume-id vol-0123456789abcdef0 --size 30), then extend the partition and filesystem inside the OS (growpart, then xfs_growfs or resize2fs). Volumes can't be shrunk.
S3: buckets, keys and prefixes
- A bucket is a container created in one region. Its name must be unique across all AWS accounts, 3-63 characters of lowercase letters, numbers, hyphens and dots.
- An object is a file plus metadata, identified by its key, like
images/2026/logo.png. S3 is flat: there are no real folders, and the console just groups keys by the prefix before each/. - Objects can be up to 50 TB (raised from 5 TB in late 2025), but a single upload request is limited to 5 GB; larger files use multipart upload, which the AWS CLI does automatically.
- S3 is strongly consistent: once a write succeeds, every following read sees it.
Storage classes
Each object has a storage class. They trade storage price against retrieval price and speed; the durability design is the same everywhere except the One Zone classes, which keep data in a single AZ.
| Class (API name) | For | Notes |
|---|---|---|
Standard (STANDARD) | Frequently accessed data | The default; no retrieval fee |
Intelligent-Tiering (INTELLIGENT_TIERING) | Unknown or changing access patterns | Moves objects between tiers automatically for a small per-object monitoring fee |
Standard-IA (STANDARD_IA) | Infrequent access, needed instantly | Cheaper storage, retrieval fee, 30-day minimum |
One Zone-IA (ONEZONE_IA) | Re-creatable infrequent data | One AZ only, so data is lost if that AZ is |
Glacier Instant Retrieval (GLACIER_IR) | Archives read a few times a year | Millisecond access, 90-day minimum |
Glacier Flexible Retrieval (GLACIER) | Archives | Restore takes minutes to hours before you can read; 90-day minimum |
Glacier Deep Archive (DEEP_ARCHIVE) | Long-term compliance archives | Cheapest storage; restore takes hours; 180-day minimum |
"Minimum" means you pay for at least that many days even if you delete the object sooner. Infrequent-access classes also bill small objects as if they were 128 KB. Check the S3 pricing page for actual prices in your region.
Versioning and lifecycle rules
With versioning on, overwriting an object keeps the old version, and deleting one just adds a delete marker on top, so mistakes and ransomware-style overwrites are recoverable. Once enabled it can only be suspended, never fully turned off, and every old version is billed. Lifecycle rules keep that under control by moving or deleting objects by age:
# lifecycle.json
{
"Rules": [
{
"ID": "logs-archive-then-expire",
"Filter": { "Prefix": "logs/" },
"Status": "Enabled",
"Transitions": [
{ "Days": 30, "StorageClass": "STANDARD_IA" },
{ "Days": 90, "StorageClass": "GLACIER" }
],
"Expiration": { "Days": 365 },
"NoncurrentVersionExpiration": { "NoncurrentDays": 30 }
},
{
"ID": "clean-up-failed-uploads",
"Filter": { "Prefix": "" },
"Status": "Enabled",
"AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 7 }
}
]
}
$ aws s3api put-bucket-versioning --bucket demee-practice-assets \
--versioning-configuration Status=Enabled
$ aws s3api put-bucket-lifecycle-configuration --bucket demee-practice-assets \
--lifecycle-configuration file://lifecycle.json
The first rule moves anything under logs/ to Standard-IA after 30 days and to Glacier Flexible Retrieval after 90, deletes it after a year, and removes old versions 30 days after they're replaced. The second deletes the invisible parts of multipart uploads that never finished, which are otherwise billed forever.
Keeping buckets private, and sharing safely
New buckets have Block Public Access turned on and ACLs disabled (the "bucket owner enforced" setting), so nothing is public unless you deliberately change that. Access is granted with IAM policies (for your own users and roles) and bucket policies (for other accounts or services). Leave Block Public Access on unless the bucket really must serve the public, and even then prefer CloudFront in front.
To share one private object temporarily, create a presigned URL: a link that carries a signature made with your credentials and works until it expires, with no change to the bucket's settings.
$ aws s3 presign s3://demee-practice-assets/docs/report.pdf --expires-in 3600
https://demee-practice-assets.s3.eu-central-1.amazonaws.com/docs/report.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&...
--expires-in is in seconds (default 3600, maximum 604800, which is 7 days). A URL signed with temporary role credentials stops working when those credentials expire, even if the time you asked for hasn't passed.
Static website hosting
S3 can serve a static site (HTML, CSS, JS, images) directly:
$ aws s3 website s3://demee-practice-site/ --index-document index.html --error-document error.html
$ aws s3 sync ./public s3://demee-practice-site/ --delete
The website endpoint only speaks HTTP and needs the bucket to be publicly readable (Block Public Access off plus a bucket policy allowing s3:GetObject to everyone). The better production setup keeps the bucket private and puts CloudFront in front with Origin Access Control: you get HTTPS with an ACM certificate, your own domain, caching at edge locations, and only CloudFront can read the bucket.
The AWS CLI for S3
$ aws s3 mb s3://demee-practice-assets --region eu-central-1 # make a bucket
$ aws s3 ls # list your buckets
$ aws s3 ls s3://demee-practice-assets/images/ # list one prefix
$ aws s3 ls s3://demee-practice-assets --recursive --human-readable --summarize
$ aws s3 cp report.pdf s3://demee-practice-assets/docs/report.pdf # upload
$ aws s3 cp s3://demee-practice-assets/docs/report.pdf ./report-copy.pdf # download
$ aws s3 cp ./photos s3://demee-practice-assets/photos/ --recursive \
--exclude "*" --include "*.jpg" # only .jpg files
$ aws s3 cp backup.tar.gz s3://demee-practice-assets/backups/ --storage-class STANDARD_IA
$ aws s3 sync ./site s3://demee-practice-assets/site/ --delete --dryrun # preview
$ aws s3 sync ./site s3://demee-practice-assets/site/ --delete # mirror it
$ aws s3 rm s3://demee-practice-assets/docs/report.pdf
$ aws s3 rm s3://demee-practice-assets/tmp/ --recursive
$ aws s3 rb s3://demee-practice-assets --force # delete all objects, then the bucket
cp copies what you name; sync copies only new and changed files, and with --delete also removes files from the destination that no longer exist in the source, so run it with --dryrun first. Filters are applied in order, which is why "exclude everything, then include *.jpg" works. rb --force can't remove old versions, so on a versioned bucket delete those first (or empty the bucket in the console).
Backups and snapshots
- EBS snapshots are point-in-time copies stored regionally (so they survive the loss of an AZ). They're incremental: after the first one, only changed blocks are stored. You can create volumes or AMIs from them and copy them to other regions.
- RDS takes automated backups daily plus transaction logs, allowing point-in-time restore to any second within the retention period (up to 35 days). They are deleted with the database unless you choose to keep them. Manual snapshots stay until you delete them.
- AWS Backup manages schedules and retention for EBS, RDS, DynamoDB, EFS and more in one place.
- A backup you have never restored is a hope, not a backup. Test restores regularly.
$ aws ec2 create-snapshot --volume-id vol-0123456789abcdef0 --description "before upgrade"
$ aws rds create-db-snapshot --db-instance-identifier practice-db \
--db-snapshot-identifier practice-db-before-upgrade
Managed relational databases: RDS and Aurora
RDS runs PostgreSQL, MySQL, MariaDB, Oracle, SQL Server or Db2 for you: AWS installs, patches, backs up and monitors it; you choose the instance size, storage, network and settings. Aurora is AWS's own MySQL- and PostgreSQL-compatible engine with a distributed storage layer that keeps six copies across three AZs; it's usually faster to fail over and to add readers, at a higher price, and Aurora Serverless v2 scales capacity up and down automatically.
$ aws rds create-db-instance --db-instance-identifier practice-db \
--engine postgres --db-instance-class db.t4g.micro --allocated-storage 20 \
--master-username appadmin --manage-master-user-password \
--no-publicly-accessible --storage-encrypted --backup-retention-period 7
--manage-master-user-password makes RDS generate the password and keep it in Secrets Manager. Two features are often confused:
| Multi-AZ | Read replica | |
|---|---|---|
| Purpose | Availability: survive an AZ or instance failure | Scaling reads (reports, read-heavy pages) |
| Replication | Synchronous to a standby in another AZ | Asynchronous, so replicas can lag slightly |
| Serves traffic? | The classic single standby doesn't; it takes over automatically on failure, with the same endpoint | Yes, reads only, on its own endpoint |
| Where | Same region | Same or another region; can be promoted to a standalone database |
Add --multi-az to the create command for the first; aws rds create-db-instance-read-replica --db-instance-identifier practice-db-replica --source-db-instance-identifier practice-db for the second. (RDS also offers a Multi-AZ cluster deployment with two readable standbys.)
DynamoDB
DynamoDB is a serverless key-value and document database: no instances, consistent single-digit-millisecond responses at almost any scale, billed on-demand per request or with provisioned capacity. Every item is found by its partition key, optionally combined with a sort key, and you design the keys around the queries you'll run, because there are no joins and scanning a whole table is slow and expensive.
$ aws dynamodb create-table --table-name Orders \
--attribute-definitions AttributeName=customerId,AttributeType=S AttributeName=orderId,AttributeType=S \
--key-schema AttributeName=customerId,KeyType=HASH AttributeName=orderId,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST
$ aws dynamodb put-item --table-name Orders \
--item '{"customerId": {"S": "c-42"}, "orderId": {"S": "o-1001"}, "total": {"N": "59.90"}}'
$ aws dynamodb query --table-name Orders --key-condition-expression "customerId = :c" \
--expression-attribute-values '{":c": {"S": "c-42"}}'
HASH is the partition key and RANGE the sort key; the query returns all orders for customer c-42. Note the typed JSON format ("S" string, "N" number sent as a string).
SQL or NoSQL?
- Choose SQL (RDS/Aurora) when data is relational (users, orders, products linked together), you need joins, ad-hoc queries and multi-row transactions, or you don't yet know every question you'll ask of the data. It's the safe default for most apps.
- Choose DynamoDB when access patterns are known and simple (get by ID, list by owner), you need huge scale or spiky traffic with no servers to size, or you're building serverless with Lambda.
- Many systems use both, plus S3 for files and ElastiCache for caching. Never store large files inside a database; store them in S3 and keep the key in the database.
Common mistakes
BucketAlreadyExists: someone, in any account, already has that name. Add something unique.AccessDeniedonListBucketbut objects work (or the reverse): bucket ARN vsbucket/*ARN in the policy.sync --deleteto the wrong prefix, or in the wrong direction: it deletes whatever doesn't match the source. Always run it with--dryrunfirst.- Versioned bucket keeps growing after deletes: old versions and delete markers are still billed. Add
NoncurrentVersionExpiration. - Database with public access "just to test": keep it private and connect through the app server or Session Manager port forwarding.