# dokploy_environment_variables (Resource)

The environment variables of one application, compose, or environment, as a map. The resource owns the whole `env` text of its target: each apply writes every variable in the map, in key order, and removes every other line.

~> **The target must not manage `env` itself.** `dokploy_application`, `dokploy_compose`, and `dokploy_environment` refresh their `env` attribute from the server, so a target without `lifecycle { ignore_changes = [env] }` plans to clear what this resource wrote on its next apply. Add that lifecycle block to the target, and do not set `env` on it.

~> **A change here does not redeploy the service.** Dokploy applies the variables on the next deploy of the application or the compose. An environment's shared variables reach a service on its next deploy too.

~> Values are written verbatim: no quotes, no escaping. A value cannot contain a line break; encode such a value first. Comment lines that a person wrote in the Dokploy UI do not survive an apply.

## Example Usage

```terraform
# The variables of an application, as a map. The resource owns the whole
# env text of its target, so the target must not manage `env` itself.
resource "dokploy_application" "api" {
  name           = "api"
  environment_id = dokploy_project.app.production_environment_id

  docker = {
    image = "ghcr.io/my-org/api:1.4.2"
  }

  # The application refreshes `env` from the server; without this block it
  # would plan to clear what the map wrote.
  lifecycle {
    ignore_changes = [env]
  }
}

resource "dokploy_environment_variables" "api" {
  application_id = dokploy_application.api.id

  variables = {
    PORT         = "8080"
    LOG_LEVEL    = "info"
    DATABASE_URL = "postgres://app:${var.db_password}@${dokploy_postgres.db.app_name}:5432/app"
  }
}

# Shared variables of an environment, which every service in it receives.
resource "dokploy_environment_variables" "staging" {
  environment_id = dokploy_environment.staging.id

  variables = {
    REGION        = "eu-west-1"
    FEATURE_FLAGS = "beta"
  }
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `variables` (Map of String) The variables, name to value. Use Terraform sensitive variables for secret values: the map itself is not sensitive, and Dokploy stores and returns it in cleartext.

### Optional

- `application_id` (String) Id of the application whose variables this resource owns. Set exactly one of `application_id`, `compose_id`, or `environment_id`. A change replaces the resource.
- `compose_id` (String) Id of the compose service whose variables this resource owns. Set exactly one of `application_id`, `compose_id`, or `environment_id`. A change replaces the resource.
- `environment_id` (String) Id of the environment whose variables this resource owns. Every service in the environment shares them. Set exactly one of `application_id`, `compose_id`, or `environment_id`. A change replaces the resource.

### Read-Only

- `id` (String) `application/<id>`, `compose/<id>`, or `environment/<id>`; also the import id.

## Import

Import is supported using the following syntax:

The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:

```shell
# Import by target: application/<id>, compose/<id>, or environment/<id>.
terraform import dokploy_environment_variables.api application/V1StGXR8_Z5jdHi6B-myT
```