Skip to content
HoginHogin
Go back

AWS from scratch, part 4: protecting the infrastructure — VPC, KMS, Secrets Manager, AWS Backup and a perimeter that matters

12 мин чтения

The first three parts closed the entrance and the eyes: accounts split by environment, humans through Identity Center, detection from part 3. Part four is about what remains when the entrance does get breached: the network, keys, secrets, and backups. The thesis for the whole text: the perimeter no longer saves you. In a VPC where pods call AWS APIs from private subnets, there is no wall that all traffic crosses. Three things do save you: segmentation, encryption, and a backup that cannot be deleted.

Why now: AWS keeps polishing the Security Reference Architecture — a prescriptive blueprint for defense in layers — while Security Hub is expanding to multicloud and will surface this baseline as a set of controls. It assembles in one evening of Terraform — let’s do it.

Table of contents

Open Table of contents

The perimeter is over — echelons remain

“DMZ and a firewall at the edge” assumed all traffic crosses a boundary. In cloud infrastructure it doesn’t: RDS is called from the same VPC, S3 goes over HTTPS directly, consoles and break-glass from part 2 never touch your NGINX. A boundary that everything crosses doesn’t physically exist — what does exist are the forever-mistakes GuardDuty flags in every other account: 0.0.0.0/0 on port 22, a database in a public subnet, one security group for the whole VPC.

Flat network vs three echelons: segmentation, encryption, undeletable backup

Instead of a perimeter — three echelons, each surviving the compromise of the next:

VPC: three tiers and SG references instead of a CIDR mesh

Subnets are split by traffic direction, not “however it happened”:

The key technique isn’t subnets — it’s SG references. A security group is stateful, and a rule can point at another SG instead of a CIDR: “db accepts 5432 from sg/app”. CIDR rules rot: the subnet moved — the rule silently stopped matching reality; a reference holds as long as the SG lives.

Three subnet tiers, traffic only flows down via SG references

NACLs are stateless and can’t see instances: their niche is explicit denies (“this range doesn’t go into this subnet”), not the main policy.

Anti-patterns: 0.0.0.0/0 on 22/5432 (“temporary, for debugging” — lives for years), RDS in a public subnet, “one SG for everything” — the sneakiest one: the rules formally exist, segmentation is factually zero. SSH goes through SSM Session Manager from part 2; port 22 stays closed.

KMS: the key as the environment boundary

AWS-managed keys (aws/rds, aws/ebs) are free and on by default almost everywhere, but they have no policy and no audit of your own. Customer-managed (CMK) costs about a dollar a month plus cents per request and gives you a key policy separate from IAM, CloudTrail on every key operation, a 7–30 day deletion window — and most importantly, the right to revoke access to the data without touching IAM.

The environment rule: as many keys as environments — prod/data, stage/data, dev/data. One shared key nullifies the account separation from part 1: leaked dev credentials read prod data.

Encryption by default is turned on once and forever:

Secrets: Secrets Manager vs Parameter Store

SSM Parameter Store with SecureString is free and encrypted with your KMS key, but it has no rotation: the password gets changed by a script “eventually” — meaning never. Secrets Manager at $0.40/secret/month adds the main thing — rotation on a Lambda schedule: for RDS it’s managed, no code of yours; for the rest there’s a ready function template.

Why not in Terraform: an aws_secretsmanager_secret_version with the password puts the value in plain text into the state and its history, and CI sees the secret on every plan; after 30 days rotation desyncs the value from the state anyway. The right way: Terraform creates the secret container and the rotation config, the value is placed once, and rotation manages it from there.

The boundary: database passwords and API keys with a lifespan — Secrets Manager with rotation; feature flags and non-secrets — plaintext parameters.

AWS Backup + Vault Lock: a backup you cannot delete

AWS Backup is one plan over RDS, EBS, EFS, and DynamoDB: schedules, lifecycle, resource selection by tag (backup=daily). By itself it isn’t protection: a recovery point in the same account is deleted by the same admin.

The protection is Vault Lock: a backup vault has a WORM mode, and in compliance mode after a 72-hour grace period the policy is immutable — neither admin nor root can shorten retention or delete a recovery point until the term expires.

Ransomware wipes prod and ordinary backups; Vault Lock holds the last echelon

The ransomware scenario this is all for: the admin key leaks → EC2, RDS, S3 wiped → ordinary snapshots and a backup account without lock get cleaned by the same key → while recovery points under Vault Lock survive. Hence two conditions: the vault in a separate account (cross-account backup — otherwise the compromised account owns its own backups) and the lock enabled in advance, not after the first incident.

Honest numbers: RPO equals the plan frequency (daily — a day), RTO is hours of restore. That’s the difference between an incident and the end of the company.

A perimeter that matters: Network Firewall, WAF, Shield

The question isn’t “do we need a firewall” but “what traffic actually crosses a boundary”:

One criterion: the tool’s bill is less than an hour of downtime and there is traffic it actually filters. Otherwise it’s security theater.

One comparison table

