Skip to content

Reference Resources

Reference externally created resources in your app.

Referencing is the process of using some externally created resources in your SST app. But without having SST to manage them.

This is for when you have some resources that are either managed by a different team or a different IaC tool. Typically these are low-level resources and not SST’s built-in components.


How it works

When you create a resource in your SST app, two things happen. First, the resource is created by making a bunch of calls to your cloud provider. Second, SST makes a call to get the resource from the cloud provider. The data that it gets back is stored in your state.

When you reference a resource, it skips the creation step and just gets the resource. It does this every time you deploy. But the object you get in both cases is the same.


How to reference

You reference a resource by passing in a property of the resource. These properties are the same ones that you’d use if you were trying to import them.

We’ve compiled a list of the most commonly referenced low-level resources and their Import Properties.

Most low-level resources come with a static get method that use this property to look up the resource.


Look up the resource

Say you want to reference a previously created S3 Bucket with the following name.

mybucket-xnbmhcvd

We are going use the static aws.s3.BucketV2.get method.

sst.config.ts
const bucket = aws.s3.BucketV2.get("MyBucket", "mybucket-xnbmhcvd");

This gives you the same bucket object that you’d get if you had created this resource in your app. Let’s take it a step further.


Make it linkable

You can use the sst.Linkable component, to can link any property of this resource.

sst.config.ts
const storage = new sst.Linkable("MyStorage", {
properties: {
domain: bucket.bucketDomainName
}
});

Here we are using the domain name of the bucket as an example.


And link it to a function.

sst.config.ts
new sst.aws.Function("MyFunction", {
handler: "src/lambda.handler",
link: [storage]
});

Now you can use the SDK to access them at runtime.

src/lambda.ts
import { Resource } from "sst";
console.log(Resource.MyStorage.domain);

Check out the list of Import Properties if you want to reference other low-level resources.