Skip to content

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.

  1. Serverless
  2. Containers

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.

Terminal window
mkdir aws-hono && cd aws-hono
npm init -y
Init SST

Now let’s initialize SST in our app.

Terminal window
npx sst@latest init
npm install

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.

sst.config.ts
async run() {
const hono = new sst.aws.Function("Hono", {
url: true,
handler: "index.handler",
});
}

We are enabling the function URL for this.


Start dev mode

Start your app in dev mode. This runs your functions Live.

Terminal window
npx sst dev

This will give you the URL of your API.

Complete
Hono: https://gyrork2ll35rsuml2yr4lifuqu0tsjft.lambda-url.us-east-1.on.aws

3. Add an S3 Bucket

Let’s add an S3 Bucket for file uploads. Update your sst.config.ts.

sst.config.ts
const bucket = new sst.aws.Bucket("MyBucket");

Now, link the bucket to the API.

sst.config.ts
const hono = new sst.aws.Function("Hono", {
url: true,
link: [bucket],
handler: "index.handler",
});

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. Create an index.ts file and add the following.

index.ts
const app = new Hono()
.get("/", async (c) => {
const command = new PutObjectCommand({
Key: crypto.randomUUID(),
Bucket: Resource.MyBucket.name,
});
return c.text(await getSignedUrl(s3, command));
});
export const handler = handle(app);

Install the npm packages.

Terminal window
npm install hono @aws-sdk/client-s3 @aws-sdk/s3-request-presigner

Then add the relevant imports.

index.ts
import { Resource } from "sst";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import {
S3Client,
GetObjectCommand,
PutObjectCommand,
ListObjectsV2Command,
} from "@aws-sdk/client-s3";
import { Hono } from "hono";
import { handle } from "hono/aws-lambda";
const s3 = new S3Client({});

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 index.ts.

index.ts
const app = new Hono()
// ...
.get("/latest", async (c) => {
const objects = await s3.send(
new ListObjectsV2Command({
Bucket: Resource.MyBucket.name,
}),
);
const latestFile = objects.Contents!.sort(
(a, b) =>
(b.LastModified?.getTime() ?? 0) - (a.LastModified?.getTime() ?? 0),
)[0];
const command = new GetObjectCommand({
Key: latestFile.Key,
Bucket: Resource.MyBucket.name,
});
return c.redirect(await getSignedUrl(s3, command));
});

Test your app

Let’s try uploading a file from your project root. Make sure to use your API URL.

Terminal window
curl --upload-file package.json "$(curl https://gyrork2ll35rsuml2yr4lifuqu0tsjft.lambda-url.us-east-1.on.aws)"

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.

Terminal window
npx sst deploy --stage production

You can use any stage name here but it’s good to create a new stage for production.


Containers

We are going to build a hit counter Hono API with Redis. We’ll deploy it to AWS in a container using 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.

Terminal window
npm create hono@latest aws-hono-container
cd aws-hono-container

We are picking the nodejs template.

Init SST

Now let’s initialize SST in our app.

Terminal window
npx sst@latest init
npm install

Select the defaults and pick AWS. This’ll create a sst.config.ts file in your project root.


2. Add a Cluster

To deploy our Hono app in a container, we’ll use AWS Fargate with Amazon ECS. Replace the run function in

sst.config.ts
async run() {
const vpc = new sst.aws.Vpc("MyVpc", { bastion: true });
const cluster = new sst.aws.Cluster("MyCluster", { vpc });
cluster.addService("MyService", {
loadBalancer: {
ports: [{ listen: "80/http", forward: "3000/http" }],
},
dev: {
command: "npm run dev",
},
});
}

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 Astro site 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.

sst.config.ts
const redis = new sst.aws.Redis("MyRedis", { vpc });

This shares the same VPC as our ECS cluster.


Now, link the Redis cluster to the container.

sst.config.ts
cluster.addService("MyService", {
// ...
link: [redis],
});

This will allow us to reference the Redis cluster in our Astro site.


Install a tunnel

Since our Redis cluster is in a VPC, we’ll need a tunnel to connect to it from our local machine.

Terminal window
sudo npx sst tunnel install

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.

Terminal window
npx sst dev

This will deploy your app, start a tunnel in the Tunnel tab, and run your Astro site locally in the MyServiceDev tab.


4. Connect to Redis

We want the / route to increment a counter in our Redis cluster. Let’s start by installing the npm package we’ll use.

Terminal window
npm install ioredis

Add the relevant imports to your index.ts.

index.ts
import { Resource } from "sst";
import { Cluster } from "ioredis";
const redis = new Cluster(
[{ host: Resource.MyRedis.host, port: Resource.MyRedis.port }],
{
dnsLookup: (address, callback) => callback(null, address),
redisOptions: {
tls: {},
username: Resource.MyRedis.username,
password: Resource.MyRedis.password,
},
}
);

Let’s update the / route.

index.ts
app.get("/", async (c) => {
const counter = await redis.incr("counter");
return c.text(`Hit counter: ${counter}`);
});

Test your app

Let’s head over to http://localhost:3000 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.

View Dockerfile
Dockerfile
FROM node:20-alpine AS base
FROM base AS builder
RUN apk add --no-cache gcompat
WORKDIR /app
COPY package*json tsconfig.json src ./
# Copy over generated types
COPY sst-env.d.ts* ./
RUN npm ci && \
npm run build && \
npm prune --production
FROM base AS runner
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 hono
COPY --from=builder --chown=hono:nodejs /app/node_modules /app/node_modules
COPY --from=builder --chown=hono:nodejs /app/dist /app/dist
COPY --from=builder --chown=hono:nodejs /app/package.json /app/package.json
USER hono
EXPOSE 3000
CMD ["node", "/app/dist/index.js"]

This is pretty much the same setup from the Hono docs.

Let’s also add a .dockerignore file in the root.

.dockerignore
node_modules
.git

To compile our TypeScript file, we’ll need add the following to the tsconfig.json.

tsconfig.json
{
"compilerOptions": {
// ...
"outDir": "./dist"
},
"exclude": ["node_modules"]
}

Install TypeScript.

Terminal window
npm install typescript --save-dev

And add a build script to our package.json.

package.json
"scripts": {
"build": "tsc"
}

Now to build our Docker image and deploy we run:

Terminal window
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 Hono app deployed as a Fargate service.

Terminal window
Complete
MyService: http://prod-MyServiceLoadBalanc-491430065.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.

SST Console Autodeploy

You can create a free account and connect it to your AWS account.