Config as Code to Railway IaC field mapping
How each railway.json / railway.toml setting translates into service(name, { ... }) in .railway/railway.ts.
Legend
- ✅ Documented shorthand: confirmed in the IaC reference
- 🔧 Via
railway config migrate: no documented shorthand; let the CLI translate it, or use the nestedbuild:/deploy:object form (the docs confirm nested form forpreDeployCommand; verify others viarailway config plan)
Build section
| CaC field | IaC DSL | Status / notes |
|---|---|---|
build.buildCommand |
build: "cmd" |
✅ |
build.builder (RAILPACK / DOCKERFILE) |
- | 🔧 config migrate handles it; no documented service() key |
build.watchPatterns |
- | 🔧 same |
build.dockerfilePath |
- | 🔧 same (Dockerfile builds are also implied by a Dockerfile in the repo) |
build.railpackVersion |
- | 🔧 same; RAILPACK_VERSION variable also still works |
Deploy section
| CaC field | IaC DSL | Status / notes |
|---|---|---|
deploy.startCommand |
start: "cmd" |
✅ |
deploy.preDeployCommand (array) |
preDeploy: "cmd" (string) |
✅ Or keep nested form deploy: { preDeployCommand: ["cmd"] }, which config pull renders |
deploy.healthcheckPath |
healthcheck: "/path" |
✅ |
deploy.healthcheckTimeout |
healthcheckTimeout: 300 |
✅ |
deploy.multiRegionConfig |
replicas: { "us-west2": 2, ... } |
✅ Drop the numReplicas wrapper: {region: {numReplicas: n}} becomes {region: n} |
deploy.restartPolicyType / restartPolicyMaxRetries |
- | 🔧 config migrate handles; verify in plan output |
deploy.cronSchedule |
- | 🔧 same |
deploy.overlapSeconds / drainingSeconds |
- | 🔧 same (CaC stored these as strings) |
Things CaC had that IaC models differently
| CaC concept | IaC equivalent |
|---|---|
environments.<name>.{build,deploy} |
ctx.isEnvironment("<name>") ? a : b (or ctx.environment === "<name>") inside defineRailway |
environments.pr.* |
Conditional on the ephemeral environment's name via ctx.environment; no dedicated pr key |
| File picked up automatically at deploy | Nothing reads .railway/ at deploy time; railway config apply (local or CI) pushes config to the environment |
| "Config in code overrides dashboard" | Apply writes the values to the environment; there is one source of truth afterward, not per-deploy override |
Dashboard settings CaC could express that IaC adds
Once migrated, the same file can also own things CaC never touched: source (GitHub repo/image), env variables (preserve(), ctx.shared, cross-resource refs), domains, volumeMounts, databases (postgres/redis/mysql/mongo), volume, bucket, group, and replicas as a plain number. Consider folding service Settings-page config (env vars, domains, sources) into railway.ts during migration. Running railway config pull first is an easy way to capture them.
Worked example
// railway.json (before)
{
"build": { "builder": "RAILPACK", "buildCommand": "pnpm build" },
"deploy": {
"preDeployCommand": ["npm run db:migrate"],
"startCommand": "pnpm start",
"healthcheckPath": "/health",
"healthcheckTimeout": 100,
"restartPolicyType": "on_failure",
"multiRegionConfig": { "us-west2": { "numReplicas": 2 } }
},
"environments": {
"staging": { "deploy": { "startCommand": "pnpm start:staging" } }
}
}// .railway/railway.ts (the shape `railway config migrate` produces)
export default defineRailway((ctx) => {
const web = service("web", {
build: "pnpm build",
start: ctx.isEnvironment("staging") ? "pnpm start:staging" : "pnpm start",
preDeploy: "npm run db:migrate",
healthcheck: "/health",
healthcheckTimeout: 100,
replicas: { "us-west2": 2 },
// builder / restartPolicyType: rendered by `config migrate` in the
// form the CLI accepts; don't hand-translate, diff against the old file
});
return project("my-project", { resources: [web] });
});