Skip to content

Commit 044fa75

Browse files
committed
docs: hono quick start
1 parent bc6d898 commit 044fa75

File tree

10 files changed

+169
-112
lines changed

10 files changed

+169
-112
lines changed

examples/aws-hono-container/src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { serve } from '@hono/node-server'
22
import { Hono } from 'hono'
3-
import { Resource } from "sst";
3+
import { Resource } from 'sst'
44
import {
55
S3Client,
66
GetObjectCommand,
77
ListObjectsV2Command,
8-
} from "@aws-sdk/client-s3";
9-
import { Upload } from "@aws-sdk/lib-storage";
10-
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
8+
} from '@aws-sdk/client-s3'
9+
import { Upload } from '@aws-sdk/lib-storage'
10+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
1111

1212
const s3 = new S3Client();
1313

examples/aws-hono/.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
# prod
2+
dist/
3+
lambda.zip
4+
5+
# dev
6+
.yarn/
7+
!.yarn/releases
8+
.vscode/*
9+
!.vscode/launch.json
10+
!.vscode/*.code-snippets
11+
.idea/workspace.xml
12+
.idea/usage.statistics.xml
13+
.idea/shelf
14+
15+
# deps
16+
node_modules/
17+
18+
# env
19+
.env
20+
.env.production
21+
22+
# logs
23+
logs/
24+
*.log
25+
npm-debug.log*
26+
yarn-debug.log*
27+
yarn-error.log*
28+
pnpm-debug.log*
29+
lerna-debug.log*
30+
31+
# misc
32+
.DS_Store
133

234
# sst
335
.sst

examples/aws-hono/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```
2+
npm install
3+
npm run deploy
4+
```

examples/aws-hono/index.ts

-42
This file was deleted.

examples/aws-hono/package.json

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
{
22
"name": "aws-hono",
3-
"version": "1.0.0",
4-
"description": "",
5-
"main": "index.js",
3+
"type": "module",
64
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
5+
"build": "esbuild --bundle --outfile=./dist/index.js --platform=node --target=node20 ./src/index.ts",
6+
"deploy": "run-s build zip update",
7+
"update": "aws lambda update-function-code --zip-file fileb://lambda.zip --function-name hello",
8+
"zip": "zip -j lambda.zip dist/index.js"
9+
},
10+
"devDependencies": {
11+
"@types/aws-lambda": "8.10.146",
12+
"esbuild": "^0.21.4",
13+
"npm-run-all2": "^6.2.0"
814
},
9-
"keywords": [],
10-
"author": "",
11-
"license": "ISC",
1215
"dependencies": {
13-
"@aws-sdk/client-s3": "^3.552.0",
14-
"@aws-sdk/s3-request-presigner": "^3.552.0",
15-
"esbuild-plugin-tsc": "^0.4.0",
16-
"hono": "^4.1.3",
17-
"sst": "3.3.x"
16+
"@aws-sdk/client-s3": "^3.701.0",
17+
"@aws-sdk/s3-request-presigner": "^3.701.0",
18+
"hono": "^4.6.12",
19+
"sst": "3.3.35"
1820
}
1921
}

examples/aws-hono/src/index.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Hono } from 'hono'
2+
import { handle } from 'hono/aws-lambda'
3+
import { Resource } from 'sst'
4+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
5+
import {
6+
S3Client,
7+
GetObjectCommand,
8+
PutObjectCommand,
9+
ListObjectsV2Command,
10+
} from '@aws-sdk/client-s3'
11+
12+
const s3 = new S3Client();
13+
14+
const app = new Hono()
15+
16+
app.get('/', async (c) => {
17+
const command = new PutObjectCommand({
18+
Key: crypto.randomUUID(),
19+
Bucket: Resource.MyBucket.name,
20+
});
21+
22+
return c.text(await getSignedUrl(s3, command));
23+
});
24+
25+
app.get('/latest', async (c) => {
26+
const objects = await s3.send(
27+
new ListObjectsV2Command({
28+
Bucket: Resource.MyBucket.name,
29+
}),
30+
);
31+
32+
const latestFile = objects.Contents!.sort(
33+
(a, b) =>
34+
(b.LastModified?.getTime() ?? 0) - (a.LastModified?.getTime() ?? 0),
35+
)[0];
36+
37+
const command = new GetObjectCommand({
38+
Key: latestFile.Key,
39+
Bucket: Resource.MyBucket.name,
40+
});
41+
42+
return c.redirect(await getSignedUrl(s3, command));
43+
});
44+
45+
export const handler = handle(app)

examples/aws-hono/sst-env.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ import "sst"
66
export {}
77
declare module "sst" {
88
export interface Resource {
9+
"Hono": {
10+
"name": string
11+
"type": "sst.aws.Function"
12+
"url": string
13+
}
14+
"MyBucket": {
15+
"name": string
16+
"type": "sst.aws.Bucket"
17+
}
918
}
1019
}

examples/aws-hono/sst.config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ export default $config({
44
app(input) {
55
return {
66
name: "aws-hono",
7-
home: "aws",
87
removal: input?.stage === "production" ? "retain" : "remove",
8+
home: "aws",
99
};
1010
},
1111
async run() {
1212
const bucket = new sst.aws.Bucket("MyBucket");
13-
const hono = new sst.aws.Function("Hono", {
13+
new sst.aws.Function("Hono", {
1414
url: true,
1515
link: [bucket],
16-
handler: "index.handler",
16+
handler: "src/index.handler",
1717
});
18-
},
18+
}
1919
});

examples/aws-hono/tsconfig.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
{}
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"moduleResolution": "Bundler",
6+
"strict": true,
7+
"skipLibCheck": true,
8+
"types": [
9+
"node"
10+
],
11+
"jsx": "react-jsx",
12+
"jsxImportSource": "hono/jsx",
13+
}
14+
}

www/src/content/docs/docs/start/aws/hono.mdx

+43-49
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ Before you get started, make sure to [configure your AWS credentials](/docs/iam-
3838
Let's start by creating our app.
3939

4040
```bash
41-
mkdir aws-hono && cd aws-hono
42-
npm init -y
41+
npm create hono@latest aws-hono
42+
cd aws-hono
4343
```
4444

45+
We are picking the **aws-lambda** template.
46+
4547
##### Init SST
4648

4749
Now let's initialize SST in our app.
@@ -61,7 +63,7 @@ Let's add a Hono API using an AWS Lambda. Update your `sst.config.ts`.
6163

6264
```js title="sst.config.ts"
6365
async run() {
64-
const hono = new sst.aws.Function("Hono", {
66+
new sst.aws.Function("Hono", {
6567
url: true,
6668
handler: "index.handler",
6769
});
@@ -102,7 +104,7 @@ const bucket = new sst.aws.Bucket("MyBucket");
102104
Now, link the bucket to the API.
103105

104106
```ts title="sst.config.ts" {3}
105-
const hono = new sst.aws.Function("Hono", {
107+
new sst.aws.Function("Hono", {
106108
url: true,
107109
link: [bucket],
108110
handler: "index.handler",
@@ -113,20 +115,17 @@ const hono = new sst.aws.Function("Hono", {
113115

114116
### 4. Upload a file
115117

116-
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.
117-
118-
```ts title="index.ts" {5}
119-
const app = new Hono()
120-
.get("/", async (c) => {
121-
const command = new PutObjectCommand({
122-
Key: crypto.randomUUID(),
123-
Bucket: Resource.MyBucket.name,
124-
});
118+
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`.
125119

126-
return c.text(await getSignedUrl(s3, command));
120+
```ts title="src/index.ts" {4}
121+
app.get('/', async (c) => {
122+
const command = new PutObjectCommand({
123+
Key: crypto.randomUUID(),
124+
Bucket: Resource.MyBucket.name,
127125
});
128126

129-
export const handler = handle(app);
127+
return c.text(await getSignedUrl(s3, command));
128+
});
130129
```
131130

132131
:::tip
@@ -136,55 +135,50 @@ We are directly accessing our S3 bucket with `Resource.MyBucket.name`.
136135
Install the npm packages.
137136

138137
```bash
139-
npm install hono @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
138+
npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
140139
```
141140

142-
Then add the relevant imports.
141+
Then add the relevant imports. We'll use the extra ones below.
143142

144-
```ts title="index.ts"
145-
import { Resource } from "sst";
146-
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
143+
```ts title="src/index.ts"
144+
import { Resource } from 'sst'
145+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
147146
import {
148147
S3Client,
149148
GetObjectCommand,
150149
PutObjectCommand,
151150
ListObjectsV2Command,
152-
} from "@aws-sdk/client-s3";
153-
import { Hono } from "hono";
154-
import { handle } from "hono/aws-lambda";
151+
} from '@aws-sdk/client-s3'
155152

156-
const s3 = new S3Client({});
153+
const s3 = new S3Client();
157154
```
158155

159-
160156
---
161157

162158
### 5. Download a file
163159

164-
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`.
160+
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`.
165161

166-
```ts title="index.ts"
167-
const app = new Hono()
168-
// ...
169-
.get("/latest", async (c) => {
170-
const objects = await s3.send(
171-
new ListObjectsV2Command({
172-
Bucket: Resource.MyBucket.name,
173-
}),
174-
);
175-
176-
const latestFile = objects.Contents!.sort(
177-
(a, b) =>
178-
(b.LastModified?.getTime() ?? 0) - (a.LastModified?.getTime() ?? 0),
179-
)[0];
180-
181-
const command = new GetObjectCommand({
182-
Key: latestFile.Key,
162+
```ts title="src/index.ts"
163+
app.get('/latest', async (c) => {
164+
const objects = await s3.send(
165+
new ListObjectsV2Command({
183166
Bucket: Resource.MyBucket.name,
184-
});
167+
}),
168+
);
169+
170+
const latestFile = objects.Contents!.sort(
171+
(a, b) =>
172+
(b.LastModified?.getTime() ?? 0) - (a.LastModified?.getTime() ?? 0),
173+
)[0];
185174

186-
return c.redirect(await getSignedUrl(s3, command));
175+
const command = new GetObjectCommand({
176+
Key: latestFile.Key,
177+
Bucket: Resource.MyBucket.name,
187178
});
179+
180+
return c.redirect(await getSignedUrl(s3, command));
181+
});
188182
```
189183

190184
---
@@ -345,14 +339,14 @@ app.post('/', async (c) => {
345339
Add the imports. We'll use the extra ones below.
346340

347341
```tsx title="src/index.ts"
348-
import { Resource } from "sst";
342+
import { Resource } from 'sst'
349343
import {
350344
S3Client,
351345
GetObjectCommand,
352346
ListObjectsV2Command,
353-
} from "@aws-sdk/client-s3";
354-
import { Upload } from "@aws-sdk/lib-storage";
355-
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
347+
} from '@aws-sdk/client-s3'
348+
import { Upload } from '@aws-sdk/lib-storage'
349+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
356350

357351
const s3 = new S3Client();
358352
```

0 commit comments

Comments
 (0)