Hono on AWS with SST
Create and deploy a Hono API in AWS with SST.
There are two ways to deploy a Hono app to AWS with SST.
We’ll use both to build a couple of simple apps below.
Examples
We also have a few other Hono examples that you can refer to.
Serverless
We are going to build a serverless Hono API, add an S3 Bucket for file uploads, and deploy it using a Lambda function.
Before you get started, make sure to configure your AWS credentials.
1. Create a project
Let’s start by creating our app.
We are picking the aws-lambda template.
Init SST
Now let’s initialize SST in our app.
Select the defaults and pick AWS. This’ll create a sst.config.ts
file in your project root.
2. Add an API
Let’s add a Hono API using an AWS Lambda. Update your sst.config.ts
.
We are enabling the function URL for this.
Start dev mode
Start your app in dev mode. This runs your functions Live.
This will give you the URL of your API.
3. Add an S3 Bucket
Let’s add an S3 Bucket for file uploads. Update your sst.config.ts
.
Link the bucket
Now, link the bucket to the API.
4. Upload a file
We want the /
route of our API to generate a pre-signed URL to upload a file to our S3 Bucket. Replace the Hello Hono route in src/index.ts
.
Install the npm packages.
Then add the relevant imports. We’ll use the extra ones below.
5. Download a file
We want the /latest
route of our API to generate a pre-signed URL to download the last uploaded file in our S3 Bucket. Add this to your routes in src/index.ts
.
Test your app
Let’s try uploading a file from your project root. Make sure to use your API URL.
Now head over to https://gyrork2ll35rsuml2yr4lifuqu0tsjft.lambda-url.us-east-1.on.aws/latest
in your browser and it’ll download the file you just uploaded.
6. Deploy your app
Now let’s deploy your app.
You can use any stage name here but it’s good to create a new stage for production.
Containers
We are going to create a Hono API, add an S3 Bucket for file uploads, and deploy it in a container with the Cluster
component.
Before you get started, make sure to configure your AWS credentials.
1. Create a project
Let’s start by creating our app.
We are picking the nodejs template.
Init SST
Now let’s initialize SST in our app.
Select the defaults and pick AWS. This’ll create a sst.config.ts
file in your project root.
2. Add a Service
To deploy our Hono app in a container, we’ll use AWS Fargate with Amazon ECS. Replace the run
function in
This creates a VPC, and an ECS Cluster with a Fargate service in it.
The dev.command
tells SST to instead run our Hono app locally in dev mode.
Start dev mode
Run the following to start dev mode. This’ll start SST and your Hono app.
Once complete, click on MyService in the sidebar and open your Hono 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 Hono 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 Hono route in our src/index.ts
.
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 src/index.ts
.
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:3000/latest
in your browser and it’ll show you what you just uploaded.
6. 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.
This builds our Hono app in a Docker image.
Let’s also add a .dockerignore
file in the root.
To compile our TypeScript file, we’ll need add the following to the tsconfig.json
.
Install TypeScript.
And add a build
script to our package.json
.
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 Hono 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 view logs from it.
You can create a free account and connect it to your AWS account.