Skip to content

Share Across Stages

Share resources across stages in your app.

You define all the resources in your app in your sst.config.ts. These resources then get created for each stage that you deploy to.

However, there might be some cases where you don’t want to recreate certain resources for every stage.


Why share

You typically want to share for cases where:

  • Resources that are expensive and their pricing is not truly pay-per-use, like your Postgres cluster.
  • Or, if they contain data that these new stages need to reuse. For example, your PR stages might just be for testing against your staging data and don’t need to recreate some resources.

While it might be tempting to share more resources across stages, we only recommend doing it for the above cases.


How to share

To help with this some SST components come with a static get method. These components are typically ones that people want to be able to share. Here are some components that have this:

If you’d like us to add to this list, feel free to open a GitHub issue.

It’s worth noting that complex components like our frontends, Nextjs, or StaticSite, are not likely to be supported. Both because they are made up of a large number of resources. But also because they really aren’t worth sharing across stages.

Let’s look at an example.


Example

The static get in the Bucket component has the following signature. It takes the name of the component and the name of the existing bucket.

get(name: string, bucketName: string)

Imagine you create a bucket in the dev stage. And in your personal stage frank, instead of creating a new bucket, you want to share the bucket from dev.

sst.config.ts
const bucket = $app.stage === "frank"
? sst.aws.Bucket.get("MyBucket", "app-dev-mybucket-12345678")
: new sst.aws.Bucket("MyBucket");

We are using $app.stage, a global to get the current stage the CLI is running on. It allows us to conditionally create the bucket.

Here app-dev-mybucket-12345678 is the auto-generated bucket name for the bucket created in the dev stage. You can find this by outputting the bucket name in the dev stage.

sst.config.ts
return {
bucket: bucket.name
};

And it’ll print it out on sst deploy.

bucket: app-dev-mybucket-12345678

You can read more about outputs in the run function.