Skip to content
HoginHogin
Go back

AWS from scratch, part 5: putting it all together — a secure baseline EKS + RDS Postgres setup

12 мин чтения

Four parts closed the accounts (pt. 1), the people and services (pt. 2), the vision (pt. 3), and the network with the data (pt. 4). What’s left is the workload — an EKS cluster and an RDS Postgres database that make all of it come alive. The thesis of this finale: a “basic” setup and a “secure” setup are not two different projects but the same Terraform module, if you assemble it from the details of the first four parts.

Why now: org-wide GuardDuty with EKS detections and centralized posture management makes “secure by default” reachable without an enterprise budget: Kubernetes detections are an organization-wide checkbox, not a quarterly project.

Table of contents

Open Table of contents

What’s already built, and what’s missing

On the foundation of parts 1–4 the workload assembles in an evening — and that’s exactly where it usually breaks: EKS gets stood up “by the tutorial” — public endpoint on 0.0.0.0/0, nodes wherever they landed, AWS keys in variables. The cluster works, and the baseline gets retrofitted afterwards, as a migration with downtime. The right order: rules first, then terraform apply.

The assembled baseline: ALB in public, EKS in private subnets, RDS in the data tier; IRSA and ESO outbound; GuardDuty and the backup vault watching

EKS: a cluster that doesn’t stick out

Three settings separate this cluster from the tutorial one:

The cluster’s etcd secrets are encrypted with the same CMK — cluster_encryption_config in the module.

IRSA: pods without keys

IRSA — Roles for Service Accounts — carries the part-2 model over from people to pods. The cluster publishes an OIDC provider; a service account annotated with eks.amazonaws.com/role-arn gets a projected token and exchanges it at STS for temporary credentials. Zero long-lived keys: a leaked manifest is worth nothing within hours.

A static key in a Secret lives for years; IRSA exchanges a projected token for temporary creds via STS

Permissions work like the permission sets in part 2: a role bound to namespace:serviceaccount, a policy scoped to one resource — s3:GetObject on one bucket, not * on the account. AdministratorAccess on a service account is formally “IRSA”, but in substance it’s the same eternal key, just fashionable.

RDS Postgres: a database you can’t reach from outside

The carcass comes entirely from part 4: database subnets with no IGW route, publicly_accessible = false, 5432 open only from the EKS nodes’ SG (a reference, not a CIDR), storage_encrypted at creation under the prod/data CMK.

The new choice is how the pod authenticates to the database:

Rule of thumb: connection pool → a rotated secret; no pool → IAM auth.

Secrets: External Secrets Operator

We covered distributing secrets into a cluster in detail in the ESO article — here it falls into place: the operator under its own IRSA role, a ClusterSecretStore pointed at Secrets Manager, an ExternalSecret syncing prod/db/pg into a Kubernetes Secret. Rotation reaches the pods on its own — ESO polls the source on refreshInterval.

Observability and backups: parts 3 and 4 on autopilot

Almost nothing new here — and that’s the series’ main point. GuardDuty EKS: the org-wide GuardDuty from part 3 already watches the cluster — control-plane audit-log analysis is enabled org-wide, and runtime protection installs as the aws-guardduty-agent managed add-on, an agent on every node; a finding goes to the same EventBridge → Telegram pipeline. Audit logs of the control plane go to CloudWatch next to CloudTrail — one investigation timeline. Backups: RDS automated backups with a 7–35 day retention give PITR down to 5 minutes (“dropped a table” rolls back in minutes), while the backup=daily tag from part 4 attaches the database to the plan and a copy lands in the vault with Vault Lock in a separate account (“wiped everything” rolls back in hours). Two scenarios — two mechanisms.

The comparison in one table

“Stood up by a tutorial”The series baseline
EKS APIpublic, 0.0.0.0/0private-only, access from the VPC
Nodeswherever they landed, often publicprivate subnets + NAT
Pod permissions on AWSstatic keys in a Secretan IRSA role per service account
Pod privilegesunrestrictedPSS restricted in app namespaces
Databasepublic subnet, password in envprivate + SG from EKS + CMK + rotation
Secretsin git and CI variablesSecrets Manager → ESO
DetectionnoneGuardDuty EKS: audit + runtime

What you need to assemble the baseline

Preconditions: parts 1–4 closed, Terraform state in S3, kubectl and the AWS CLI configured. Region eu-central-1, two AZs.

1. The VPC from part 4 and the CMK — same as there, in short (aws_kms_key.data is the prod/data CMK from part 4):

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"]
  private_subnets  = ["10.20.8.0/22", "10.20.12.0/22"]   # nodes land here
  database_subnets = ["10.20.64.0/22", "10.20.68.0/22"]  # no IGW route

  create_database_subnet_group = true
}

