What is SST.dev
Serverless Stack Toolkit or SST is an open-source infrastructure-as-code framework to help developers and companies create full-stack applications leveraging AWS serverless technologies.
SST is a framework used for building full-stack applications, like Rails, Laravel, or Django but it also includes building the application infrastructure in AWS. SST is an extension of AWS CDK, which also provides a higher level of abstraction, automating resource provisioning and generating CloudFormation templates, resulting in faster and more consistent infrastructure deployment and management.
Live local development is one of the main selling point of SST and it was only made possible using AWS CDK. Originally, the team tried using Serverless.com but they had some issues with live local development and they didn’t prefer defining infrastructure as code using YAML or JSON.
SST has built-in support and libraries for building AWS Lambda, Amazon API Gateway, Amazon S3, Amazon EventBridge and more with a few lines of code. There are even templates and examples that can be used to jump-start your application and infrastructure.
Typescript is the chosen programming language for SST due to its readability, succinctness, and enjoyment to work with.
What are Constructs?
A Construct is a TypeScript or JavaScript class that represents the functionality of your program. The constructs of SST are developed on top of AWS CDK. They are intended to solve specific use situations and provide sensible defaults that make using AWS easier.
SST applications rely on constructs as their foundational building blocks. They empower you to define your application’s infrastructure by combining multiple AWS resources into cohesive and functional units.
Types of Constructs
Core Construct
A Core Construct is used by every SST app.
// Stack is an example of core construct |
Frontend Construct
A Frontend Construct deploys frontend code to AWS.
// Static construct |
Database Construct
A Database Construct is used to add managed database to your app.
// RDS Construct |
API Construct
An API Construct adds a dedicated API to your app.
// Cron Construct |
Async Construct
An Async Construct includes async features like cron jobs, long-running jobs, queues, etc.
// API Construct |
S3 Bucket Construct
An S3 Bucket Construct for creating S3 buckets and assigning settings to it like notifications and more.
// S3 Bucket construct |
How are CDK constructs used in SST constructs?
SST is an abstraction of CDK, meaning it could extend an existing CDK, or the SST will create its own Typescript class and use those CDK constructs in the class. Suppose you look at the S3 bucket construct example, inside the bucket class. In that case, it imports the “aws-cdk-lib/aws-s3” and use the BucketProps and IBucket interfaces to be used as props validator and EventType and HttpMethods as enumerated values for bucket events and HTTP methods respectively.
Who is SST for?
SST.dev is a rapid development tool for teams building web and mobile applications that require serverless back-ends and want to have a quick product iteration. It greatly improves development productivity since you can run and debug your application locally. SST includes the Live Lambda Development environment, which provides a first-class local development environment for building applications.
SST Design Principles
By following these principles, you can easily bootstrap your application and still tailor it to your needs as your application grows.
- Zero-config – If you’re starting with a new tooling, you will find yourself learning a lot of things like configuring multiple plugins, tuning Webpack, Babel, TypeScript, and setting up testing frameworks and linters. SST has batteries included for your applications to run without worrying about configuration.
- Progressive disclosure – is a design and development approach that focuses on delivering features and functionalities incrementally, based on the needs and the complexity of the application.
The example below defines a simple API where you need only two handlers.
new Api(stack, “Api”, { |
You can customise it even further if you require a table to be accessed in your handler.
new Api(stack, “Api”, { |
Lastly, if you need deployment stages you can add them too!
new Api(stack, “Api”, { |
- An escape hatch – Some open-source frameworks will force you to be locked into their framework (sometimes called vendor lock-in). This means if you require a solution that’s not supported by the framework, you’ll be forced to eject, wait for the source code maintainer to add the fix, or fix it yourself. One of the biggest selling points of SST is you won’t be locked in, so you can always fall back to native CDK constructs without ejecting from the framework. Below is an example of CDK construct building an ECS Cluster.
import * as ecs from “aws-cdk-lib/aws-ecs”; |
Other tools for building serverless apps in AWS
AWS Amplify
AWS SAM
AWS developed AWS SAM (Serverless Application Model) in response to the serverless framework. Although it extends AWS CloudFormation, the developer experience is less desirable but there is always that large community which can help you on your development journey.
If you’re building full-stack application, it lacks built-in support for frontend technologies like Next.js, Svelte, Remix, or Astro and does not offer simple means to link the frontend with the backend, making it challenging for creating contemporary full-stack apps. On the bright side, SAM has addressed the issue with long redeployment durations using aws sam cli sync.
Serverless Framework
Serverless.com is a platform that provides resources, tools, and frameworks for building and deploying serverless applications. It aims to simplify the development process by abstracting away the underlying infrastructure concerns and allowing developers to focus solely on writing code for their applications’ business logic.
However, due to several restrictions, it is not suitable for creating contemporary full-stack apps. Its Next.js plugin is no longer updated (last release on March 2022), and there are no official plugins for other frameworks, hence it lacks proper compatibility for modern frontends. Second, integrating the frontend and backend is not simple because it mainly relies on AWS CloudFormation, which is challenging and demands substantial AWS expertise. Last but not least, the local development environment is subpar; either AWS services must be mocked, or lengthy redeployment periods result from each change that is performed.
What can you build with SST?
- Front-end applications – SST natively supports React.js, Next.js, Vue.js, Svelte, Gatsby and Angular.
- API development – With SST, you can easily create a REST API and WebSocket API with AWS API Gateway and GraphQL API using Pothos and AppSync.
- Native Mobile app development – If you’re into creating mobile apps, it also supports the development and deployment of Expo and Flutter using serverless API.
- Data Store – A data store is a place where you can save and retrieve data, whether it’s an object in the form of a blob, a string, plain text or encrypted data. With SST.dev constructs, you can create Amazon RDS, AWS DynamoDB and NoSQL serverless databases.
- Testing – SST already comes with testing frameworks to test your front-end, back-end and infrastructure codes.
SST has more advanced features that you might need in your project as your application grows, such as integrated monitoring tools like Datadog, Sentry, New Relic, adding Lambda Layers, and setting up IAM Credentials and Permission Boundaries. Their documentation is top notch and their development team updates the framework quite frequently.
Known issues
The list of known issues can be found here. These include:
- Mismatched versions of AWS CDK packages
There is a known issue in AWS CDK when using mismatched versions of their NPM packages. This means that all your AWS CDK packages in your package.json should use the same version. And since SST uses AWS CDK internally, this means that your app needs to use the same versions as well.
- CloudFront CacheBehaviors limit exceeded
While deploying a frontend app, SST creates a cache behaviour for each top-level file and folder in the assets directory. However, there’s a default limit of 25 cache behaviors allowed per distribution. Therefore, if there are more than 20 top-level files and folders, deployment will fail, displaying an error:
Limit exceeded for resource of type ‘AWS::CloudFront::Distribution’. Reason: Your request contains more CacheBehaviors than are allowed per distribution.
Alternatively, a limit increase can be requested through AWS Support.
Conclusion
SST (Serverless Stack Toolkit) is a great choice if you’re looking for a full-stack serverless application framework that offers various practical benefits whether you’re starting small or scaling to a complex application, including:
- It enables sharing types, secrets, and environment variables across the application.
- SST provides built-in support for essential tasks like writing database migrations, unit tests, and integration tests for the application code.
- It also facilitates deploying to separate environments, allowing for a pull request (PR) workflow and offers higher-level constructs for common backend features such as APIs, databases, and cron jobs.
- Additionally, SST provides typesafe libraries for Lambda functions, enhancing development efficiency and reliability.
In my next blog, we will set up a full-stack application using SST.