Skip to main content

Creating Resources

Alchemy uses a pseudo-class pattern for defining infrastructure resources. Each resource is created using the Resource() function, which manages the complete lifecycle (create, update, delete) of your infrastructure.

Basic Resource Creation

Resources are created by calling a resource constructor function with an ID and props:

Resource IDs and Physical Names

Every resource has two important identifiers:

Resource ID

The ID is the logical identifier used in your Alchemy code:
  • Must be unique within a scope
  • Used to reference the resource in state
  • Cannot contain colons (:)

Physical Name

The physical name is the actual name in the cloud provider. By default, Alchemy generates this as:
Use custom names when you need to reference existing resources or follow specific naming conventions.

Resource Lifecycle

Alchemy automatically manages the complete lifecycle of your resources:

Create Phase

When you run your alchemy.run.ts script, new resources are created:

Update Phase

If you change resource properties and re-run, Alchemy updates the resource:

Delete Phase

Run with --destroy to delete all resources:
Alchemy tracks which resources exist in your code. If you remove a resource from your script, it will be automatically deleted on the next run (orphan cleanup).

Resource References

You can pass resources as properties to other resources:
Alchemy automatically:
  • Resolves resource dependencies
  • Creates resources in the correct order
  • Extracts the necessary properties for bindings

Concurrent Resource Creation

Resources can be created concurrently when they don’t depend on each other:
Use Promise.all() to create independent resources faster. Keep batches under 50 resources for optimal performance.

Adopting Existing Resources

If a resource already exists with the same name, you can adopt it:
Or per-resource:
Adoption will update the existing resource to match your configuration. Make sure this is intentional before enabling.

Resource Outputs

Every resource returns output properties you can use:
These outputs can be used to:
  • Display deployment information
  • Configure other resources
  • Pass to external systems

Error Handling

Alchemy provides clear error messages for common issues:
Common errors:
  • Duplicate resource ID: Using the same ID twice in a scope
  • Invalid resource ID: IDs containing colons or invalid characters
  • Resource conflicts: Resource already exists without adopt: true
  • Missing credentials: Provider credentials not configured

Best Practices

Next Steps