> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ubiquex.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Environments

> Multi-environment patterns with extend blocks and the --env flag.

ubx handles multiple environments (staging, production, etc.) through `extend` blocks without duplicating stack definitions.

## How It Works

1. Write base resource definitions in `main.iac` (staging defaults)
2. Write environment-specific overrides in `envs/<env>/overrides.iac` using `extend` blocks
3. Deploy with `ubx apply --env <env>`

## File Layout

```
project/
  main.iac              # base definitions
  ubx.yaml
  envs/
    staging/
      overrides.iac     # staging-specific overrides
    prod/
      overrides.iac     # production overrides
    eu/
      overrides.iac     # region-specific overrides
```

## extend Block Syntax

```hcl theme={null}
# envs/prod/overrides.iac
extend "unit" "aws_rds_instance" "db" {
  instance_class    = "db.r6g.2xlarge"
  allocated_storage = 100
  multi_az          = true
}
```

Only the overridden attributes change — all other attributes inherit from the base.

## The `--env` Flag

```bash theme={null}
ubx plan --env staging     # preview staging
ubx plan --env prod        # preview production
ubx apply --env prod       # deploy to production
```

The `--env` flag tells ubx which `envs/<env>/` folder to load overrides from.

## Default Environment

```yaml theme={null}
# ubx.yaml
defaultEnv: staging
```

When `--env` is omitted, `defaultEnv` is used.

## Environment in Resources

Use `input.env` to inject the environment name into resource configurations:

```hcl theme={null}
input "env" {
  type    = "string"
  default = "staging"
}

unit "aws_s3_bucket_v2" "assets" {
  bucket = "${input.env}-assets"
}
```

```bash theme={null}
ubx apply --env prod     # bucket = "prod-assets"
ubx apply --env staging  # bucket = "staging-assets"
```
