Skip to main content

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
Optional configuration for the resource provider.
boolean
default:"false"
If true, the resource will be updated even if the inputs have not changed.
'sequential' | 'parallel'
default:"'sequential'"
The strategy to use when destroying this resource and its dependencies.
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 a Context 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