Resources
Resources are the fundamental building blocks in Alchemy. Each resource represents a piece of infrastructure—whether it’s a worker, database, storage bucket, or any other cloud service component.What is a Resource?
A resource in Alchemy is a TypeScript function that manages the complete lifecycle of an infrastructure component. Resources handle:- Creation: Provisioning new infrastructure
- Updates: Applying configuration changes
- Deletion: Cleaning up resources when no longer needed
- State tracking: Maintaining resource state across deployments
All resources implement the same lifecycle pattern, making them composable and predictable regardless of the underlying provider.
Resource Anatomy
Resources are defined using theResource() function and follow a consistent structure:
Resource Identity
Every resource has multiple forms of identity:Logical ID
The first parameter passed to a resource is its logical ID—a unique identifier within your application:"api" is the logical ID.
Fully Qualified Name (FQN)
The FQN is the complete path to a resource, including the app name, scopes, and logical ID:resource[ResourceFQN]:
Physical Name
The physical name is what appears in your cloud provider’s console. Alchemy generates deterministic names by default:Resource Lifecycle
Resources go through distinct phases during deployment:Create Phase
When a resource doesn’t exist in state:Update Phase
When a resource exists but props have changed:Delete Phase
When a resource is removed from your code:The
this.destroy() call is required in the delete phase—it signals Alchemy that cleanup is complete and state should be removed.Resource Context
Thethis context in a resource handler provides access to:
Resource Properties
Every Alchemy resource has these built-in properties:These symbols are used internally for type discrimination and resource tracking. You typically don’t need to access them directly.
Resource Dependencies
Resources can reference each other, creating automatic dependency graphs:- Creates resources in dependency order
- Updates dependent resources when dependencies change
- Deletes resources in reverse dependency order
Resource Replacement
Some property changes require replacing a resource:1
Alchemy creates the new resource
A new resource is created with the updated configuration
2
Alchemy deletes the old resource
Once the new resource is ready, the old one is destroyed
3
State is updated
State files are updated to track the new resource
Type Safety
Alchemy provides full TypeScript type safety:Custom State
Resources can store custom data in state:Local Development
Resources can provide local alternatives for development:Resource Registration
Resources are automatically registered in a global registry when defined:The global registry enables Alchemy to resolve resources during deletion even if the code has been removed.
Best Practices
1
Use descriptive logical IDs
Choose IDs that clearly describe the resource’s purpose:
"api", "user-db", "image-bucket"2
Handle all phases
Always implement create, update, and delete logic comprehensively
3
Validate immutable properties
Detect immutable property changes and call
this.replace() when needed4
Provide defaults
Use
this.scope.createPhysicalName(id) for default naming5
Support local development
Check
this.scope.local and provide local alternatives when possibleNext Steps
Scopes
Learn how to organize resources with scopes
State Management
Understand how Alchemy tracks resource state
Secrets
Secure sensitive values in your infrastructure
Lifecycle
Deep dive into the resource lifecycle