Migrate Railway Config as Code to IaC
Config as Code (railway.json / railway.toml) is deprecated. It stops being read on 2026-12-01 (hard cutoff), and new services can't opt in. The replacement is Infrastructure as Code: .railway/railway.ts evaluated by the Railway CLI.
| Config as Code | Infrastructure as Code | |
|---|---|---|
| Scope | One service's deployment | Whole project/environment |
| File | railway.json / railway.toml in the repo |
.railway/railway.ts |
| Evaluated | At deploy time, overrides dashboard | By CLI: railway config plan / apply |
A service cannot be managed by both systems. railway config plan refuses to run while any targeted service still reads a CaC file; the error names the service. Migration means: get config into railway.ts, get CaC out of the deploy path (remove the file AND clear any custom config-path setting), then apply.
Step 1: Inventory what needs migrating
# Find every CaC file in the repo
find . -name "railway.toml" -o -name "railway.json" | grep -v node_modulesAlso check each service's Settings page for a custom config file path (e.g. /backend/railway.toml): a file outside the default search locations still counts as CaC-managed, and its path must be cleared during migration.
Note the repo topology; it decides the output shape:
- One repo holds all services in the environment: single
railway.ts, nopartialexport. - Services live in separate repos: run the migration in each repo; each writes
export const partial = "<service name>"so it owns only its slice.
Step 2: Migrate with the CLI (preferred)
# Preview the generated authoring file (writes nothing)
railway config migrate
# Write .railway/railway.ts AND clear the Railway Config File settings on the services
railway config migrate --apply
# Also delete the old railway.json / railway.toml files
railway config migrate --apply --delete-files
# Options
railway config migrate --service api --apply # one service only; writes a named `partial` export
railway config migrate --lang py --apply # Python (beta); --lang go for GoBehavior to expect:
- Monorepo:
migratefinds every CaC file in the repository and merges them into onerailway.ts. Nopartialexport. - Single-service repo (
--serviceor one file found): writesexport const partial = "<service name>", matching how CaC applied per-service. --applyclears the dashboard's "Railway Config File" setting so deploys stop reading the old file.
If railway config migrate isn't available in the installed CLI, use the manual path: railway config pull --force, translate fields per references/field-mapping.md, then continue at Step 3.
Step 3: Detach Config as Code completely
Both must happen or the service stays CaC-managed and plan keeps failing:
- Remove
railway.json/railway.tomlfrom the service's source repo (or use--delete-files). - Clear the custom config file path in service Settings, if one was set. (
--applydoes this; the manual path requires doing it in the dashboard.)
Step 4: Review the plan to verify
railway config planA good migration plan shows only the settings you intentionally moved (e.g. build, start, healthcheck updates on migrated services). Red flags (do NOT apply):
- Deletes of services, variables, volumes, or buckets you didn't intend
- Changes to unrelated services
- A plan still blocked by "managed by config file" errors, meaning a CaC file or custom path survived (Step 3)
If the CaC file had settings the DSL doesn't cover, config migrate preserves them. Diff the generated railway.ts against the old file field by field before deleting it (field-mapping reference below).
Step 5: Apply
railway config apply # interactive confirm
railway config apply --yes # non-interactive; + --confirm-destructive if the plan marks deletesFor a single-service (partial) migration, the first apply also claims ownership of the declared resources. It runs even when the plan shows no changes. That's expected.
Translating environments.* overrides
CaC environments.<name> blocks become ctx conditionals in defineRailway:
// railway.json: { "environments": { "staging": { "deploy": { "startCommand": "npm run staging" } } } }
const web = service("web", {
start: ctx.isEnvironment("staging") ? "npm run staging" : "npm start",
});CaC environments.pr applied to every ephemeral PR environment. There's no ctx.isPR; reproduce it by checking the ephemeral environment name via ctx.environment, or keep per-env conditionals for the environments you actually deploy.
Field mapping
Full CaC-to-DSL translation table (confirmed shorthands vs. fields handled by config migrate): references/field-mapping.md.
Working agreement
- Always run
railway config migrate(preview) before--apply. Read the generated file before it lands. - Never apply a plan you haven't reviewed. Destructive lines are marked; a stray
--yescan't delete (needs--confirm-destructive), but a reviewed-wrong plan still can. - Delete old CaC files only after the plan is clean. They're the ground truth for the diff.
- Secrets stay on Railway. Generated
preserve()entries keep existing values; never paste secret values intorailway.ts, and be deliberate about--include-variables(it decrypts non-sealed variables into source). - One authoring language per repo. Don't leave both
railway.tsandrailway.pyin.railway/. - For multi-repo setups, repeat in every repo. A partial only covers its own slice.
Troubleshooting
| Symptom | Fix |
|---|---|
plan stops: "service managed by railway.json/railway.toml" |
CaC file still in repo or custom config path still set. Finish Step 3 |
Migrated file has partial, now other repos' files fail |
Expected: once any partial is named, every file targeting that environment must export one |
| Plan shows unexpected deletes | CaC config wasn't fully translated. Compare old file vs railway.ts, then re-plan |
| Plan shows tracing being removed | SDKs don't support tracing yet; don't apply. See railway-iac skill |