“However it happened”The baseline from this part
Networkone VPC, all publicthree tiers, data has no IGW route
RulesCIDR mesh in SGsSG references: app ← alb, db ← app
SSH0.0.0.0/0 “temporarily”SSM Session Manager, 22 closed
Encryptionaws/* defaultsCMK per environment: EBS, RDS, S3
Secretsin Terraform and CI varsSecrets Manager + rotation
Backupsnapshots by handtag-based plan + Vault Lock (WORM)

What you need to assemble the baseline in one evening

Preconditions: parts 1–3 done — accounts per environment, Identity Center, GuardDuty; Terraform state already in S3.

1. A VPC with three tiers and SG references:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 6"

  name = "prod"
  cidr = "10.20.0.0/16"
  azs  = ["eu-central-1a", "eu-central-1b"]

  public_subnets   = ["10.20.0.0/24", "10.20.1.0/24"]    # ALB, NAT
  private_subnets  = ["10.20.8.0/22", "10.20.12.0/22"]   # app/EKS
  database_subnets = ["10.20.64.0/22", "10.20.68.0/22"]  # no IGW route

  create_database_subnet_group = true
}

resource "aws_security_group" "alb" {
  name   = "prod-alb"
  vpc_id = module.vpc.vpc_id

  ingress { # the only legitimate 0.0.0.0/0
    description = "https from the world"
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_security_group" "app" {
  name   = "prod-app"
  vpc_id = module.vpc.vpc_id

  ingress { # a reference to an SG, not a CIDR
    description     = "http from the ALB"
    from_port       = 8080
    to_port         = 8080
    protocol        = "tcp"
    security_groups = [aws_security_group.alb.id]
  }
}

resource "aws_security_group" "db" {
  name   = "prod-db"
  vpc_id = module.vpc.vpc_id

  ingress {
    description     = "postgres from the app layer only"
    from_port       = 5432
    to_port         = 5432
    protocol        = "tcp"
    security_groups = [aws_security_group.app.id]
  }
}

2. A CMK per environment and RDS on it — encrypted at creation:

resource "aws_kms_key" "data" {
  description             = "prod/data: RDS, EBS, S3"
  deletion_window_in_days = 30
}

resource "aws_db_instance" "pg" {
  identifier     = "prod-pg"
  engine         = "postgres"
  instance_class = "db.t4g.medium"

  storage_encrypted = true
  kms_key_id        = aws_kms_key.data.arn

  db_subnet_group_name   = module.vpc.database_subnet_group_name
  vpc_security_group_ids = [aws_security_group.db.id]
  publicly_accessible    = false
}

3. A secret with rotation — Terraform creates the container, the value goes in once via put-secret-value, rotation via rotate-secret with the managed Lambda for RDS (AutomaticallyAfterDays=30):

resource "aws_secretsmanager_secret" "db" {
  name       = "prod/db/pg"
  kms_key_id = aws_kms_key.data.arn
}

4. A backup vault with Vault Lock and a tag-based plan:

resource "aws_backup_vault" "locked" {
  name       = "prod-locked"
  kms_key_id = aws_kms_key.data.arn
}

resource "aws_backup_vault_lock_configuration" "worm" {
  backup_vault_name   = aws_backup_vault.locked.name
  min_retention_days  = 35
  max_retention_days  = 365
  changeable_for_days = 3 # compliance mode: 72h grace — then forever
}

resource "aws_backup_plan" "daily" {
  name = "prod-daily"

  rule {
    rule_name         = "daily-35d"
    target_vault_name = aws_backup_vault.locked.name
    schedule          = "cron(0 3 * * ? *)"

    lifecycle { delete_after = 35 }
  }
}

resource "aws_backup_selection" "tagged" {
  plan_id        = aws_backup_plan.daily.id
  selection_name = "prod-by-tag"

  selection_tag {
    type  = "STRINGEQUALS"
    key   = "backup"
    value = "daily"
  }
}

The vault lives in a separate backup account (cross-account via an organization policy); the same Terraform tags resources with backup=daily.

How to verify

One command per echelon:

# segmentation: everything open to the world is only 443/80 on the ALB
aws ec2 describe-security-groups \
  --filters Name=ip-permission.cidr,Values=0.0.0.0/0 \
  --query 'SecurityGroups[].{sg:GroupName,ports:IpPermissions[].[FromPort,ToPort]}'

# encryption: RDS encrypted with your CMK and not public
aws rds describe-db-instances --db-instance-identifier prod-pg \
  --query 'DBInstances[].{enc:StorageEncrypted,kms:KmsKeyId,pub:PubliclyAccessible}'

# WORM: deleting a recovery point must fail with AccessDenied
aws backup delete-recovery-point --backup-vault-name prod-locked \
  --recovery-point-arn "$RP_ARN"

# path to the database: reachable from the app ENI, not from others
aws ec2 create-network-insights-path --source "$APP_ENI" \
  --destination "$DB_ENI" --protocol tcp --destination-port 5432

AccessDenied on delete-recovery-point is the main check: the last echelon holds even against the owner of all the keys.

Pitfalls

What’s next: the series plan

Part 5 — the workload: EKS and RDS on this baseline: IRSA from part 2, runtime detection from part 3, network policies on top of SG references.

Bottom line

The perimeter no longer saves you: encryption, segmentation, and a backup that cannot be deleted do. The trade: CMK and Secrets Manager are single-digit dollars a month on a small estate, Vault Lock costs nothing — it’s a policy, not a service; in return, the “ransomware with an admin key” scenario turns from the end of the company into an incident with an RTO of hours, and Security Hub surfaces baseline drift before it becomes a GuardDuty finding.


Share this post:

Next Post
AWS from scratch, part 3: logs and detection — CloudTrail, GuardDuty, Security Hub: see the intrusion before your customers do