Skip to content

Commit

Permalink
docs: streaming examples closes #1014
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Sep 21, 2024
1 parent e35b953 commit 2a1af97
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 182 deletions.
3 changes: 3 additions & 0 deletions examples/aws-lambda-stream/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# sst
.sst
19 changes: 19 additions & 0 deletions examples/aws-lambda-stream/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { APIGatewayProxyEventV2 } from "aws-lambda";
import { streamifyResponse, ResponseStream } from "lambda-stream";

export const handler = streamifyResponse(myHandler);

async function myHandler(
_event: APIGatewayProxyEventV2,
responseStream: ResponseStream
): Promise<void> {
return new Promise((resolve, _reject) => {
responseStream.setContentType('text/plain')
responseStream.write('Hello')
setTimeout(() => {
responseStream.write(' World')
responseStream.end()
resolve()
}, 3000)
})
}
19 changes: 19 additions & 0 deletions examples/aws-lambda-stream/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "aws-lambda-stream",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"lambda-stream": "^0.5.0",
"sst": "latest"
},
"devDependencies": {
"@types/aws-lambda": "8.10.145"
}
}
14 changes: 14 additions & 0 deletions examples/aws-lambda-stream/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"MyFunction": {
"name": string
"type": "sst.aws.Function"
"url": string
}
}
}
80 changes: 80 additions & 0 deletions examples/aws-lambda-stream/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/// <reference path="./.sst/platform/config.d.ts" />

/**
* ## AWS Lambda streaming
*
* An example on how to enable streaming for Lambda functions.
*
* ```ts title="sst.config.ts"
* {
* streaming: true
* }
* ```
*
* While `sst dev` doesn't support streaming, you can use the
* [`lambda-stream`](https://github.com/astuyve/lambda-stream) package to test locally.
*
* ```bash
* npm install lambda-stream
* ```
*
* Then, you can use the `streamifyResponse` function to wrap your handler:
*
* ```ts title="index.ts"
* import { APIGatewayProxyEventV2 } from "aws-lambda";
* import { streamifyResponse, ResponseStream } from "lambda-stream";
*
* export const handler = streamifyResponse(myHandler);
*
* async function myHandler(
* _event: APIGatewayProxyEventV2,
* responseStream: ResponseStream
* ): Promise<void> {
* return new Promise((resolve, _reject) => {
* responseStream.setContentType('text/plain')
* responseStream.write('Hello')
* setTimeout(() => {
* responseStream.write(' World')
* responseStream.end()
* resolve()
* }, 3000)
* })
* }
* ```
*
* When deployed, this will use the `awslambda.streamifyResponse`.
*
* :::note
* Streaming is currently not supported in `sst dev`.
* :::
*
* To test this in your terminal, use the `curl` command with the `--no-buffer` option.
*
* ```bash "--no-buffer"
* curl --no-buffer https://u3dyblk457ghskwbmzrbylpxoi0ayrbb.lambda-url.us-east-1.on.aws
* ```
*
* Here we are using a Function URL directly because API Gateway doesn't support streaming.
*
*/
export default $config({
app(input) {
return {
name: "aws-lambda-stream",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
};
},
async run() {
const fn = new sst.aws.Function("MyFunction", {
url: true,
streaming: true,
timeout: "15 minutes",
handler: "index.handler",
});

return {
url: fn.url,
};
},
});
1 change: 1 addition & 0 deletions examples/aws-lambda-stream/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading

0 comments on commit 2a1af97

Please sign in to comment.