Containers on AWS with SST
Create and deploy a container to AWS with SST.
We are going to build Express app in a container, add an S3 Bucket for file uploads, and deploy it to AWS using SST.
Before you get started:
1. Create a project
Let’s start by creating our Express app.
Init Express
Create your app by adding an index.mjs
to the root.
Init SST
Now let’s initialize SST in our app. Make sure you have the CLI installed.
This’ll create a sst.config.ts
file in your project root. And add a dev
script to your package.json
.
2. Add a Cluster
To deploy our Express app, let’s add an AWS Fargate container with Amazon ECS. Update your sst.config.ts
.
This creates a VPC, uses it for a new ECS Cluster, adds a Fargate service to it, and exposes it through http.
Create a Dockerfile
Let’s add a Dockerfile
in the root with the following.
3. Add an S3 Bucket
Let’s add a public
S3 Bucket for file uploads. Update your sst.config.ts
.
Link the bucket
Now, link the bucket to the container service.
This will allow us to reference the bucket in our Express app.
Start dev mode
Start your app in dev mode.
This will deploy your resources and start your Express app locally.
4. Upload a file
We want the /
route of our API to upload a file to our S3 Bucket. Let’s start by installing the npm packages we’ll use for the upload.
Add the relevant imports.
And add the route to your index.mjs
.
5. Download a file
We want the /latest
route of our app to generate a pre-signed URL to download the last uploaded file in our S3 Bucket. Add this to your index.mjs
.
Test your app
Let’s try uploading a file from your project root.
Now head over to http://localhost:80/latest
in your browser and it’ll download the file you just uploaded.
6. Deploy your app
Now let’s deploy your app.
This’ll give the URL of your Express app deployed as a Fargate service.