Linking
Link resources together and access them in a typesafe and secure way.
Resource Linking allows you to access your infrastructure in your runtime code in a typesafe and secure way.
-
Create a resource that you want to link to. For example, a bucket.
-
Link it to your function or frontend, using the
link
prop. -
Use the SDK to access the linked resource in your runtime in a typesafe way.
Working locally
The above applies to your app deployed through sst deploy
.
To access linked resources locally you’ll need to be running sst dev
. By default, the sst dev
CLI runs a multiplexer that also starts your frontend for you. This loads all your linked resources in the environment. Read more about sst dev
.
However if you are not using the multiplexer.
You’ll need to wrap your frontend’s dev command with the sst dev
command.
How it works
At high level when you link a resource to a function or frontend, the following happens:
-
The links that the resource exposes are injected into the function package.
-
The types to access these links are generated.
-
The function is given permission to access the linked resource.
Injecting links
Resource links are injected into your function or frontend package when you run sst dev
or sst deploy
. But this is done in a slightly different way for both these cases.
Functions
The functions in SST are tree shaken and bundled using esbuild. While bundling, SST injects the resource links into the globalThis
.
Frontends
The frontends are not bundled by SST. Instead, when they are built, SST injects the resource links into the process.env
object using the prefix SST_RESOURCE_
.
This is why when you are running your frontend locally, it needs to be wrapped in the sst dev
command.
Generating types
When you run sst dev
or sst deploy
, it generates the types to access the linked resources. These are generated as:
- A
sst-env.d.ts
file in the same directory of the nearestpackage.json
of the function or frontend that’s receiving the links. This contains the types for the resources that are linked to it. - A
.sst/types.generated.ts
file that can be referenced from asst-env.d.ts
. This is useful for monorepo packages that don’t have a function or frontend. For example, if you have a core package that contains some shared code.
You can check the generated sst-env.d.ts
types into source control. This will let your teammates see the types without having to run sst dev
when they pull your changes.
Extending linking
The examples above are built into SST’s components. You might want to modify the permissions that are granted as a part of these links.
Or, you might want to link other resources from the Pulumi/Terraform ecosystem. Or want to link a different set of outputs than what SST exposes.
You can do this using the sst.Linkable
component.
Link any value
The Linkable
component takes a list of properties that you want to link. These can be
outputs from other resources or constants.
You can optionally include permissions or bindings for the linked resource.
Now you can now link this resource to your frontend or a function.
Then use the SDK to access that at runtime.
Read more about sst.Linkable
.
Link any resource
You can also wrap any resource class to make it linkable with the Linkable.wrap
static method.
Now you create an instance of aws.dynamodb.Table
and link it in your app like any other SST
component.
And use the SDK to access it at runtime.
Modify built-in links
You can also modify the links SST creates. For example, you might want to change the permissions of a linkable resource.
This overrides the existing link and lets you create your own.
Read more about sst.Linkable.wrap
.