Resource()
Defines a custom resource provider that handles the complete lifecycle (create, update, delete) of a cloud resource. Resources are the building blocks of Alchemy applications.Signature
Parameters
string
required
The unique type identifier for this resource, conventionally in the format
"provider::ResourceName" (e.g., "cloudflare::Worker", "aws::S3Bucket").ProviderOptions
ResourceLifecycleHandler
required
The async function that manages the resource lifecycle. Receives a
Context object as this, along with id and props parameters.Returns
function
A callable function that creates instances of the resource. When called with an
id and props, it returns a Promise that resolves to the resource output.Context API
The resource handler function receives aContext object as this with the following properties and methods:
'create' | 'update' | 'delete'
The current lifecycle phase of the resource.
Output | undefined
The previous output of the resource (undefined during creation).
Props | undefined
The previous props of the resource (undefined during creation).
Scope
The current scope containing this resource.
string
The resource identifier.
string
The fully qualified name of the resource.
function
Terminates the resource lifecycle and marks it for deletion. Used in the delete phase.
function
Signals that the resource should be replaced (deleted and recreated). Used when an immutable property changes.
function
Retrieves a value from the resource’s internal state storage.
function
Stores a value in the resource’s internal state storage.
function
Registers a cleanup function to run when the process exits.
Examples
Basic Resource Provider
Resource with Secret Handling
Resource with Adoption
Resource with Immutable Properties
Related
- Context - Context API reference
- alchemy.secret() - Secret handling
- Scope - Scope API reference