Skip to content
TypeScript
2.4 KB
Raw Blame
---
title: Environments
description: Extra deploy environments under a Project. Production is created with the Project — do not duplicate it.
---

A [`Railway.Environment`](/providers/railway/environment) is an extra
deploy environment under a [Project](/railway/compute/projects)
(staging, preview, …). The production environment is created with
the Project — do **not** recreate it as an Environment resource.

## Create an extra environment

Alchemy generates a unique name unless you pass one. Production is
already on the Project as `environmentId`.

```typescript
const site = yield* Railway.Project("Site");
const staging = yield* Railway.Environment("Staging", {
  project: site,
});
```

:::note
Prefer omitting `name` in tests and CI so names stay unique and
reclaimable.
:::

## A stable name

Pass `name` when you need a stable environment name (`staging`).
Changing it later updates the environment in place via
`environmentRename`.

```typescript
const staging = yield* Railway.Environment("Staging", {
  project: site,
  name: "staging",
});
```

## Fork from production

`sourceEnvironmentId` copies services, volumes, configuration, and
variables from another environment. Create-only.

```typescript
const staging = yield* Railway.Environment("Staging", {
  project: site,
  sourceEnvironmentId: site.environmentId,
});
```

## Target an environment from a child

Services, Volumes, Postgres, Redis, Variables, and Buckets default
to the Project's primary environment. Pass `environment` to put them
in staging.

```typescript
const api = yield* Railway.Service("StagingApi", {
  project: site,
  environment: staging,
  image: "hashicorp/http-echo",
  port: 5678,
});
```

:::caution[Changing `project` replaces the Environment]
The Environment is created in the new Project. The old Environment
is deleted.
:::

## Module-scope declarations

Declare the Project once. Pass it into every child.

```typescript
// src/project.ts
import * as Railway from "@alchemy.run/railway";

export const Site = Railway.Project("Site");
export const Staging = Railway.Environment("Staging", {
  project: Site,
});
```

Resource-valued props accept the resource or an Effect producing it.

## Where next

[Projects](/railway/compute/projects) own the production environment.
[Services](/railway/compute/services) take `environment`.
The [`Environment` reference](/providers/railway/environment) lists
every prop.