| 1 | # Rails behind the CARLOS edge (Classic/Pro staging PoC) Implementation Plan |
| 2 | |
| 3 | > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. |
| 4 | |
| 5 | **Goal:** Serve Tito Classic and Tito Pro staging — full process tree, real staging dependencies — at `classic.platform.tito.io` / `pro.platform.tito.io` through the CARLOS edge, from two tofu-provisioned boxes running the Buildkite amd64 images. |
| 6 | |
| 7 | **Architecture:** Two x86 EC2 boxes in the `tito-tito-staging` VPC (native SG access to the staging Aurora/Valkey/Memcached), a new default↔staging VPC peering with `/32` routes for just the two box IPs (the default VPC already routes all of `10.0.0.0/16` to the *production* peering, so only host routes are safe), docker + systemd units running the k8s images, env from SSM Parameter Store, deploys over SSM RunCommand, and two `carlos add -kind service -addr` routes on the IE edge. |
| 8 | |
| 9 | **Tech Stack:** OpenTofu (`platform-infrastructure/tito/`), cloud-init, systemd, docker, AWS SSM, the `carlos` CLI on the IE box. |
| 10 | |
| 11 | **Spec:** `docs/superpowers/specs/2026-08-11-rails-behind-carlos-edge-poc-design.md` (this repo). |
| 12 | |
| 13 | ## Global Constraints |
| 14 | |
| 15 | - AWS account `665106592142`, region `eu-west-1`, CLI profile `titogo`; tofu state `s3://carlos-tfstate-665106592142/tito/`. |
| 16 | - **Targeted plans/applies only** in `platform-infrastructure/tito/` — the tree carries drift from open PRs and a full apply would strip live tcp/25 SMTP ingress (recorded in `tito/object-store.tf`'s commit message). |
| 17 | - No SSH anywhere. All box operation via SSM (`aws ssm send-command` / session-manager). |
| 18 | - Images are the Buildkite amd64 builds, **unmodified** — no rebuilds. |
| 19 | - Secrets never in tofu, the repo, or terminal output — SSM Parameter Store `SecureString` only. |
| 20 | - Never set `-hibernate` on the edge routes; the edge registry is `/data/registry.db`, written by `carlos add` run **as user `carlos`** on the IE box. |
| 21 | - Staging only is applied; production tofu is written gated off (`rails_poc_prod_enabled = false`) and NOT applied. |
| 22 | - Both repos use worktree discipline: infra work on a branch in a `platform-infrastructure` worktree; this plan/spec live on branch `rails-behind-edge-poc-spec` in `platform`. |
| 23 | |
| 24 | ## Facts (preflighted 2026-08-11 — verify only if something disagrees) |
| 25 | |
| 26 | | Fact | Value | |
| 27 | |---|---| |
| 28 | | Default VPC (edge lives here) | `vpc-02416de3913753989`, `172.31.0.0/16`, main rtb `rtb-08863e575439a6377` | |
| 29 | | IE edge box | `i-025a0b70f3d93304d`, private IP `172.31.4.137`, SG `carlos-tito-ie` (`module.host_ie.security_group_id`) | |
| 30 | | Staging VPC | `vpc-03be5678d08549203` (`tito-tito-staging`), `10.0.0.0/16`; subnets `10.0.0.0/23`, `10.0.2.0/23`, `10.0.4.0/23`; igw rtb `rtb-041f57e000fb5797e`, main rtb `rtb-0a01ed1a14d1a52ce` | |
| 31 | | Production VPC | `vpc-90dffef5`, also `10.0.0.0/16`; **existing peering** `pcx-056b55b69cbf89143`; default VPC routes `10.0.0.0/16 → pcx-056b…` already | |
| 32 | | Classic staging DB | `tito-classic-mysql-staging-writer.cefg7n9rzaz0.eu-west-1.rds.amazonaws.com` (aurora-**mysql**, 3306) | |
| 33 | | Pro staging DB | `tito-pro-postgres-staging-writer.cefg7n9rzaz0.eu-west-1.rds.amazonaws.com` (aurora-**postgresql**, 5432) | |
| 34 | | Staging caches | `tito-classic-memcached-staging` (11211), `tito-classic-redis-staging-001` (valkey, 6379), `tito-pro-memcached-staging`, `tito-pro-redis-staging-001` | |
| 35 | | Edge env | `PLATFORM_APPS_DOMAIN=platform.tito.io`, `CARLOS_WILDCARD_DOMAINS=go.tito.io` → `classic.`/`pro.platform.tito.io` get per-host HTTP-01 certs automatically once routed | |
| 36 | | DNS | `*.platform.tito.io` A → IE EIP already live (`tito/dns.tf`) | |
| 37 | |
| 38 | **CIDR overlap rule:** staging subnets overlap production's `10.0.0.x–10.0.5.x` subnets exactly. The default VPC may only ever get `/32` routes toward staging, at addresses verified to have **no ENI in the production VPC** (that check is Task 1). |
| 39 | |
| 40 | --- |
| 41 | |
| 42 | ### Task 1: Discovery — record the execution facts the tofu needs |
| 43 | |
| 44 | Read-only. Everything lands in `tito/rails-poc-facts.md` in the infra worktree so later tasks (and the prod apply later) never re-derive it. |
| 45 | |
| 46 | **Files:** |
| 47 | - Create: `platform-infrastructure/tito/rails-poc-facts.md` |
| 48 | |
| 49 | **Interfaces:** |
| 50 | - Produces: chosen box IPs (`classic_ip`, `pro_ip`), image refs (`classic_image`, `pro_image`), process commands (`WEB_COMMAND`, `WORKER_COMMAND`, `CLOCK_COMMAND` per app), web port + health path per app, staging DB/cache security-group IDs, box subnet ID. Task 2/3/6 consume these verbatim. |
| 51 | |
| 52 | - [ ] **Step 1: Create the infra worktree** |
| 53 | |
| 54 | ```bash |
| 55 | cd ~/github.com/carlosframework/platform-infrastructure |
| 56 | git fetch origin |
| 57 | git worktree add .claude/worktrees/rails-poc -b rails-poc origin/main |
| 58 | cd .claude/worktrees/rails-poc |
| 59 | ``` |
| 60 | |
| 61 | - [ ] **Step 2: Find where Buildkite pushes the Classic/Pro images** |
| 62 | |
| 63 | ```bash |
| 64 | aws --profile titogo --region eu-west-1 ecr describe-repositories \ |
| 65 | --query 'repositories[].repositoryUri' --output table | grep -i -E 'classic|pro|tito' |
| 66 | ``` |
| 67 | |
| 68 | If nothing matches, the images live outside ECR — ask Paul where Buildkite pushes (and how the boxes should authenticate); record the answer. Record the full image URI and the tag currently deployed to k8s staging for each app. |
| 69 | |
| 70 | - [ ] **Step 3: Record each app's process tree from the k8s staging manifests** |
| 71 | |
| 72 | Requires kubectl access to the staging cluster (Paul has it; if the session doesn't, ask Paul to paste the output): |
| 73 | |
| 74 | ```bash |
| 75 | kubectl get deploy -n <staging-namespace> -o jsonpath='{range .items[*]}{.metadata.name}{" cmd: "}{.spec.template.spec.containers[0].command}{" "}{.spec.template.spec.containers[0].args}{" port: "}{.spec.template.spec.containers[0].ports}{"\n"}{end}' |
| 76 | ``` |
| 77 | |
| 78 | Record per app: the web command + container port, worker command(s), clock/cron command (if any), and the HTTP health path the k8s probes use (`.livenessProbe.httpGet.path`). |
| 79 | |
| 80 | - [ ] **Step 4: Record the staging DB/cache security groups** |
| 81 | |
| 82 | ```bash |
| 83 | aws --profile titogo --region eu-west-1 rds describe-db-clusters \ |
| 84 | --query 'DBClusters[?contains(DBClusterIdentifier,`staging`)].{id:DBClusterIdentifier,sg:VpcSecurityGroups[].VpcSecurityGroupId}' --output json |
| 85 | aws --profile titogo --region eu-west-1 elasticache describe-cache-clusters \ |
| 86 | --query 'CacheClusters[?contains(CacheClusterId,`staging`)].{id:CacheClusterId,sg:SecurityGroups[].SecurityGroupId}' --output json |
| 87 | ``` |
| 88 | |
| 89 | - [ ] **Step 5: Choose the two box IPs and prove the /32s shadow nothing** |
| 90 | |
| 91 | Pick the box subnet: whichever staging subnet is associated with the igw route table `rtb-041f57e000fb5797e` (check `aws ec2 describe-route-tables --route-table-ids rtb-041f57e000fb5797e --query 'RouteTables[].Associations'`). Pick two free IPs high in that subnet's range (e.g. `.200`/`.201` of its last /24), then verify **both** VPCs: |
| 92 | |
| 93 | ```bash |
| 94 | for ip in <candidate1> <candidate2>; do |
| 95 | aws --profile titogo --region eu-west-1 ec2 describe-network-interfaces \ |
| 96 | --filters Name=addresses.private-ip-address,Values=$ip \ |
| 97 | --query 'NetworkInterfaces[].{vpc:VpcId,desc:Description}' --output json |
| 98 | done |
| 99 | ``` |
| 100 | |
| 101 | Requirement: no ENI at that address in `vpc-90dffef5` (production — the /32 would shadow it from the default VPC) and none in `vpc-03be5678d08549203` (staging — the IP must be free to assign). Record IPs + the empty query output. |
| 102 | |
| 103 | - [ ] **Step 6: Commit the facts file** |
| 104 | |
| 105 | ```bash |
| 106 | git add tito/rails-poc-facts.md |
| 107 | git commit -m "tito: record rails-poc execution facts (IPs, images, commands, SGs)" |
| 108 | ``` |
| 109 | |
| 110 | --- |
| 111 | |
| 112 | ### Task 2: Networking tofu — peering and host routes |
| 113 | |
| 114 | **Files:** |
| 115 | - Create: `platform-infrastructure/tito/rails-poc-network.tf` |
| 116 | |
| 117 | **Interfaces:** |
| 118 | - Consumes: `classic_ip`, `pro_ip`, box subnet id from Task 1. |
| 119 | - Produces: `aws_vpc_peering_connection.rails_poc_staging` and `local.rails_poc` locals; Task 3 references both. |
| 120 | |
| 121 | - [ ] **Step 1: Write `tito/rails-poc-network.tf`** |
| 122 | |
| 123 | ```hcl |
| 124 | # 🤖 Rails-behind-the-edge PoC (see platform repo spec 2026-08-11): the |
| 125 | # edge (default VPC) reaches two staging Rails boxes over a peering. |
| 126 | # The default VPC ALREADY routes 10.0.0.0/16 to the PRODUCTION peering |
| 127 | # (pcx-056b55b69cbf89143) and staging shares that CIDR, so only /32 |
| 128 | # host routes are safe here — each verified ENI-free in the production |
| 129 | # VPC before choosing (tito/rails-poc-facts.md). |
| 130 | |
| 131 | locals { |
| 132 | rails_poc = { |
| 133 | default_vpc_id = "vpc-02416de3913753989" |
| 134 | staging_vpc_id = "vpc-03be5678d08549203" |
| 135 | default_rtb_id = "rtb-08863e575439a6377" |
| 136 | staging_rtb_id = "<igw rtb from Task 1 step 5>" |
| 137 | box_subnet_id = "<subnet from Task 1 step 5>" |
| 138 | edge_private_ip = "172.31.4.137" |
| 139 | classic_ip = "<from facts>" |
| 140 | pro_ip = "<from facts>" |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | resource "aws_vpc_peering_connection" "rails_poc_staging" { |
| 145 | vpc_id = local.rails_poc.default_vpc_id |
| 146 | peer_vpc_id = local.rails_poc.staging_vpc_id |
| 147 | auto_accept = true |
| 148 | tags = { Name = "carlos-tito-rails-poc-staging" } |
| 149 | } |
| 150 | |
| 151 | resource "aws_route" "rails_poc_to_classic" { |
| 152 | route_table_id = local.rails_poc.default_rtb_id |
| 153 | destination_cidr_block = "${local.rails_poc.classic_ip}/32" |
| 154 | vpc_peering_connection_id = aws_vpc_peering_connection.rails_poc_staging.id |
| 155 | } |
| 156 | |
| 157 | resource "aws_route" "rails_poc_to_pro" { |
| 158 | route_table_id = local.rails_poc.default_rtb_id |
| 159 | destination_cidr_block = "${local.rails_poc.pro_ip}/32" |
| 160 | vpc_peering_connection_id = aws_vpc_peering_connection.rails_poc_staging.id |
| 161 | } |
| 162 | |
| 163 | # Return path: the box subnet's route table learns the default VPC. |
| 164 | # 172.31.0.0/16 previously fell through to the igw (dropped); no live |
| 165 | # traffic changes meaning. |
| 166 | resource "aws_route" "rails_poc_return" { |
| 167 | route_table_id = local.rails_poc.staging_rtb_id |
| 168 | destination_cidr_block = "172.31.0.0/16" |
| 169 | vpc_peering_connection_id = aws_vpc_peering_connection.rails_poc_staging.id |
| 170 | } |
| 171 | ``` |
| 172 | |
| 173 | Replace the three `<…>` values from `rails-poc-facts.md` — they are Task 1 outputs, not decisions. |
| 174 | |
| 175 | - [ ] **Step 2: Targeted plan, verify it creates exactly 4 resources** |
| 176 | |
| 177 | ```bash |
| 178 | tofu -chdir=tito plan \ |
| 179 | -target=aws_vpc_peering_connection.rails_poc_staging \ |
| 180 | -target=aws_route.rails_poc_to_classic \ |
| 181 | -target=aws_route.rails_poc_to_pro \ |
| 182 | -target=aws_route.rails_poc_return |
| 183 | ``` |
| 184 | |
| 185 | Expected: `4 to add, 0 to change, 0 to destroy`. Anything else — stop, do not apply, diagnose. |
| 186 | |
| 187 | - [ ] **Step 3: Commit** |
| 188 | |
| 189 | ```bash |
| 190 | git add tito/rails-poc-network.tf |
| 191 | git commit -m "tito: rails-poc peering + /32 host routes (staging PoC networking)" |
| 192 | ``` |
| 193 | |
| 194 | --- |
| 195 | |
| 196 | ### Task 3: Box tofu — SGs, IAM, AMI, instances, cloud-init |
| 197 | |
| 198 | **Files:** |
| 199 | - Create: `platform-infrastructure/tito/rails-poc-boxes.tf` |
| 200 | - Create: `platform-infrastructure/tito/rails-poc-init.sh.tpl` |
| 201 | |
| 202 | **Interfaces:** |
| 203 | - Consumes: `local.rails_poc` (Task 2), image URIs + ports + commands (Task 1 facts). |
| 204 | - Produces: `aws_security_group.rails_poc["classic"|"pro"]` (Task 6 references), boxes tagged `carlos-tito-rails-classic-staging` / `carlos-tito-rails-pro-staging` (Tasks 5/8 find them by that tag), output `rails_poc_ips`. |
| 205 | |
| 206 | - [ ] **Step 1: Write `tito/rails-poc-boxes.tf`** |
| 207 | |
| 208 | ```hcl |
| 209 | # svc-pdf pattern scaled up: SSM-only IAM, SG trust, docker via |
| 210 | # cloud-init. x86 because the Buildkite images are amd64 (spec §1) — |
| 211 | # the fleet's shared AMI is arm64, so a separate amd64 data source. |
| 212 | |
| 213 | data "aws_ami" "rails_poc_debian_amd64" { |
| 214 | most_recent = true |
| 215 | owners = ["136693071363"] # Debian official |
| 216 | filter { |
| 217 | name = "name" |
| 218 | values = ["debian-13-amd64-*"] |
| 219 | } |
| 220 | filter { |
| 221 | name = "architecture" |
| 222 | values = ["x86_64"] |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | locals { |
| 227 | rails_poc_apps = { |
| 228 | classic = { ip = local.rails_poc.classic_ip, port = <classic web port>, image = "<classic image uri>" } |
| 229 | pro = { ip = local.rails_poc.pro_ip, port = <pro web port>, image = "<pro image uri>" } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | resource "aws_security_group" "rails_poc" { |
| 234 | for_each = local.rails_poc_apps |
| 235 | name = "carlos-tito-rails-${each.key}-staging" |
| 236 | description = "Rails PoC ${each.key}: web port from the IE edge only" |
| 237 | vpc_id = local.rails_poc.staging_vpc_id |
| 238 | ingress { |
| 239 | from_port = each.value.port |
| 240 | to_port = each.value.port |
| 241 | protocol = "tcp" |
| 242 | cidr_blocks = ["${local.rails_poc.edge_private_ip}/32"] |
| 243 | } |
| 244 | egress { |
| 245 | from_port = 0 |
| 246 | to_port = 0 |
| 247 | protocol = "-1" |
| 248 | cidr_blocks = ["0.0.0.0/0"] |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | resource "aws_iam_role" "rails_poc" { |
| 253 | for_each = local.rails_poc_apps |
| 254 | name = "carlos-tito-rails-${each.key}-staging" |
| 255 | assume_role_policy = jsonencode({ |
| 256 | Version = "2012-10-17" |
| 257 | Statement = [{ |
| 258 | Effect = "Allow" |
| 259 | Principal = { Service = "ec2.amazonaws.com" } |
| 260 | Action = "sts:AssumeRole" |
| 261 | }] |
| 262 | }) |
| 263 | } |
| 264 | |
| 265 | resource "aws_iam_role_policy_attachment" "rails_poc_ssm" { |
| 266 | for_each = local.rails_poc_apps |
| 267 | role = aws_iam_role.rails_poc[each.key].name |
| 268 | policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" |
| 269 | } |
| 270 | |
| 271 | resource "aws_iam_role_policy" "rails_poc_pull_and_env" { |
| 272 | for_each = local.rails_poc_apps |
| 273 | name = "pull-and-env" |
| 274 | role = aws_iam_role.rails_poc[each.key].id |
| 275 | policy = jsonencode({ |
| 276 | Version = "2012-10-17" |
| 277 | Statement = [ |
| 278 | { Effect = "Allow", Action = ["ecr:GetAuthorizationToken"], Resource = "*" }, |
| 279 | { Effect = "Allow", |
| 280 | Action = ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer", "ecr:BatchCheckLayerAvailability"], |
| 281 | Resource = "<repo arn(s) for ${each.key} from Task 1>" }, |
| 282 | { Effect = "Allow", Action = ["ssm:GetParameter"], |
| 283 | Resource = "arn:aws:ssm:eu-west-1:665106592142:parameter/tito/rails-poc/${each.key}-staging/*" }, |
| 284 | ] |
| 285 | }) |
| 286 | } |
| 287 | |
| 288 | resource "aws_iam_instance_profile" "rails_poc" { |
| 289 | for_each = local.rails_poc_apps |
| 290 | name = "carlos-tito-rails-${each.key}-staging" |
| 291 | role = aws_iam_role.rails_poc[each.key].name |
| 292 | } |
| 293 | |
| 294 | resource "aws_instance" "rails_poc" { |
| 295 | for_each = local.rails_poc_apps |
| 296 | ami = data.aws_ami.rails_poc_debian_amd64.id |
| 297 | instance_type = "t3a.large" |
| 298 | subnet_id = local.rails_poc.box_subnet_id |
| 299 | private_ip = each.value.ip |
| 300 | associate_public_ip_address = true # egress for SSM/ECR/apt; ingress is SG-closed |
| 301 | vpc_security_group_ids = [aws_security_group.rails_poc[each.key].id] |
| 302 | iam_instance_profile = aws_iam_instance_profile.rails_poc[each.key].name |
| 303 | user_data = templatefile("${path.module}/rails-poc-init.sh.tpl", { |
| 304 | app = each.key |
| 305 | port = each.value.port |
| 306 | }) |
| 307 | user_data_replace_on_change = false |
| 308 | root_block_device { |
| 309 | volume_size = 40 |
| 310 | volume_type = "gp3" |
| 311 | encrypted = true |
| 312 | } |
| 313 | tags = { Name = "carlos-tito-rails-${each.key}-staging" } |
| 314 | # Same floating-AMI trap as svc-pdf.tf: without this a newer Debian |
| 315 | # image turns `tofu apply` into "replace the box". |
| 316 | lifecycle { |
| 317 | ignore_changes = [ami] |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | output "rails_poc_ips" { |
| 322 | value = { for k, v in aws_instance.rails_poc : k => v.private_ip } |
| 323 | } |
| 324 | ``` |
| 325 | |
| 326 | - [ ] **Step 2: Write `tito/rails-poc-init.sh.tpl`** |
| 327 | |
| 328 | ```bash |
| 329 | #!/bin/bash |
| 330 | # 🤖 Rails PoC box init (${app}): docker + ECR helper + env from SSM + |
| 331 | # systemd units for the full process tree. Deploys happen via |
| 332 | # scripts/rails-poc-deploy.sh over SSM — first boot starts nothing |
| 333 | # until a deploy writes /etc/rails-poc/image. |
| 334 | set -euo pipefail |
| 335 | export DEBIAN_FRONTEND=noninteractive |
| 336 | apt-get update |
| 337 | apt-get install -y docker.io amazon-ecr-credential-helper awscli jq |
| 338 | |
| 339 | mkdir -p /root/.docker /etc/rails-poc |
| 340 | cat > /root/.docker/config.json <<'EOF' |
| 341 | { "credsStore": "ecr-login" } |
| 342 | EOF |
| 343 | |
| 344 | # App env from SSM (SecureString), 0600. Refreshed on every deploy too. |
| 345 | cat > /usr/local/bin/rails-poc-fetch-env <<'EOF' |
| 346 | #!/bin/bash |
| 347 | set -euo pipefail |
| 348 | aws ssm get-parameter --region eu-west-1 --with-decryption \ |
| 349 | --name /tito/rails-poc/${app}-staging/env \ |
| 350 | --query Parameter.Value --output text > /etc/rails-poc/app.env.new |
| 351 | chmod 600 /etc/rails-poc/app.env.new |
| 352 | mv /etc/rails-poc/app.env.new /etc/rails-poc/app.env |
| 353 | EOF |
| 354 | chmod 755 /usr/local/bin/rails-poc-fetch-env |
| 355 | |
| 356 | # One runner for every role; the role's command comes from the env file |
| 357 | # (WEB_COMMAND / WORKER_COMMAND / CLOCK_COMMAND, recorded from the k8s |
| 358 | # manifests in rails-poc-facts.md). |
| 359 | cat > /usr/local/bin/rails-poc-run <<'EOF' |
| 360 | #!/bin/bash |
| 361 | set -euo pipefail |
| 362 | role="$1" |
| 363 | image="$(cat /etc/rails-poc/image)" |
| 364 | source /etc/rails-poc/app.env |
| 365 | var="$(echo "$role" | tr a-z A-Z)_COMMAND" |
| 366 | publish="" |
| 367 | [ "$role" = web ] && publish="-p ${port}:${port}" |
| 368 | exec docker run --rm --name "rails-$role" $publish \ |
| 369 | --env-file /etc/rails-poc/app.env "$image" $${!var} |
| 370 | EOF |
| 371 | chmod 755 /usr/local/bin/rails-poc-run |
| 372 | |
| 373 | for role in web worker clock; do |
| 374 | cat > /etc/systemd/system/rails-$role.service <<EOF |
| 375 | [Unit] |
| 376 | Description=Rails PoC ${app} $role |
| 377 | After=docker.service network-online.target |
| 378 | Requires=docker.service |
| 379 | [Service] |
| 380 | ExecStartPre=-/usr/bin/docker rm -f rails-$role |
| 381 | ExecStart=/usr/local/bin/rails-poc-run $role |
| 382 | Restart=always |
| 383 | RestartSec=5 |
| 384 | [Install] |
| 385 | WantedBy=multi-user.target |
| 386 | EOF |
| 387 | done |
| 388 | systemctl daemon-reload |
| 389 | # Deliberately NOT enabled/started: the first deploy does that, once |
| 390 | # /etc/rails-poc/image exists and the env parameter is populated. |
| 391 | /usr/local/bin/rails-poc-fetch-env || true |
| 392 | ``` |
| 393 | |
| 394 | Note the tpl escaping: `${app}` / `${port}` are template vars; `$${!var}` renders as shell `${!var}`. If an app has no clock process (Task 1 facts), that unit simply never gets enabled. |
| 395 | |
| 396 | - [ ] **Step 3: Targeted plan** |
| 397 | |
| 398 | ```bash |
| 399 | tofu -chdir=tito plan $(printf ' -target=%s' \ |
| 400 | aws_security_group.rails_poc 'aws_iam_role.rails_poc' \ |
| 401 | 'aws_iam_role_policy_attachment.rails_poc_ssm' 'aws_iam_role_policy.rails_poc_pull_and_env' \ |
| 402 | 'aws_iam_instance_profile.rails_poc' 'aws_instance.rails_poc') |
| 403 | ``` |
| 404 | |
| 405 | Expected: adds only (2 SGs, 2 roles, 2 attachments, 2 policies, 2 profiles, 2 instances = 12), zero change/destroy. Verify the plan shows `t3a.large` and an `debian-13-amd64` AMI. |
| 406 | |
| 407 | - [ ] **Step 4: Commit** |
| 408 | |
| 409 | ```bash |
| 410 | git add tito/rails-poc-boxes.tf tito/rails-poc-init.sh.tpl |
| 411 | git commit -m "tito: rails-poc staging boxes (x86, docker+systemd, SSM-only)" |
| 412 | ``` |
| 413 | |
| 414 | --- |
| 415 | |
| 416 | ### Task 4: SSM env parameters (operator step with Paul) |
| 417 | |
| 418 | **Files:** none (parameters live in SSM only, per Global Constraints). |
| 419 | |
| 420 | **Interfaces:** |
| 421 | - Produces: `/tito/rails-poc/classic-staging/env` and `/tito/rails-poc/pro-staging/env` (SecureString, env-file format). Task 3's boxes and Task 5 depend on them existing. |
| 422 | |
| 423 | - [ ] **Step 1: Assemble each app's env from the k8s staging config** |
| 424 | |
| 425 | The k8s deployment's env (ConfigMap + Secrets) IS the source; dump it with Paul (values must not land in the transcript — pipe to a root-owned temp file, or Paul runs it): |
| 426 | |
| 427 | ```bash |
| 428 | kubectl -n <staging-namespace> exec deploy/<web-deploy> -- env | sort > /tmp/classic-staging.env |
| 429 | ``` |
| 430 | |
| 431 | Then append the three process-command lines from `rails-poc-facts.md`: |
| 432 | |
| 433 | ``` |
| 434 | WEB_COMMAND=<web command> |
| 435 | WORKER_COMMAND=<worker command> |
| 436 | CLOCK_COMMAND=<clock command, omit line if none> |
| 437 | ``` |
| 438 | |
| 439 | Prune k8s-runtime noise (`KUBERNETES_*`, `*_SERVICE_HOST`, `*_SERVICE_PORT`, `HOSTNAME`, `PATH`, `HOME`). Everything Rails reads stays. |
| 440 | |
| 441 | - [ ] **Step 2: Store as SecureString and shred the temp file** |
| 442 | |
| 443 | ```bash |
| 444 | aws --profile titogo --region eu-west-1 ssm put-parameter \ |
| 445 | --name /tito/rails-poc/classic-staging/env --type SecureString \ |
| 446 | --value file:///tmp/classic-staging.env |
| 447 | shred -u /tmp/classic-staging.env |
| 448 | ``` |
| 449 | |
| 450 | Repeat for pro. Verify with `aws ssm get-parameter --name … --query Parameter.Version` (never `--with-decryption` in the transcript). |
| 451 | |
| 452 | --- |
| 453 | |
| 454 | ### Task 5: Apply and boot the boxes |
| 455 | |
| 456 | **Interfaces:** |
| 457 | - Consumes: Tasks 2–4 all landed. |
| 458 | - Produces: two running boxes answering SSM, env fetched; nothing serving yet (first deploy is Task 8). |
| 459 | |
| 460 | - [ ] **Step 1: Targeted apply — networking first, then boxes** |
| 461 | |
| 462 | ```bash |
| 463 | tofu -chdir=tito apply \ |
| 464 | -target=aws_vpc_peering_connection.rails_poc_staging \ |
| 465 | -target=aws_route.rails_poc_to_classic \ |
| 466 | -target=aws_route.rails_poc_to_pro \ |
| 467 | -target=aws_route.rails_poc_return |
| 468 | tofu -chdir=tito apply $(printf ' -target=%s' \ |
| 469 | aws_security_group.rails_poc 'aws_iam_role.rails_poc' \ |
| 470 | 'aws_iam_role_policy_attachment.rails_poc_ssm' 'aws_iam_role_policy.rails_poc_pull_and_env' \ |
| 471 | 'aws_iam_instance_profile.rails_poc' 'aws_instance.rails_poc') |
| 472 | ``` |
| 473 | |
| 474 | Review each plan before confirming — adds only, and the route apply must show exactly the two `/32`s + return route + peering. |
| 475 | |
| 476 | - [ ] **Step 2: Verify both boxes via SSM** |
| 477 | |
| 478 | ```bash |
| 479 | for tag in classic pro; do |
| 480 | iid=$(aws --profile titogo --region eu-west-1 ec2 describe-instances \ |
| 481 | --filters Name=tag:Name,Values=carlos-tito-rails-$tag-staging Name=instance-state-name,Values=running \ |
| 482 | --query 'Reservations[].Instances[].InstanceId' --output text) |
| 483 | cid=$(aws --profile titogo --region eu-west-1 ssm send-command --instance-ids "$iid" \ |
| 484 | --document-name AWS-RunShellScript \ |
| 485 | --parameters 'commands=["docker --version; ls -l /etc/rails-poc/app.env; systemctl list-unit-files rails-*"]' \ |
| 486 | --query Command.CommandId --output text) |
| 487 | sleep 8 |
| 488 | aws --profile titogo --region eu-west-1 ssm get-command-invocation \ |
| 489 | --command-id "$cid" --instance-id "$iid" --query '{s:Status,o:StandardOutputContent}' --output json |
| 490 | done |
| 491 | ``` |
| 492 | |
| 493 | Expected: docker installed, `app.env` present mode 0600, three `rails-*.service` unit files. If `app.env` is missing, cloud-init ran before Task 4's parameters existed — rerun `/usr/local/bin/rails-poc-fetch-env` via SSM. |
| 494 | |
| 495 | - [ ] **Step 3: Commit the facts-file update (instance IDs)** |
| 496 | |
| 497 | ```bash |
| 498 | git add tito/rails-poc-facts.md |
| 499 | git commit -m "tito: rails-poc boxes applied; record instance ids" |
| 500 | ``` |
| 501 | |
| 502 | --- |
| 503 | |
| 504 | ### Task 6: Open the staging dependencies to the boxes |
| 505 | |
| 506 | **Files:** |
| 507 | - Create: `platform-infrastructure/tito/rails-poc-deps.tf` |
| 508 | |
| 509 | **Interfaces:** |
| 510 | - Consumes: staging DB/cache SG IDs (Task 1 facts), `aws_security_group.rails_poc` (Task 3). |
| 511 | |
| 512 | - [ ] **Step 1: Write `tito/rails-poc-deps.tf`** |
| 513 | |
| 514 | ```hcl |
| 515 | # Ingress on the (externally-managed) staging dependency SGs admitting |
| 516 | # the PoC boxes. aws_security_group_rule against SG ids this tree does |
| 517 | # not own — removal of the PoC removes exactly these rules and nothing |
| 518 | # else. Classic is MySQL, Pro is Postgres (rails-poc-facts.md). |
| 519 | |
| 520 | locals { |
| 521 | rails_poc_dep_rules = { |
| 522 | classic_mysql = { sg = "<classic staging DB sg>", port = 3306, app = "classic" } |
| 523 | classic_valkey = { sg = "<classic staging redis sg>", port = 6379, app = "classic" } |
| 524 | classic_memcached = { sg = "<classic staging memcached sg>", port = 11211, app = "classic" } |
| 525 | pro_postgres = { sg = "<pro staging DB sg>", port = 5432, app = "pro" } |
| 526 | pro_valkey = { sg = "<pro staging redis sg>", port = 6379, app = "pro" } |
| 527 | pro_memcached = { sg = "<pro staging memcached sg>", port = 11211, app = "pro" } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | resource "aws_security_group_rule" "rails_poc_dep" { |
| 532 | for_each = local.rails_poc_dep_rules |
| 533 | type = "ingress" |
| 534 | security_group_id = each.value.sg |
| 535 | from_port = each.value.port |
| 536 | to_port = each.value.port |
| 537 | protocol = "tcp" |
| 538 | source_security_group_id = aws_security_group.rails_poc[each.value.app].id |
| 539 | description = "rails-poc ${each.value.app} staging box" |
| 540 | } |
| 541 | ``` |
| 542 | |
| 543 | (Same-VPC SG references — the boxes live in the staging VPC precisely so this works.) |
| 544 | |
| 545 | - [ ] **Step 2: Targeted plan (6 adds only), apply, commit** |
| 546 | |
| 547 | ```bash |
| 548 | tofu -chdir=tito plan -target=aws_security_group_rule.rails_poc_dep |
| 549 | tofu -chdir=tito apply -target=aws_security_group_rule.rails_poc_dep |
| 550 | git add tito/rails-poc-deps.tf |
| 551 | git commit -m "tito: rails-poc staging dependency SG ingress" |
| 552 | ``` |
| 553 | |
| 554 | - [ ] **Step 3: Prove connectivity from each box (SSM)** |
| 555 | |
| 556 | ```bash |
| 557 | # on the classic box via send-command: |
| 558 | timeout 3 bash -c '</dev/tcp/tito-classic-mysql-staging-writer.cefg7n9rzaz0.eu-west-1.rds.amazonaws.com/3306' && echo mysql-ok |
| 559 | timeout 3 bash -c '</dev/tcp/<classic redis endpoint>/6379' && echo valkey-ok |
| 560 | timeout 3 bash -c '</dev/tcp/<classic memcached endpoint>/11211' && echo memcached-ok |
| 561 | # pro box: postgres 5432 + its two caches |
| 562 | ``` |
| 563 | |
| 564 | Expected: three `*-ok` lines per box. A hang means the SG rule or a subnet route is wrong — fix before proceeding. |
| 565 | |
| 566 | --- |
| 567 | |
| 568 | ### Task 7: Edge routes |
| 569 | |
| 570 | **Interfaces:** |
| 571 | - Consumes: box IPs + web ports (facts), boxes booted (Task 5). |
| 572 | - Produces: `classic.platform.tito.io` / `pro.platform.tito.io` routed (503/refused until Task 8 deploys). |
| 573 | |
| 574 | - [ ] **Step 1: Add both routes on the IE box via SSM, as user `carlos`** |
| 575 | |
| 576 | ```bash |
| 577 | aws --profile titogo --region eu-west-1 ssm send-command --instance-ids i-025a0b70f3d93304d \ |
| 578 | --document-name AWS-RunShellScript --parameters 'commands=[ |
| 579 | "sudo -u carlos /opt/carlos/carlos add -kind service -addr <classic_ip>:<classic port> classic.platform.tito.io", |
| 580 | "sudo -u carlos /opt/carlos/carlos add -kind service -addr <pro_ip>:<pro port> pro.platform.tito.io" |
| 581 | ]' --query Command.CommandId --output text |
| 582 | ``` |
| 583 | |
| 584 | Fetch the invocation output; each line must say `added` (or `updated … (existing route)` on a re-run — `carlos add` upserts; that is also how an upstream gets repointed). **No `-hibernate`, ever, on these routes.** |
| 585 | |
| 586 | - [ ] **Step 2: Verify TLS + routing end to end** |
| 587 | |
| 588 | ```bash |
| 589 | curl -sv https://classic.platform.tito.io/ -o /dev/null 2>&1 | grep -E 'subject:|HTTP/' |
| 590 | curl -sv https://pro.platform.tito.io/ -o /dev/null 2>&1 | grep -E 'subject:|HTTP/' |
| 591 | ``` |
| 592 | |
| 593 | Expected: a Let's Encrypt cert for each exact hostname (first hit may take a few seconds while HTTP-01 completes), then `502`/`503` — the edge is up, nothing listens on the box yet. A TLS `internal error` alert means the route didn't land in `/data/registry.db`; re-check Step 1's output. |
| 594 | |
| 595 | --- |
| 596 | |
| 597 | ### Task 8: Deploy script + first deploy |
| 598 | |
| 599 | **Files:** |
| 600 | - Create: `platform-infrastructure/scripts/rails-poc-deploy.sh` |
| 601 | |
| 602 | **Interfaces:** |
| 603 | - Consumes: everything above; image tag currently on k8s staging (facts). |
| 604 | - Produces: `rails-poc-deploy.sh <classic|pro> <image-ref>` — THE deploy path from here on. |
| 605 | |
| 606 | - [ ] **Step 1: Write `scripts/rails-poc-deploy.sh`** |
| 607 | |
| 608 | ```bash |
| 609 | #!/bin/bash |
| 610 | # 🤖 Deploy a Rails PoC box over SSM: refresh env, point at the image, |
| 611 | # restart the tree, wait for health through the edge. |
| 612 | # Usage: rails-poc-deploy.sh <classic|pro> <image-ref> |
| 613 | set -euo pipefail |
| 614 | app="$1"; image="$2" |
| 615 | region=eu-west-1; profile=titogo |
| 616 | iid=$(aws --profile $profile --region $region ec2 describe-instances \ |
| 617 | --filters Name=tag:Name,Values=carlos-tito-rails-$app-staging Name=instance-state-name,Values=running \ |
| 618 | --query 'Reservations[].Instances[].InstanceId' --output text) |
| 619 | [ -n "$iid" ] || { echo "no running box for $app" >&2; exit 1; } |
| 620 | |
| 621 | cid=$(aws --profile $profile --region $region ssm send-command --instance-ids "$iid" \ |
| 622 | --document-name AWS-RunShellScript --parameters commands="[ |
| 623 | \"/usr/local/bin/rails-poc-fetch-env\", |
| 624 | \"docker pull $image\", |
| 625 | \"echo $image > /etc/rails-poc/image\", |
| 626 | \"systemctl enable rails-web rails-worker\", |
| 627 | \"systemctl restart rails-web rails-worker\", |
| 628 | \"systemctl is-enabled rails-clock >/dev/null 2>&1 && systemctl restart rails-clock || true\" |
| 629 | ]" --query Command.CommandId --output text) |
| 630 | |
| 631 | for i in $(seq 1 60); do |
| 632 | st=$(aws --profile $profile --region $region ssm get-command-invocation \ |
| 633 | --command-id "$cid" --instance-id "$iid" --query Status --output text) |
| 634 | [ "$st" = Success ] && break |
| 635 | [ "$st" = Failed ] && { aws --profile $profile --region $region ssm get-command-invocation \ |
| 636 | --command-id "$cid" --instance-id "$iid" --query StandardErrorContent --output text >&2; exit 1; } |
| 637 | sleep 5 |
| 638 | done |
| 639 | |
| 640 | url="https://$app.platform.tito.io<health path from facts>" |
| 641 | for i in $(seq 1 30); do |
| 642 | code=$(curl -s -o /dev/null -w '%{http_code}' "$url") && [ "$code" = 200 ] && { echo "$app healthy: $url"; exit 0; } |
| 643 | sleep 5 |
| 644 | done |
| 645 | echo "$app not healthy after deploy ($url last code $code)" >&2; exit 1 |
| 646 | ``` |
| 647 | |
| 648 | `chmod +x scripts/rails-poc-deploy.sh`. (Apps with a clock process get it enabled once, manually, via SSM: `systemctl enable rails-clock` — the script then keeps restarting it.) |
| 649 | |
| 650 | - [ ] **Step 2: First deploys, with the tag k8s staging currently runs** |
| 651 | |
| 652 | ```bash |
| 653 | ./scripts/rails-poc-deploy.sh classic <classic image:tag from facts> |
| 654 | ./scripts/rails-poc-deploy.sh pro <pro image:tag from facts> |
| 655 | ``` |
| 656 | |
| 657 | Expected: both end with `healthy`. Debug path if not: `journalctl -u rails-web -n 100` via SSM on the box. Likely first-boot issues: a missing env var (fix the SSM parameter, redeploy), or the app binding localhost only (the k8s manifests' web command already binds 0.0.0.0 — reuse it exactly). |
| 658 | |
| 659 | - [ ] **Step 3: Verify the full tree and the replica behavior** |
| 660 | |
| 661 | ```bash |
| 662 | # via SSM on each box: |
| 663 | docker ps --format '{{.Names}} {{.Status}}' # rails-web, rails-worker (+ rails-clock) |
| 664 | ``` |
| 665 | |
| 666 | Then the shared-queue proof: with Paul, enqueue a staging background job through the **k8s** app (or its console) and watch it get picked up on a PoC box: `journalctl -u rails-worker -f` via an SSM session. A job processed on the PoC box while k8s workers also run = the parallel-replica claim, proven. |
| 667 | |
| 668 | - [ ] **Step 4: Commit** |
| 669 | |
| 670 | ```bash |
| 671 | git add scripts/rails-poc-deploy.sh |
| 672 | git commit -m "scripts: rails-poc deploy over SSM (pull, restart, edge health)" |
| 673 | ``` |
| 674 | |
| 675 | --- |
| 676 | |
| 677 | ### Task 9: Production tofu — written, gated off |
| 678 | |
| 679 | **Files:** |
| 680 | - Create: `platform-infrastructure/tito/rails-poc-prod.tf` |
| 681 | |
| 682 | - [ ] **Step 1: Write the gated file** |
| 683 | |
| 684 | ```hcl |
| 685 | # 🤖 Production twin of the staging PoC — WRITTEN, NOT APPLIED (spec: |
| 686 | # staging first; flipping this variable is the post-PoC decision). |
| 687 | # Networking is SIMPLER than staging: the default VPC already routes |
| 688 | # 10.0.0.0/16 to the existing production peering pcx-056b55b69cbf89143, |
| 689 | # so prod boxes need NO new peering and NO new routes — only boxes, |
| 690 | # SG rules on the prod dependency SGs, and /tito/rails-poc/<app>-prod/env |
| 691 | # parameters. Fill the <…> values from a prod re-run of Task 1 |
| 692 | # (rails-poc-facts.md gets a prod section) before ever enabling. |
| 693 | |
| 694 | variable "rails_poc_prod_enabled" { |
| 695 | type = bool |
| 696 | default = false |
| 697 | } |
| 698 | |
| 699 | resource "aws_instance" "rails_poc_prod" { |
| 700 | for_each = var.rails_poc_prod_enabled ? { |
| 701 | classic = { ip = "<prod classic ip — ENI-free check vs BOTH VPCs>", port = <port>, subnet = "<prod subnet>" } |
| 702 | pro = { ip = "<prod pro ip>", port = <port>, subnet = "<prod subnet>" } |
| 703 | } : {} |
| 704 | ami = data.aws_ami.rails_poc_debian_amd64.id |
| 705 | instance_type = "t3a.large" |
| 706 | subnet_id = each.value.subnet |
| 707 | private_ip = each.value.ip |
| 708 | vpc_security_group_ids = [] # prod SGs defined alongside when enabling |
| 709 | user_data = templatefile("${path.module}/rails-poc-init.sh.tpl", { |
| 710 | app = each.key |
| 711 | port = each.value.port |
| 712 | }) |
| 713 | root_block_device { |
| 714 | volume_size = 40 |
| 715 | volume_type = "gp3" |
| 716 | encrypted = true |
| 717 | } |
| 718 | tags = { Name = "carlos-tito-rails-${each.key}-prod" } |
| 719 | lifecycle { ignore_changes = [ami] } |
| 720 | } |
| 721 | ``` |
| 722 | |
| 723 | - [ ] **Step 2: Prove it is inert, commit** |
| 724 | |
| 725 | ```bash |
| 726 | tofu -chdir=tito plan -target=aws_instance.rails_poc_prod |
| 727 | # Expected: "No changes." — the empty for_each makes it a no-op. |
| 728 | git add tito/rails-poc-prod.tf |
| 729 | git commit -m "tito: rails-poc production twin, gated off (post-PoC decision)" |
| 730 | ``` |
| 731 | |
| 732 | --- |
| 733 | |
| 734 | ### Task 10: Runbook, PRs, and the evaluation record |
| 735 | |
| 736 | **Files:** |
| 737 | - Modify: `platform-infrastructure/tito/README.md` (append a `## Rails PoC` section) |
| 738 | - Modify (platform repo, branch `rails-behind-edge-poc-spec`): the spec's §7 — check off what was actually met |
| 739 | |
| 740 | - [ ] **Step 1: Append the runbook to `tito/README.md`** |
| 741 | |
| 742 | ```markdown |
| 743 | ## Rails PoC (classic/pro staging behind the edge) |
| 744 | |
| 745 | Spec + plan: platform repo `docs/superpowers/{specs,plans}/2026-08-11-rails-behind-carlos-edge-poc*`. |
| 746 | Facts (IPs, images, SGs): `tito/rails-poc-facts.md`. |
| 747 | |
| 748 | - Deploy: `scripts/rails-poc-deploy.sh <classic|pro> <image-ref>` (SSM; no SSH). |
| 749 | - Routes live in the IE box's `/data/registry.db` (`carlos add` as user |
| 750 | `carlos`; re-run to repoint; NEVER `-hibernate` on these). |
| 751 | - Logs: `journalctl -u rails-{web,worker,clock}` via SSM on the box. |
| 752 | - Teardown: remove the routes on the IE box, then targeted destroys of |
| 753 | `aws_instance.rails_poc`, `aws_security_group_rule.rails_poc_dep`, |
| 754 | the two `/32` `aws_route.rails_poc_to_*`, `aws_route.rails_poc_return`, |
| 755 | `aws_vpc_peering_connection.rails_poc_staging`; delete the |
| 756 | `/tito/rails-poc/*` SSM parameters. |
| 757 | - The default VPC's `10.0.0.0/16 → pcx-056b55b69cbf89143` (production) |
| 758 | route is why staging only ever gets `/32` routes — do not "simplify". |
| 759 | - Fallback if a shared cache is unreachable (spec §"Decisions"): an |
| 760 | on-box memcached is one unit away — |
| 761 | `docker run -d --restart=always -p 127.0.0.1:11211:11211 memcached:1.6` |
| 762 | plus pointing the app's cache env var at `127.0.0.1:11211`. Documented |
| 763 | fallback only; the default is the shared staging Elasticache. |
| 764 | ``` |
| 765 | |
| 766 | - [ ] **Step 2: During the parallel-run week, record the §7 gap list against reality** |
| 767 | |
| 768 | In the spec (platform repo), annotate each §7 item with what actually happened (e.g. "config delivery: hand-rolled SSM fetch worked but rotation is manual — a real `appconfig` gap"). This annotated list is the deliverable of the evaluation. |
| 769 | |
| 770 | - [ ] **Step 3: Open the PRs** |
| 771 | |
| 772 | ```bash |
| 773 | # infra: |
| 774 | cd ~/github.com/carlosframework/platform-infrastructure/.claude/worktrees/rails-poc |
| 775 | git push -u origin rails-poc |
| 776 | gh pr create --title "tito: Rails-behind-the-edge staging PoC (classic/pro)" \ |
| 777 | --body "Boxes, peering, dep SG rules, deploy script per the platform repo spec 2026-08-11-rails-behind-carlos-edge-poc-design.md. Targeted applies only — see plan. |
| 778 | |
| 779 | 🤖 Generated with [Claude Code](https://claude.com/claude-code)" |
| 780 | # spec+plan: |
| 781 | cd ~/github.com/carlosframework/platform/.claude/worktrees/rails-behind-edge-poc-spec |
| 782 | git push -u origin rails-behind-edge-poc-spec |
| 783 | gh pr create --title "docs: Rails-behind-the-edge PoC — design record + implementation plan" \ |
| 784 | --body "Design + plan for serving Tito Classic/Pro staging through the CARLOS edge (spec approved by Paul 2026-08-11). |
| 785 | |
| 786 | 🤖 Generated with [Claude Code](https://claude.com/claude-code)" |
| 787 | ``` |
| 788 | |
| 789 | --- |
| 790 | |
| 791 | ## Execution notes |
| 792 | |
| 793 | - Tasks 1→8 are strictly ordered; 9 and 10 can follow any time after 3 and 8 respectively. |
| 794 | - Applies (Tasks 5, 6) and anything touching the IE box (Task 7) or k8s (Tasks 1, 4, 8) are operator-visible actions — do them deliberately, one at a time, reading every plan before confirming. |
| 795 | - If any targeted plan proposes a change or destroy outside the `rails_poc*` names, STOP — that is the known tree drift; never apply through it. |
| 796 | |