Global
Reference doc for the Global `$` library.
The Global library is a collection of $
functions and variables that are available in the run
function, of your sst.config.ts
.
You don’t need to import the Global library. It’s available in the run
function of your sst.config.ts
.
For example, you can get the name of your app in your app config using $app.name
.
The variables contain the context of the app that’s being run. While the functions help you work with the Outputs of components.
Variables
$app
$app.name
Type string
The name of the current app.
$app.providers
Type undefined
|
Record
<
string
, any
>
The providers currently being used in the app.
$app.removal
Type “
remove
”
|
“
retain
”
|
“
retain-all
”
The removal policy for the current stage. If removal
was not set in the sst.config.ts
, this will be return its default value, retain
.
$app.stage
Type string
The stage currently being run. You can use this to conditionally deploy resources based on the stage.
For example, to deploy a bucket only in the dev
stage:
$dev
Type boolean
Returns true
if the app is running in sst dev
.
$util
Type @pulumi/pulumi
A convenience reference to the the util
module from Pulumi.
This is useful for working with components. You can use these without importing or installing the Pulumi SDK. For example, to create a new asset, you can:
This is equivalent to doing:
Functions
$asset
Parameters
-
assetPath
string
Returns FileArchive
|
FileAsset
Packages a file or directory into a Pulumi asset. This can be used for Pulumi resources that take an asset as input.
When the given path is a file, it returns a
FileAsset
. If the
path is a directory, it returns a
FileArchive
with the
zipped contents of the directory.
Relative paths are resolved relative to the root of the app. While, absolute paths are used as is.
If you have a file inside the images
directory at the root of your app, you can upload it
to S3 on deploy.
You can also use this to zip up the files in the files/
directory and upload it to S3.
$concat
Parameters
-
params
any
[]
Returns Output
<
string
>
Takes a sequence of Output values or plain JavaScript values, stringifies each, and concatenates them into one final string.
This is takes care of resolving the Output values for you. Say you had a bucket:
Instead of having to resolve the bucket name first::
You can directly do this:
$interpolate
Parameters
-
literals
TemplateStringsArray
<
>
-
placeholders
any
[]
Returns Output
<
string
>
Use string interpolation on Output values.
This is takes care of resolving the Output values for you. Say you had a bucket:
Instead of resolving the bucket name first:
You can directly do this:
$jsonParse
Parameters
-
text
Input
<
string
>
-
reviver?
JSON.parse reviver
Returns Output
<
any
>
Takes an Output value or plain JavaScript value, uses JSON.parse
on the resolved JSON string to turn it into a JSON object.
So for example, instead of doing of resolving the value first:
You can directly do this:
$jsonStringify
Parameters
-
obj
any
-
replacer?
JSON.stringify replacer
-
space?
string
|
number
Returns Output
<
string
>
Takes an Output value or plain JSON object, uses JSON.stringify
on the resolved JSON object to turn it into a JSON string.
So for example, instead of doing of resolving the value first:
You can directly do this:
$resolve
Parameters
-
val
Record
<
string
,Input
<
T
>
>
Returns Output
<
Record
<
string
, T
>
>
Wait for a list of Output values to be resolved, and then apply a function to their resolved values.
Say you had a couple of S3 Buckets:
You can run a function after both of them are resolved:
$transform
Parameters
-
resource
Component Class
-
cb
(args, opts) => void
Returns void
Register a function that’ll be called when a component of the given type is about to be created. This is useful for setting global defaults for your components.
The function takes the arguments and options that are being passed to the component, and can modify them.
For example, to set a default runtime for all function components.
Here, args
and opts
are what you’d pass to the Function
component. Recall the
signature of the Function
component: