NestJS on AWS with SST
Create and deploy an NestJS app to AWS with SST.
We are going to build an app with NestJS, add an S3 Bucket for file uploads, and deploy it to AWS in a container with SST.
Before you get started, make sure to configure your AWS credentials.
Also make sure you have Node 22.12. Or set the --experimental-require-module
flag. This’ll allow NestJS to import the SST SDK.
Examples
We also have a few other NestJS examples that you can refer to.
1. Create a project
Let’s start by creating our Nest app.
nest new aws-nestjs-containercd aws-nestjs-container
We are picking npm as the package manager.
Init SST
Now let’s initialize SST in our app.
npx sst@latest initnpm install
This’ll create a sst.config.ts
file in your project root.
To make sure the types in the sst.config.ts
are picked up, add the following to the tsconfig.json
.
{ "include": ["src/**/*", "test/**/*", "sst-env.d.ts"]}
2. Add a Service
To deploy our Nest app, let’s add an AWS Fargate container with Amazon ECS. Update your sst.config.ts
.
async run() { const vpc = new sst.aws.Vpc("MyVpc"); const cluster = new sst.aws.Cluster("MyCluster", { vpc });
new sst.aws.Service("MyService", { cluster, loadBalancer: { ports: [{ listen: "80/http", forward: "3000/http" }], }, dev: { command: "npm run start:dev", }, });}
This creates a VPC with an ECS Cluster, and adds a Fargate service to it.
The dev.command
tells SST to instead run our Nest app locally in dev mode.
Start dev mode
Run the following to start dev mode. This’ll start SST and your Nest app.
npx sst dev
Once complete, click on MyService in the sidebar and open your Nest app in your browser.
3. Add an S3 Bucket
Let’s add an S3 Bucket for file uploads. Add this to your sst.config.ts
below the Vpc
component.
const bucket = new sst.aws.Bucket("MyBucket");
Link the bucket
Now, link the bucket to the container.
new sst.aws.Service("MyService", { // ... link: [bucket],});
This will allow us to reference the bucket in our Nest app.
4. Upload a file
We want a POST
request made to the /
route to upload a file to our S3 bucket. Let’s add this below our getHello
method in our src/app.controller.ts
.
@Post()@UseInterceptors(FileInterceptor('file'))async uploadFile(@UploadedFile() file: Express.Multer.File): Promise<string> { const params = { Bucket: Resource.MyBucket.name, ContentType: file.mimetype, Key: file.originalname, Body: file.buffer, };
const upload = new Upload({ params, client: s3, });
await upload.done();
return 'File uploaded successfully.';}
Add the imports. We’ll use the extra ones below.
import { S3Client, GetObjectCommand, ListObjectsV2Command,} from '@aws-sdk/client-s3';import { Resource } from 'sst';import { Express } from 'express';import { Upload } from '@aws-sdk/lib-storage';import { FileInterceptor } from '@nestjs/platform-express';import { getSignedUrl } from '@aws-sdk/s3-request-presigner';import { Post, Redirect, UploadedFile, UseInterceptors } from '@nestjs/common';
const s3 = new S3Client({});
And install the npm packages.
npm install -D @types/multernpm install @aws-sdk/client-s3 @aws-sdk/lib-storage @aws-sdk/s3-request-presigner
5. Download the file
We’ll add a /latest
route that’ll download the latest file in our S3 bucket. Let’s add this below our uploadFile
method in src/app.controller.ts
.
@Get('latest')@Redirect('/', 302)async getLatestFile() { const objects = await s3.send( new ListObjectsV2Command({ Bucket: Resource.MyBucket.name, }), );
const latestFile = objects.Contents.sort( (a, b) => b.LastModified.getTime() - a.LastModified.getTime(), )[0];
const command = new GetObjectCommand({ Key: latestFile.Key, Bucket: Resource.MyBucket.name, }); const url = await getSignedUrl(s3, command);
return { url };}
Test your app
To upload a file run the following from your project root.
curl -F file=@package.json http://localhost:3000/
This should upload the package.json
. Now head over to http://localhost:3000/latest
in your browser and it’ll download you what you just uploaded.
5. Deploy your app
To deploy our app we’ll first add a Dockerfile
.
FROM node:22
WORKDIR /usr/src/appCOPY package*.json ./RUN npm installCOPY . .RUN npm run build
EXPOSE 3000CMD ["node", "dist/main"]
This just builds our Nest app in a Docker image.
Let’s also add a .dockerignore
file in the root.
distnode_modules
Now to build our Docker image and deploy we run:
npx sst deploy --stage production
You can use any stage name here but it’s good to create a new stage for production. This’ll give the URL of your Nest app deployed as a Fargate service.
✓ Complete MyService: http://jayair-MyServiceLoadBala-592628062.us-east-1.elb.amazonaws.com
Connect the console
As a next step, you can setup the SST Console to git push to deploy your app and monitor it for any issues.
You can create a free account and connect it to your AWS account.