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

# Stack

> formae's unit of lifecycle: the resources you create, reconcile, and destroy together.

A stack is a collection of resources you choose to manage together. It is
formae's unit of lifecycle: reconciling a stack is what lets formae remove
resources that are no longer declared, and destroying a stack tears down
everything in it.

A stack is *not* a boundary for references. Resources in one stack can reference
resources in another through [resolvables](/documentation/concepts/resolvable).
The stack is about lifecycle, not about isolating dependencies.

You never create a stack up front. When you apply a forma, formae looks for a
stack by label and creates it if it doesn't exist yet:

```kotlin theme={"languages":{"custom":["/languages/pkl.json"]}}
new formae.Stack {
  label = "production-api"
  description = "Production API infrastructure"
}
```

Every resource in that forma is assigned to the stack automatically.

<Info>
  A stack cleans up after itself. When its last resource is destroyed, formae
  deletes the empty stack for you; you never remove one by hand.
</Info>

## One stack per resource

Each resource belongs to exactly one stack. A single owner is what keeps lifecycle
and state tracking unambiguous, and it stops two stacks from fighting over the
same resource. Decide which stack a resource lives in when you design your
infrastructure.

## Designing stack boundaries

Because the stack is the unit of lifecycle, its boundaries decide what gets
deployed, reconciled, and destroyed together. Common ways to draw them:

* **By environment**: `dev`, `staging`, `production`
* **By application or service**: `user-service`, `payments`, `analytics`
* **By layer**: `networking`, `databases`, `compute`

Smaller stacks give you finer-grained control; larger stacks keep related
resources together.

<Warning>
  Policies attach at the **stack** level. A [TTL](/documentation/concepts/policies/ttl)
  or [auto-reconcile](/documentation/concepts/policies/auto-reconcile) policy governs
  the whole stack, so a resource inherits whatever policy its stack carries. Factor
  this into where you draw boundaries: if two resources need different lifecycle
  policies, they belong in different stacks. (Resource-level policies are coming,
  which will relax this.)
</Warning>

## Version control

formae tracks resources and their lifecycle at the stack level. You can extract
any managed stack in your preferred format for version control, backup, or reuse
in another environment.

## See also

* [Forma](/documentation/concepts/forma): the declaration that creates and updates stacks.
* [Target](/documentation/concepts/target): where a stack's resources are applied.
* [Apply modes](/documentation/concepts/apply-modes): how reconcile uses stack boundaries to decide what to remove.
* [Policy](/documentation/concepts/policy): TTL and auto-reconcile policies, which are scoped to a stack.
