> ## 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/policy_attachment",
  "feedback": "Description of the issue"
}
```

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

</AgentInstructions>

# kosli_policy_attachment resource

> Attaches a Kosli policy to an environment. When this resource is destroyed, the policy is detached from the environment.

Attaches a Kosli policy to an environment (physical or logical). When this resource is destroyed, the policy is detached from the environment.

Both `environment_name` and `policy_name` are immutable: changing either attribute will destroy the existing attachment and create a new one.

<Note>
  Both the policy and environment must exist before creating an attachment. Use the [`kosli_policy` resource](/terraform-reference/resources/policy) and [`kosli_environment` resource](/terraform-reference/resources/environment) to manage them.
</Note>

## Example usage

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

# Attach a policy to an environment.
# Both the policy and environment must exist before creating the attachment.
resource "kosli_policy_attachment" "example" {
  environment_name = "my-environment"
  policy_name      = "my-policy"
}
```

## Import

Policy attachments can be imported using the composite ID `environment_name/policy_name`:

```shell theme={"theme":"dracula","languages":{"custom":["/languages/rego.json"]}}
# Import a policy attachment using the composite ID: environment_name/policy_name
terraform import kosli_policy_attachment.example my-environment/my-policy
```

## Schema

### Required

* `environment_name` (String) Name of the environment to attach the policy to. Changing this will force recreation of the resource.
* `policy_name` (String) Name of the policy to attach. Changing this will force recreation of the resource.