2. EKS — private endpoint, nodes in private, the GuardDuty agent:

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "~> 21"

  name       = "prod"
  vpc_id     = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets # private only

  # the control plane doesn't stick out into the internet
  endpoint_private_access = true
  endpoint_public_access  = false

  # access via the Identity Center role (pt. 2)
  enable_cluster_creator_admin_permissions = true

  # etcd secrets under the CMK (pt. 4)
  cluster_encryption_config = {
    provider_key_arn = aws_kms_key.data.arn
    resources        = ["secrets"]
  }

  cluster_addons = {
    coredns             = {}
    kube-proxy          = {}
    vpc-cni             = {}
    aws-guardduty-agent = {} # runtime detection (pt. 3)
  }

  eks_managed_node_groups = {
    apps = {
      instance_types = ["m7g.large"]
      min_size       = 2
      max_size       = 4
      desired_size   = 2
      subnets        = module.vpc.private_subnets
    }
  }
}

3. The database SG — 5432 from EKS only — and RDS itself:

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

  ingress { # a reference to the nodes' SG, not a CIDR (pt. 4)
    description     = "postgres from EKS nodes only"
    from_port       = 5432
    to_port         = 5432
    protocol        = "tcp"
    security_groups = [module.eks.node_security_group_id]
  }
}

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

  db_name = "app"
  # the password without manual values: RDS puts it
  # into Secrets Manager and rotates it itself (pt. 4)
  manage_master_user_password = true

  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

  backup_retention_period = 14 # PITR down to 5 minutes
  deletion_protection     = true
}

4. An IRSA role for one service — reading a single bucket:

module "irsa_app" {
  source    = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
  role_name = "prod-app-s3"

  role_policy_arns = { s3 = aws_iam_policy.app_s3.arn }

  oidc_providers = {
    main = {
      provider_arn               = module.eks.oidc_provider_arn
      namespace_service_accounts = ["apps:app"] # this SA only
    }
  }
}

resource "aws_iam_policy" "app_s3" {
  name = "prod-app-s3"
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect   = "Allow"
      Action   = ["s3:GetObject"]
      Resource = "arn:aws:s3:::prod-uploads/*" # one bucket, one action
    }]
  })
}

And in the cluster — the annotation on a service account and PSS on the namespace:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: app
  namespace: apps
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::ACCOUNT_ID:role/prod-app-s3
kubectl create namespace apps
kubectl label namespace apps pod-security.kubernetes.io/enforce=restricted

5. ESO and the SecretStore — per the ESO article.

How to verify

One command per layer:

# the API doesn't stick out: public is off
aws eks describe-cluster --name prod \
  --query 'cluster.resourcesVpcConfig.[endpointPublicAccess,endpointPrivateAccess]'

# IRSA: the pod sees its service-account role, creds are temporary
kubectl exec -n apps deploy/app -- aws sts get-caller-identity

# PSS: the namespace actually enforces restricted
kubectl get ns apps --show-labels | grep 'enforce=restricted'

# the path to the database: a connection from inside the cluster works
kubectl run pg-check --rm -it --image=postgres:18-alpine -- \
  psql "host=prod-pg.abcdefgh.eu-central-1.rds.amazonaws.com \
  user=app dbname=app sslmode=require"

# the GuardDuty runtime agent runs on the nodes; backups — retention ≥ 7
kubectl get daemonset -A | grep guardduty
aws rds describe-db-instances --db-instance-identifier prod-pg \
  --query 'DBInstances[].BackupRetentionPeriod'

get-caller-identity from the pod answering with the prod-app-s3 role is the key check: there are no keys in the cluster — and none are needed.

The honest monthly bill

A minimal production setup, us-east-1, on-demand, rounded (eu-central-1 comes out 5–10% higher):

ItemWhat$/mo
EKS control plane$0.10/hour73
Nodes2 × m7g.large≈106
RDS Postgresdb.t4g.medium single-AZ + 100 GB gp3≈72
ALB+ LCU≈20
NAT Gateway1 gateway + traffic≈35
KMS CMK1 key1
Secrets Manager2 secrets1
GuardDutyaudit ≈$1.60/M events + runtime $1.50/vCPU × 4≈10
CloudWatchaudit logs, metrics≈10
Total≈330

What can be trimmed: nodes on Spot (60–70% off), the database a size smaller, dev without the runtime agent and with a single AZ. A Multi-AZ database doubles the RDS line — but that’s availability, not security. What can’t be trimmed, because it’s pennies: the $1 CMK, rotation at $0.40 a secret, Vault Lock which is free outright — a policy, not a service. All the “security” in this bill is a rounding error; the expensive lines are compute and the database — exactly what you already pay for in an unprotected setup.

The series checklist

Five layers: account → people → vision → data & network → workload; build bottom-up

Audit your setup bottom-up:

If the ticks stand across all five — you don’t have “a secure setup on top of a basic one”; you have one setup that is simply built this way.

Pitfalls

Bottom line

A “basic setup” and a “secure setup” in AWS are the same thing if you assemble it from the details of the first four parts: private subnets and SG references cost nothing, a CMK and secret rotation are dollars a month, IRSA is literally cheaper than static keys, and Vault Lock is a free policy. The trade: one evening of Terraform now against a week of rework later — moving nodes into private subnets or encrypting the database “eventually” is a migration with downtime, not a checkbox. The series is closed; from here the setup drifts, and what keeps it in line is the SCPs from part 1, the detections from part 3, and the checklist above.


Share this post:

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