typescriptaws-lambdasst

How do I read the current stage of SST in a Function (AWS Lambda)


I have an SST project, and I would like to read the current stage in order to determine if the function is running in dev mode/testing mode or production in order to switch some behaviour. My root sst.env.config looks something like this:

export default $config({
  app(input) {
    return {
      name: "my-project",
      removal: input?.stage === "production" ? "retain" : "remove",
      home: "aws",
    };
  },
  async run() {
    // some imports here
    return {
      // some exports here
      Region: aws.getRegionOutput().name,
    };
  },
});

I see in there that I can read the current stage in the "input" parameter of the app function, but I have no idea how I get it out of the SST context into the Lambda context. There is some documentation from SST 2.x that shows you can do it like this:

import { Config } from "sst/node/config";

Config.APP;
Config.STAGE;

But that import no longer works in 3.x, since you are now expected to import your local sst.env.d.ts file, not a dependency. I have also tried to get the stage via the Resource class, but no dice.

Does anyone have an idea on how to do this in SST 3.x?


Solution

  • In your lambda function you can now import Resource from "sst":

    import { Resource } from "sst"
    
    console.log(Resource.App.stage)