---
name: railway-iac-migration
description: Migrate Railway Config as Code (railway.json / railway.toml) to Infrastructure as Code (.railway/railway.ts). Use when a repo has railway.json or railway.toml files, when the user mentions "config as code", "railway.toml", "railway.json", "migrate railway config", "railway config migrate", or before the Config as Code hard cutoff on 2026-12-01. For authoring IaC from scratch, use the railway-iac skill.
license: MIT
metadata:
  version: "1.0.0"
---

# 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

```bash
# Find every CaC file in the repo
find . -name "railway.toml" -o -name "railway.json" | grep -v node_modules
```

Also 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`, no `partial` export.
- **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)

```bash
# 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 Go
```

Behavior to expect:

- Monorepo: `migrate` finds every CaC file in the repository and merges them into **one** `railway.ts`. No `partial` export.
- Single-service repo (`--service` or one file found): writes `export const partial = "<service name>"`, matching how CaC applied per-service.
- `--apply` clears 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](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:

1. **Remove `railway.json` / `railway.toml`** from the service's source repo (or use `--delete-files`).
2. **Clear the custom config file path** in service Settings, if one was set. (`--apply` does this; the manual path requires doing it in the dashboard.)

## Step 4: Review the plan to verify

```bash
railway config plan
```

A 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

```bash
railway config apply            # interactive confirm
railway config apply --yes      # non-interactive; + --confirm-destructive if the plan marks deletes
```

For 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`:

```ts
// 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](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 `--yes` can'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 into `railway.ts`, and be deliberate about `--include-variables` (it decrypts non-sealed variables into source).
- **One authoring language per repo.** Don't leave both `railway.ts` and `railway.py` in `.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 |
