Skip to content
EgyKode
04 · Infrastructure as CodeLab 26 / 59
Guided labterraform

Amazon RDS PostgreSQL & AWS Secrets Manager Integration

Run a Multi-AZ database that survives losing an availability zone, with a password no human ever types.

Time
31 min
Level
Intermediate
Objectives
4 objectives
Cost
Low cost

Where this fits in the platform

This lab adds

  • A managed database whose password nobody has ever seen

Before you start

CostLow cost

A `db.t3.micro` RDS instance is free for 12 months on a new account and ~$13/month after. AWS Secrets Manager is $0.40 per secret per month with no free tier — small, but it does not stop on its own.

Nothing to pay in the browser. Open the terminal runs this against a simulated cloud — the same API calls and the same commands, with no account and no bill. The figure above applies only if you build it in your own.

How to clean up

The scenario#

The database password is in terraform.tfvars, which is in Git. The instance is single-AZ, so a zone failure is an outage of unknown length, and publicly_accessible is true because that was how somebody connected once from a laptop.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

A password nobody types

Step 1 of 4

What you are building#

text
   private subnet 1a          private subnet 1b
  ┌──────────────────┐       ┌──────────────────┐
  │   RDS primary    │<=====>│   standby        │   synchronous replication
  └──────────────────┘       └──────────────────┘
          ^                            ^
          └──── one DNS endpoint ──────┘   failover swaps what it points to

Multi-AZ is availability, not scale. The standby serves no reads and cannot be connected to. It exists so that losing an availability zone costs you 60–120 seconds of failover instead of a restore from backup. If you want read scaling, that is a read replica, and it is a different feature.

It also roughly doubles the instance cost, which is a trade-off worth stating rather than discovering.


Build it#

Verify it worked#

Terminal
# Multi-AZ, private, encrypted
aws rds describe-db-instances --db-instance-identifier platform \
  --query 'DBInstances[0].{multiAZ:MultiAZ,public:PubliclyAccessible,enc:StorageEncrypted,az:AvailabilityZone,standby:SecondaryAvailabilityZone}'
 
# The endpoint does not resolve to anything public
dig +short "$(terraform output -raw db_endpoint)"     # a 10.x address
 
# From OUTSIDE the VPC — this must fail
nc -zv -w5 "$(terraform output -raw db_endpoint)" 5432
 
# From a pod or instance INSIDE — this must work
kubectl run pg --rm -it --image=postgres:16-alpine --restart=Never -- \
  psql "postgresql://platform_admin:$(aws secretsmanager get-secret-value \
    --secret-id platform/rds/credentials --query SecretString --output text \
    | jq -r .password)@<endpoint>:5432/platform" -c "SELECT version();"
 
# The password is nowhere in your source
grep -ri "password" --include="*.tf" --include="*.tfvars" . | grep -v random_password

The pair of connection tests is the point. One proves it works; the other proves the isolation is real.


Clean up#

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
aws rds modify-db-instance --db-instance-identifier platform \
  --no-deletion-protection --apply-immediately
terraform destroy -auto-approve
aws secretsmanager delete-secret --secret-id platform/rds/credentials \
  --force-delete-without-recovery
aws rds describe-db-snapshots --query 'DBSnapshots[].DBSnapshotIdentifier'

Cost of this lab: Billable. db.t4g.micro Multi-AZ is roughly $0.05/hour — about $30/month if left running, twice the single-AZ price. Snapshots bill separately after the instance is gone, so check the last command.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 26 of 59 on the project path

Jenkins EC2 Instance, S3 Backend & AWS Backup VaultMove Terraform state off your laptop into locked remote storage, and put the build server under a backup plan.23 minIntermediate

Previous: Amazon ECR Container Registry & S3 Storage Buckets