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

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.kosli.com/feedback

```json
{
  "path": "/terraform-reference/resources/environment",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# kosli_environment resource

> Manages a Kosli environment. Environments represent deployment targets where artifacts are deployed. Supports physical environment types: K8S, ECS, S3, docker, server, and lambda.

Manages a Kosli environment. Environments represent deployment targets where artifacts are deployed. Supports physical environment types: K8S, ECS, S3, docker, server, and lambda.

<Warning>
  This resource manages the environment configuration and tags. To attach compliance policies, use the [`kosli_policy_attachment` resource](/terraform-reference/resources/policy_attachment). For querying environment metadata such as `last_modified_at`, `last_reported_at`, and `archived` status, use the [`kosli_environment` data source](/terraform-reference/data-sources/environment).
</Warning>

Kosli environments track deployments and provide visibility into what's running in your infrastructure. Physical environments represent actual runtime locations such as:

* **K8S**: Kubernetes clusters
* **ECS**: Amazon Elastic Container Service clusters
* **S3**: Amazon S3 buckets
* **docker**: Docker containers
* **server**: Bare-metal or VM servers
* **lambda**: AWS Lambda functions

<Note>
  For aggregating multiple physical environments into logical groups, use the [`kosli_logical_environment` resource](/terraform-reference/resources/logical_environment).
</Note>

<Note>
  To attach compliance policies to environments, use the [`kosli_policy_attachment` resource](/terraform-reference/resources/policy_attachment).
</Note>

## Example usage

```terraform theme={"theme":"dracula","languages":{"custom":["/languages/rego.json"]}}
terraform {
  required_providers {
    kosli = {
      source = "kosli-dev/kosli"
    }
  }
}

# Basic K8S environment
resource "kosli_environment" "production_k8s" {
  name        = "production-k8s"
  type        = "K8S"
  description = "Production Kubernetes cluster"
}

# K8S environment with tags
resource "kosli_environment" "tagged" {
  name        = "production-k8s-tagged"
  type        = "K8S"
  description = "Production cluster managed by Terraform"
  tags = {
    managed-by  = "terraform"
    environment = "production"
    team        = "platform"
  }
}

# ECS environment with scaling
resource "kosli_environment" "staging_ecs" {
  name            = "staging-ecs"
  type            = "ECS"
  description     = "Staging ECS cluster"
  include_scaling = true
}

# S3 environment
resource "kosli_environment" "data_lake" {
  name        = "data-lake-s3"
  type        = "S3"
  description = "Data lake S3 bucket environment"
}

# Docker environment
resource "kosli_environment" "local_docker" {
  name = "local-docker"
  type = "docker"
}

# Server environment
resource "kosli_environment" "production_servers" {
  name            = "production-servers"
  type            = "server"
  description     = "Production bare-metal servers"
  include_scaling = false
}

# Lambda environment
resource "kosli_environment" "serverless_functions" {
  name        = "serverless-lambda"
  type        = "lambda"
  description = "AWS Lambda functions"
}
```

## Environment types

The `type` attribute must be one of the following physical environment types:

* `K8S` - Kubernetes clusters
* `ECS` - Amazon Elastic Container Service
* `S3` - Amazon S3 buckets
* `docker` - Docker containers
* `server` - Bare-metal or VM servers
* `lambda` - AWS Lambda functions

## Configuration options

### Include scaling

The `include_scaling` attribute (default: `false`) determines whether scaling events in the environment should be tracked. This is useful for environments with auto-scaling where you want to monitor scale-up and scale-down events.

## Import

Environments can be imported using their name:

```shell theme={"theme":"dracula","languages":{"custom":["/languages/rego.json"]}}
#!/bin/bash

# Import an existing environment by name
terraform import kosli_environment.production_k8s production-k8s

# Import multiple environments
terraform import kosli_environment.staging_ecs staging-ecs
terraform import kosli_environment.data_lake data-lake-s3
```

## Monitoring environments

For querying environment metadata such as `last_modified_at` and `last_reported_at` timestamps, use the [`kosli_environment` data source](/terraform-reference/data-sources/environment). This is useful for monitoring and creating conditional logic based on environment state.

## Schema

### Required

* `name` (String) Name of the environment. Must be unique within the organization. Changing this will force recreation of the resource.
* `type` (String) Type of the environment. Valid values: `K8S`, `ECS`, `S3`, `docker`, `server`, `lambda`. Changing this will force recreation of the resource.

### Optional

* `description` (String) Description of the environment. Explains the purpose and characteristics of this deployment target.
* `include_scaling` (Boolean) Whether to include scaling information when reporting environment snapshots. Defaults to `false`.
* `tags` (Map of String) Key-value pairs to tag the environment. Tags are applied via a diff — only changed tags are sent to the API. An empty map (`tags = {}`) removes all tags.
