www/src/content/docs/docs/live.mdx
Live is a feature of SST that lets you test changes made to your AWS Lambda functions in milliseconds. Your changes work without having to redeploy. And they can be invoked remotely.
:::tip
By default, sst dev will run all the functions in your app "live".
:::
It works by proxying requests from AWS to your local machine, executing it locally, and proxying the response back.
This setup of running your functions locally and proxying the results back allows you to do a couple of things:
https://my-api.com/hello is your API endpoint. Hitting that will run the local version of that function.
Live uses AWS AppSync Events to communicate between your local machine and the remote Lambda function.
When you run sst dev, it bootstraps a new AppSync Events API for the region you are using.
This is roughly what the flow looks like:
sst dev, it deploys your app and replaces the Lambda functions with a stub version.There are a couple of quirks with this setup that are worth noting.
Runtime change
The stub function that's deployed uses a different runtime than your Lambda function. You might run into this when you change the runtime in your config but the runtime of the Lambda function in the AWS Console doesn't change.
:::tip The stub function that’s deployed uses a different runtime than the actual function. :::
We use a different runtime because we want the function to be as fast as possible at proxying requests.
Live mode persists
If you kill the sst dev CLI, your functions are not run locally anymore but the stub function in AWS are still there. This means that it'll attempt to proxy requests to your machine and timeout.
:::tip
Only use sst dev in your personal stage.
:::
You can fix this by running sst deploy and it'll deploy the real version of your app. But the next time you run sst dev it'll need to deploy the stub back. This'll take a couple of minutes. So we recommend only using your personal stages for sst dev. And avoid flipping back and forth between dev and deploy.
When a function is running live it sets the SST_DEV environment variable to true. So in your Node.js functions you can access it using process.env.SST_DEV.
export async function main(event) {
const body = process.env.SST_DEV ? "Hello, Live!" : "Hello, World!";
return {
body,
statusCode: 200,
};
}
This is useful if you want to access some resources locally.
For example, when running locally you might want to connect to a local database. You can do that with the SST_DEV environment variable.
const dbHost = process.env.SST_DEV
? "localhost"
: "amazon-string.rds.amazonaws.com";
AWS AppSync Events that powers Live is completely serverless. So you don't get charged when it's not in use.
It's also pretty cheap. It's roughly $1.00 per million messages and $0.08 per million connection minutes. You can check out the details here.
This approach has been economical even for large teams with dozens of developers.
All the data stays between your local machine and your AWS account. There are no 3rd party services that are used.
Live also supports connecting to AWS resources inside a VPC.
By default your local functions cannot connect to resources in a VPC. You can fix this by either setting up a VPN connection or creating a tunnel.
To create a tunnel, you'll need to:
Enable the bastion host in your VPC.
new sst.aws.Vpc("MyVpc", { bastion: true });
Install the tunnel.
sudo sst tunnel install
This needs sudo to create the network interface on your machine. You only need to do this once.
:::note For NixOS users you will also need to declare the following sudo configuration to complete the setup:
security.sudo.extraRules = [
{
users = ["jay"]; # Your user
commands = [
{
command = "/opt/sst/tunnel tunnel start *";
options = ["NOPASSWD" "SETENV"];
}
];
}
];
:::
Run sst dev.
sst dev
This starts the tunnel automatically; notice the Tunnel tab on the left. Now your local environment can connect to resources in your VPC.
To set up a VPN connection, you'll need to:
Note that, the AWS Client VPC service is billed on an hourly basis but it's fairly inexpensive. Read more on the pricing here.
Since Live runs your functions locally, you can set breakpoints and debug your functions in your favorite IDE.
For VS Code, you'll need to enable Auto Attach from the Command Palette. Hit Ctrl+Shift+P or Cmd+Shift+P, type in Debug: Toggle Auto Attach and select Always.
:::note You need to start a new terminal in VS Code after enabling Auto Attach. :::
Now open a new terminal in VS Code, run sst dev, set a breakpoint in a function, and invoke the function.
Live is a feature that was created by SST when it first launched back in 2021. It's gone through a few different iterations since then.
| SST Version | Change |
|---|---|
| v0.5.0 | Then called Live Lambda, used a API Gateway WebSocket API and a DynamoDB table. |
| v2.0.0 | Switched to using AWS IoT, this was roughly 2-3x faster. |
| v3.3.1 | Switched to using AWS AppSync Events, which is even faster and handles larger payloads better. |