Skip to content

Latest commit

 

History

History
174 lines (118 loc) · 4.39 KB

File metadata and controls

174 lines (118 loc) · 4.39 KB
description Docs for the sst.Stack construct in the @serverless-stack/resources package

The Stack construct extends cdk.Stack. It automatically prefixes the stack names with the stage and app name to ensure that they can be deployed to multiple regions in the same AWS account. It also ensure that the stack uses the same AWS profile and region as the app.

Initializer

new Stack(scope: Construct, id: string, props: StackProps)

Parameters

Examples

Creating a new stack

Create a new stack by adding this in lib/MyStack.js.

import { Stack } from "@serverless-stack/resources";

export default class MyStack extends Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    // Define your stack
  }
}

Adding to an app

Add it to your app in lib/index.js.

import MyStack from "./MyStack";

export default function main(app) {
  new MyStack(app, "my-stack");

  // Add more stacks
}

Here app is an instance of App.

Note that, setting the env for an individual stack is not allowed.

new MyStack(app, "my-stack", { env: { account: "1234", region: "us-east-1" } });

It will throw this error.

Error: Do not directly set the environment for a stack

This is by design. The stacks in SST are meant to be re-deployed for multiple stages (like Serverless Framework). And so they depend on the region and AWS profile that's passed in through the CLI. If a stack is hardcoded to be deployed to a specific account or region, it can break your deployment pipeline.

Accessing app properties

The stage, region, and app name can be accessed through the app object. In your stacks (for example, lib/MyStack.js) you can use.

class MyStack extends sst.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    scope.stage;
    scope.region;
    scope.name;
  }
}

And in TypeScript.

class MyStack extends sst.Stack {
  constructor(scope: sst.App, id: string, props?: sst.StackProps) {
    super(scope, id, props);

    scope.stage;
    scope.region;
    scope.name;
  }
}

You can use this to conditionally add stacks or resources to your app.

Prefixing resource names

You can optionally prefix resource names to make sure they don't thrash when deployed to different stages in the same AWS account.

You can do so in your stacks.

scope.logicalPrefixedName("MyResource"); // Returns "dev-my-sst-app-MyResource"

This invokes the logicalPrefixedName method in App that your stack is added to. This'll return dev-my-sst-app-MyResource, where dev is the current stage and my-sst-app is the name of the app.

Adding stack outputs

export default class MyStack extends Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    const topic = new Topic(this, "Topic");
    const queue = new Queue(this, "Queue");

    this.addOutputs({
      TopicArn: topic.snsTopic.topicArn,
      QueueArn: topic.sqsQueue.queueArn,
    });
  }
}

Adding stack exports

export default class MyStack extends Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    const topic = new Topic(this, "Topic");

    this.addOutputs({
      TopicArn: { value: topic.snsTopic.topicArn, exportName: "MyTopicArn" },
    });
  }
}

Accessing AWS account info

To access the AWS account and region your app is being deployed to, use the following in your Stack instances.

this.region;
this.account;

The region here is the same as the one you can find in the scope instance in the constructor.

Methods

An instance of Stack contains the following methods.

addOutputs

addOutputs(outputs: { [key: string]: string | cdk.CfnOutputProps })

Parameters

  • outputs { [key: string]: string | cdk.CfnOutputProps }

An associative array with the key being the output name as a string and the value is either a string as the output value or the cdk.CfnOutputProps.

StackProps

Extends cdk.StackProps.