Hono on AWS with SST
Create and deploy a Hono API in AWS with SST.
We are going to build a serverless Hono API, add an S3 Bucket for file uploads, and deploy it to AWS using SST.
Before you get started, make sure to configure your AWS credentials.
1. Create a project
Let’s start by creating our app.
mkdir my-hono-app && cd my-hono-appnpm init -y
Init SST
Now let’s initialize SST in our app. Make sure to add the @ion
part.
npx sst@ion initnpm 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
.
async run() { const hono = new sst.aws.Function("Hono", { url: true, handler: "index.handler", });
return { api: hono.url, };}
We are enabling the function URL for this.
Start dev mode
Start your app in dev mode. This runs your functions Live.
npx sst dev
This will give you the URL of your API.
+ Complete api: https://gyrork2ll35rsuml2yr4lifuqu0tsjft.lambda-url.us-east-1.on.aws
3. Add an S3 Bucket
Let’s add a public
S3 Bucket for file uploads. Update your sst.config.ts
.
const bucket = new sst.aws.Bucket("MyBucket", { public: true});
Link the bucket
Now, link the bucket to the API.
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.
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.
npm install hono @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
Then add the relevant imports.
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
.
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.
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.
npx sst deploy --stage production
You can use any stage name here but it’s good to create a new stage for production.
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.