Skip to main content

Scopes

Scopes provide hierarchical organization and isolation for your infrastructure. They enable you to group related resources, manage different environments (dev, staging, prod), and control resource lifecycle at multiple levels.

What is a Scope?

A scope is a container for resources that provides:
  • Namespacing: Unique resource identification within a scope
  • State isolation: Each scope maintains its own state
  • Hierarchical organization: Scopes can contain child scopes
  • Environment separation: Different stages (dev, prod) are separate scopes
  • Credential management: Scopes can override provider credentials
Every Alchemy application has at least two scopes: the root scope (app name) and a stage scope (e.g., “dev”, “prod”).

Scope Hierarchy

Alchemy applications have a built-in scope hierarchy:
The fully qualified name (FQN) reflects this hierarchy:

Creating Scopes

Application Scope

Create the root application scope with alchemy():
The stage scope (“dev”) is automatically created based on the --stage flag or ALCHEMY_STAGE environment variable.

Child Scopes

Create child scopes with alchemy.run():

Scope Properties

Scopes provide access to configuration and metadata:

Scope Options

Stage Configuration

Specify which environment to deploy to:
Or programmatically:
Different stages maintain completely separate state. Resources in “dev” are independent from resources in “prod”.

Deployment Phases

Scopes operate in different phases:

Local Development

Enable local simulation mode:
Local mode allows you to develop and test infrastructure code without deploying to the cloud.

Watch Mode

Automatically redeploy on file changes:
Resources can detect watch mode:

Scope State Management

Scopes can store and retrieve custom data:
Scope state is separate from resource state and persists across deployments.

Physical Name Generation

Scopes provide utilities for generating deterministic resource names:
Format: {appName}-{scope-chain}-{id}-{stage}

Provider Credentials

Scopes can override provider credentials:

Scope Lifecycle

Initialization

Scopes are initialized when created:

Finalization

Finalization handles cleanup and orphan detection:
1

Execute deferred operations

Any operations registered with scope.defer() are executed
2

Finalize child scopes

All child scopes are finalized recursively
3

Detect orphaned resources

Resources that exist in state but not in code are identified
4

Destroy orphans

Orphaned resources are deleted using the configured destroy strategy
5

Cleanup processes

Any cleanup functions registered with scope.onCleanup() are called
Always call app.finalize() at the end of your script. Without it, orphaned resources won’t be cleaned up.

Process Management

Scopes can spawn and manage long-running processes:
Processes spawned with scope.spawn() are automatically stopped when the process exits.

Cleanup Handlers

Register cleanup functions to run when the process exits:

Scope Isolation

Each scope maintains isolated state:

Error Handling

Scopes track errors and propagate them:

Adoption Mode

Adopt existing resources that aren’t yet managed:
Adoption is useful when migrating existing infrastructure to Alchemy.

Quiet Mode

Suppress creation/update/deletion logs:

Best Practices

1

Use meaningful scope names

Choose names that describe the purpose: "api", "database", "storage"
2

Organize by lifecycle

Group resources that should be deployed/destroyed together
3

Leverage scope chain

Access parent scope properties via scope.parent when needed
4

Always finalize

Call await app.finalize() to ensure proper cleanup
5

Use stage isolation

Maintain separate stages for dev, staging, and production

Advanced Patterns

Conditional Scopes

Create scopes conditionally:

Scoped Secrets

Different scopes can use different passwords:

Next Steps

Resources

Learn about creating and managing resources

State Management

Understand how state is stored and managed

Secrets

Secure sensitive configuration values

Lifecycle

Deep dive into deployment phases