Deno on AWS with SST
Create and deploy a Deno app to AWS with SST.
We are going to build a hit counter using Deno and Redis. We’ll the deploy it to AWS in a container using SST.
Before you get started, make sure to configure your AWS credentials.
1. Create a project
Let’s start by creating our Deno app.
Init Deno Serve
Replace your main.ts
with the following.
This starts up an HTTP server by default on port 8000
.
Add scripts
Add the following to your deno.json
.
This adds a dev
script with a watcher.
Init SST
Make sure you have SST installed globally.
This’ll create an sst.config.ts
file in your project root.
2. Add a Cluster
To deploy our Deno app, let’s add an AWS Fargate container with Amazon ECS. Update your sst.config.ts
.
This creates a VPC with a bastion host, an ECS Cluster, and adds a Fargate service to it.
The dev.command
tells SST to instead run our Deno app locally in dev mode.
3. Add Redis
Let’s add an Amazon ElastiCache Redis cluster. Add this below the Vpc
component in your sst.config.ts
.
This shares the same VPC as our ECS cluster.
Link Redis
Now, link the Redis cluster to the container.
This will allow us to reference the Redis cluster in our Deno app.
Install a tunnel
Since our Redis cluster is in a VPC, we’ll need a tunnel to connect to it from our local machine.
This needs sudo to create a network interface on your machine. You’ll only need to do this once on your machine.
Start dev mode
Start your app in dev mode.
This will deploy your app, start a tunnel in the Tunnel tab, and run your Deno app locally in the MyServiceDev tab.
4. Connect to Redis
We want the /
route of our API to increment a counter in our Redis cluster. Let’s start by installing the npm packages we’ll use.
Add the relevant imports to your main.ts
.
Let’s update the /
route.
Test your app
Let’s head over to http://localhost:8000
in your browser and it’ll show the current hit counter.
You should see it increment every time you refresh the page.
5. Deploy your app
To deploy our app we’ll first add a Dockerfile
. This is building our app by running our build
script from above.
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 Deno 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.