Skip to content

Latest commit

 

History

History
184 lines (121 loc) · 5.3 KB

File metadata and controls

184 lines (121 loc) · 5.3 KB
description Docs for the sst.App construct in the @serverless-stack/resources package

The App construct extends cdk.App and is used internally by SST to:

  • Automatically prefix stack names with the stage and app name
  • Deploy the entire app using the same AWS profile and region

It is made available as the app in the lib/index.js of your SST app.

export default function main(app) {
  new MySampleStack(app, "sample");
}

Since it is initialized internally, the props that are passed to App cannot be changed.

Examples

Accessing app properties

The properties of the app can be accessed in the lib/index.js as:

app.name;
app.stage;
app.region;
app.account;

Specifying default function props

You can set some function props and have them apply to all the functions in the app.

export default function main(app) {
  app.setDefaultFunctionProps({
    timeout: 20,
    memorySize: 512,
    runtime: "go1.x",
    environment: { TABLE_NAME: "NOTES_TABLE" },
  });

  // Add stacks
}

Or if you need to access the Stack scope, you can pass in a callback.

import { StringParameter } from "@aws-cdk/aws-ssm";

export default function main(app) {
  app.setDefaultFunctionProps((stack) => ({
    timeout: 20,
    memorySize: 512,
    runtime: "go1.x",
    environment: {
      API_KEY: StringParameter.valueFromLookup(stack, "my_api_key"),
    },
  }));

  // Add stacks
}

You can also call setDefaultFunctionProps multiple times, and the props from each call will be merged. If a property is set more than once, the last value will be taken. Except for the environment, the layers, and the permissions properties, that will be merged.

export default function main(app) {
  app.setDefaultFunctionProps({
    timeout: 15,
    memorySize: 256,
    environment: { TABLE_NAME: "NOTES_TABLE" },
  });

  app.setDefaultFunctionProps({
    timeout: 20,
    environment: { BUCKET_NAME: "UPLAOD_BUCKET" },
  });

  // Add stacks
}

So in the above example, the functions by default will have a timeout of 20 seconds, memorySize of 256MB, and both the TABLE_NAME and the BUCKET_NAME environment variables set.

Setting a default removal policy

You can set a removal policy to apply to all the resources in the app. This is useful for ephemeral environments that need to clean up all their resources on removal.

import { RemovalPolicy } from "@aws-cdk/core";

export default function main(app) {
  // Remove all resources when the dev stage is removed
  if (app.stage === "dev") {
    app.setDefaultRemovalPolicy(RemovalPolicy.DESTROY);
  }

  // Add stacks
}

Note that, the setDefaultRemovalPolicy method isn't meant to be used for production environments.

Properties

The following properties are made available in addition to the properties of cdk.App.

name

Type : string

The name of the app. This comes from the name in your sst.json.

stage

Type : string

The stage the app is being deployed to. If this is not specified as the --stage option in the CLI, it'll default to the stage in your sst.json.

region

Type : string

The region the app is being deployed to. If this is not specified as the --region option in the CLI, it'll default to the region in your sst.json.

account

Type : string

The AWS account the app is being deployed to. This comes from the IAM credentials being used to run SST.

Methods

setDefaultFunctionProps

setDefaultFunctionProps(props: FunctionProps | ((stack: cdk.Stack) => FunctionProps))

Parameters

  • props FunctionProps | ((stack: cdk.Stack) => FunctionProps)

The default function props to be applied to all the Lambda functions in the app. These default values will be overridden if a Function sets its own props. Except for the environment, the layers, and the permissions properties, which will be merged.

Takes a FunctionProps. Or a callback function takes cdk.Stack that returns a FunctionProps.

setDefaultRemovalPolicy

setDefaultRemovalPolicy(policy: cdk.RemovalPolicy)

Parameters

The default removal policy that'll be applied to all the resources in the app. This can be useful to set ephemeral (dev or feature branch) environments to remove all the resources on deletion.

:::danger Make sure to not set the default removal policy to DESTROY for production environments. :::

logicalPrefixedName

logicalPrefixedName(logicalName: string): string

Parameters

  • logicalName string

Returns

  • string

Use this method to prefix resource names in your stacks to make sure they don't thrash when deployed to different stages in the same AWS account. This method will prefix a given resource name with the stage and app name. Using the format ${stage}-${name}-${logicalName}.