Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AWS Lambda docs with streaming support #3354

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/tests/aws-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const awsLambdaDeployment: DeploymentConfiguration<{
'func',
{
role: lambdaRole.arn,
runtime: 'nodejs18.x',
runtime: 'nodejs20.x',
handler: 'index.handler',
code: new pulumi.asset.AssetArchive({
'index.js': new pulumi.asset.FileAsset(
Expand Down
1 change: 1 addition & 0 deletions examples/aws-lambda/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
!scripts/*.js
!jest.config.js
*.d.ts
!awslambda.d.ts
node_modules

# CDK asset staging directory
Expand Down
28 changes: 28 additions & 0 deletions examples/aws-lambda/lambda/awslambda.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Writable } from 'node:stream';
import { APIGatewayEvent, Context, Handler } from 'aws-lambda';

declare global {
namespace awslambda {
export namespace HttpResponseStream {
function from(
responseStream: ResponseStream,
metadata: {
statusCode?: number;
headers?: Record<string, string>;
},
): ResponseStream;
}

export type ResponseStream = Writable & {
setContentType(type: string): void;
};

export type StreamifyHandler = (
event: APIGatewayEvent,
responseStream: ResponseStream,
context: Context,
) => Promise<unknown>;

export function streamifyResponse(handler: StreamifyHandler): Handler<APIGatewayEvent>;
}
}
71 changes: 42 additions & 29 deletions examples/aws-lambda/lambda/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIGatewayEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
import { pipeline } from 'stream/promises';
import { APIGatewayEvent, Context } from 'aws-lambda';
import { createSchema, createYoga } from 'graphql-yoga';

const yoga = createYoga<{
Expand All @@ -21,33 +22,45 @@ const yoga = createYoga<{
}),
});

export async function handler(
event: APIGatewayEvent,
lambdaContext: Context,
): Promise<APIGatewayProxyResult> {
const response = await yoga.fetch(
event.path +
'?' +
new URLSearchParams((event.queryStringParameters as Record<string, string>) || {}).toString(),
{
method: event.httpMethod,
headers: event.headers as HeadersInit,
body: event.body
? Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
: undefined,
},
{
event,
lambdaContext,
},
);
export const handler = awslambda.streamifyResponse(
async function handler(event, res, lambdaContext) {
const response = await yoga.fetch(
// Construct the URL
event.path +
'?' +
// Parse query string parameters
new URLSearchParams(
(event.queryStringParameters as Record<string, string>) || {},
).toString(),
{
method: event.httpMethod,
headers: event.headers as HeadersInit,
// Parse the body
body: event.body
? Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
: undefined,
},
{
event,
lambdaContext,
res,
},
);

// Create the metadata object for the response
const metadata = {
statusCode: response.status,
headers: Object.fromEntries(response.headers.entries()),
};

const responseHeaders = Object.fromEntries(response.headers.entries());
// Attach the metadata to the response stream
const resWithMetadata = awslambda.HttpResponseStream.from(res, metadata);

return {
statusCode: response.status,
headers: responseHeaders,
body: await response.text(),
isBase64Encoded: false,
};
}
// Pipe the response body to the response stream
if (response.body) {
await pipeline(response.body, resWithMetadata);
} else {
resWithMetadata.end();
}
},
);
2 changes: 1 addition & 1 deletion examples/aws-lambda/lib/graphql-lambda-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class GraphqlLambdaStack extends cdk.Stack {
const graphqlLambda = new lambda.Function(this, 'graphqlLambda', {
code: lambda.Code.fromAsset(path.join(__dirname, '../lambda')),
handler: 'graphql.handler',
runtime: lambda.Runtime.NODEJS_14_X,
runtime: lambda.Runtime.NODEJS_20_X,
});

new apiGateway.LambdaRestApi(this, 'graphqlEndpoint', {
Expand Down
5 changes: 1 addition & 4 deletions examples/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
"source-map-support": "^0.5.16"
},
"devDependencies": {
"@aws-cdk/assert": "2.68.0",
"@types/aws-lambda": "8.10.116",
"@types/aws-lambda": "8.10.140",
"@types/node": "18.16.16",
"aws-cdk": "2.83.0",
"aws-cdk-lib": "2.83.0",
"esbuild": "0.17.19",
"ts-node": "10.9.1",
"typescript": "5.5.3"
}
}
Loading
Loading