| 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.
new Stack(scope: Construct, id: string, props: StackProps)Parameters
- scope
Construct - id
string - props
StackProps
Create a new stack by adding this in stacks/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
}
}Add it to your app in stacks/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.
The stage, region, and app name can be accessed through the app object. In your stacks (for example, stacks/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.
You can set some function props and have them apply to all the functions in a stack. This must be called before any functions have been added to the stack; so that all functions will be created with these defaults.
class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
this.setDefaultFunctionProps({
timeout: 20,
memorySize: 512,
runtime: "go1.x",
environment: { TABLE_NAME: "NOTES_TABLE" },
});
// Start adding resources
}
}It'll also override any props set by the App's setDefaultFunctionProps, while merging the environment and permission props.
You can also use addDefaultFunctionPermissions, addDefaultFunctionEnv, and addDefaultFunctionLayers to progressively add more permissions, environment variables, and layers to the defaults. These can be called multiple times and from anywhere.
However, they only affect the functions that are created after the call.
class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
new Api(this, "Api1", {
routes: {
"GET /": "src/hello.main",
},
});
this.addDefaultFunctionEnv({
TABLE_NAME: "NOTES_TABLE"
});
this.addDefaultFunctionPermissions(["s3"]);
this.addDefaultFunctionLayers([mylayer]);
new Api(this, "Api2", {
routes: {
"GET /": "src/world.main",
},
});
// Add more resources
}
}So in the above example, the addDefaultFunctionPermissions and addDefaultFunctionEnv calls will only impact the functions in Api2.
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.
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,
});
}
}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" },
});
}
}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.
To set permission boundary on all IAM users and roles created in your Stack instances.
import * as iam from 'aws-cdk-lib/aws-iam';
class MyStack extends sst.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const boundary = new iam.ManagedPolicy(this, "Boundary", {
statements: [
new iam.PolicyStatement({
effect: iam.Effect.DENY,
actions: ["iam:*"],
resources: ["*"],
}),
],
});
iam.PermissionsBoundary.of(this).apply(boundary);
}
}An instance of Stack contains the following methods.
getAllFunctions(): FunctionReturns
Returns all the Function instances in this stack.
setDefaultFunctionProps(props: FunctionProps)Parameters
- props
FunctionProps
The default function props to be applied to all the Lambda functions in the stack. These default values will be overridden if a Function sets its own props. This cannot be called after any functions have been added to the stack.
:::note
The setDefaultFunctionProps function must be called before any functions have been added.
:::
Takes the FunctionProps.
This overrides the props from the App's setDefaultFunctionProps, while merging the permissions and environment props.
addDefaultFunctionEnv(env: { [key: string]: string })Parameters
- env
{ [key: string]: string }
Adds additional default environment variables to be applied to all Lambda functions in the stack.
:::note
Only functions created after a addDefaultFunctionEnv call will contain the new values.
:::
addDefaultFunctionPermissions(permissions: Permissions)Parameters
- permissions
Permissions
Adds additional default Permissions to be applied to all Lambda functions in the stack.
:::note
Only functions created after a addDefaultFunctionPermissions call will contain the new values.
:::
addDefaultFunctionLayers(layers: lambda.ILayerVersion[])Parameters
- layers
lambda.ILayerVersion[]
Adds additional default layers to be applied to all Lambda functions in the stack.
:::note
Only functions created after a addDefaultFunctionLayers call will contain the new values.
:::
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.
Extends cdk.StackProps.