State Management
Alchemy maintains a persistent state file for each resource to track infrastructure across deployments. This state enables Alchemy to detect changes, perform updates, and clean up orphaned resources.What is State?
State in Alchemy is a JSON representation of your deployed infrastructure that includes:- Resource metadata: ID, type, fully qualified name
- Resource props: Input parameters from previous deployment
- Resource output: Actual values returned by the provider
- Custom data: Additional state stored by resources
- Status: Current lifecycle status of the resource
State files are how Alchemy knows what’s already deployed and what needs to change.
State Structure
Each resource’s state follows this structure:Where State is Stored
By default, state is stored in the.alchemy directory:
The
.alchemy directory should be added to .gitignore for local development. For production, use a persistent state store.State Lifecycle
Creating State
When a resource is first created:1
Resource creation
Alchemy invokes the resource handler in “create” phase
2
State initialization
State file is created with status “creating”
3
Provider call
Resource is provisioned via the provider’s API
4
State persistence
Final state is saved with status “created” and output values
Updating State
When resource props change:1
State comparison
Alchemy compares new props with props in state
2
Update detection
If different, resource handler is called in “update” phase
3
State update
Previous props are stored in
oldProps, new props replace props4
Provider update
Resource is updated via provider API
5
State save
Updated state is persisted with new output values
Deleting State
When a resource is removed from code:1
Orphan detection
During finalization, Alchemy detects resources in state but not in code
2
Deletion phase
Resource handler is called with phase “delete”
3
Cleanup
Resource is destroyed via provider API
4
State removal
State file is deleted
State Stores
Alchemy supports pluggable state storage backends.File System State Store (Default)
Stores state in local.alchemy directory:
Cloudflare State Store
Stores state in Cloudflare KV:AWS S3 State Store
Stores state in S3:Custom State Store
Implement your own state store:State Operations
Reading State
Access state within a resource:Custom State Data
Store additional data in state:Custom state data persists across deployments and is separate from resource props and output.
Scope State
Scopes can also store state:State Serialization
Alchemy automatically serializes complex types:Secrets
Secrets are encrypted before storage:Resources
Resource references are serialized by FQN:Dates and Special Types
Custom serializers handle complex types:State Locking
Alchemy uses mutexes to prevent concurrent state modifications:State locking prevents race conditions when multiple resources access shared state.
State Migration
When changing state stores:1
Export existing state
Read all state from current store
2
Initialize new store
Configure new state store
3
Import state
Write state to new store
4
Verify
Run deployment with
--read to verify state5
Cutover
Update all deployments to use new store
Debugging State
Inspect state files directly:State Best Practices
1
Use persistent storage in production
File system state doesn’t work in CI/CD or team environments
2
Never manually edit state
Let Alchemy manage state automatically
3
Back up state regularly
State is critical - losing it means losing track of your infrastructure
4
Use different stores per stage
Isolate dev and prod state completely
5
Monitor state size
Large state files can slow down deployments
Troubleshooting
State Corruption
If state is corrupted:Lost State
If state is lost:State Conflicts
If state conflicts with reality:Advanced Patterns
State Versioning
Track state versions:State Inspection
Expose state for debugging:Next Steps
Scopes
Understand scope-level state management
Resources
Learn how resources interact with state
Secrets
See how secrets are encrypted in state
Lifecycle
Understand state transitions during lifecycle