Express on AWS with SST
Create and deploy an Express app to AWS with SST.
You can use SST to deploy an Express app in a container to AWS. It uses Fargate and ECS.
We’ll build a simple app below.
Examples
We also have a few other Express examples that you can refer to.
We are going to build a hit counter using Express and Redis. We’ll the deploy it to AWS in a container.
Before you get started, make sure to configure your AWS credentials.
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.
This’ll create a sst.config.ts
file in your project root.
2. Add a Service
To deploy our Express app, let’s add an AWS Fargate container with Amazon ECS. Update your sst.config.ts
.
This creates a VPC with an ECS Cluster, and adds a Fargate service to it.
The dev.command
tells SST to instead run our Express app locally in dev mode.
Start dev mode
Run the following to start dev mode. This’ll start SST and your Express app.
Once complete, click on MyService in the sidebar and open your Express 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.
Link the bucket
Now, link the bucket to the container.
This will allow us to reference the bucket in our Express 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 Hello World route in our index.mjs
.
Add the imports. We’ll use the extra ones below.
And install the npm packages.
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 upload route in index.mjs
.
Test your app
To upload a file run the following from your project root.
This should upload the package.json
. Now head over to http://localhost:80/latest
in your browser and it’ll show you what you just uploaded.
5. Deploy your app
To deploy our app we’ll first add a Dockerfile
.
This just builds our Express app in a Docker image.
Let’s also add a .dockerignore
file in the root.
Now to build our Docker image and deploy we run:
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 Express app deployed as a Fargate service.
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.