Skip to main content

One post tagged with "aws"

View All Tags

· 3 min read
Vinícius Lourenço

A beautiful stream!

Image by Hendrik Cornelissen on Unsplash

It's been a long time since I wrote a post here, but I'm happy to share this new announcement.

First, are you new to this library?

First time?

Let me introduce the library first, I named Serverless Adapter because my goal is connect any serverless environment to any NodeJS framework.

So you could just plug your framework, use the correct handler for your serverless environment, choose the adapters and then you can deploy your application!

What does this library support?

Currently, we support 8 NodeJS frameworks: Express, Fastify, tRPC, Apollo Server, NestJS, Deepkit, Koa and Hapi.

We also support 6 serverless environments: AWS, Azure, Google Cloud, Digital Ocean, Firebase and Huawei.

Talking about AWS, we support 10 different services like API Gateway V1 and V2, SQS, SNS, etc... and you can combine them to use the same codebase and lambda to handle them all.

tip

To learn understand the power of this composability, check this article I wrote about how I went From a million invocations to a thousand with correct caching.

But okay, enough self-marketing, let's get to the main point of this article.

AWS Lambda Response Streaming

Today I'm rolling out support for AWS Lambda Streaming Response using AwsStreamHandler.

If you already use this library, just change DefaultHandler to AwsStreamHandler, and make sure you're using DummyResolver and ApiGatewayV2Adapter:

index.ts
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { AwsStreamHandler } from '@h4ad/serverless-adapter/handlers/aws';
import { DummyResolver } from '@h4ad/serverless-adapter/resolvers/dummy';
import { ApiGatewayV2Adapter } from '@h4ad/serverless-adapter/adapters/aws';
import app from './app';

export const handler = ServerlessAdapter.new(app)
// .setHandler(new DefaultHandler())
.setHandler(new AwsStreamHandler())
.setResolver(new DummyResolver())
.setAdapter(new ApiGatewayV2Adapter())
// more options...
//.setFramework(new ExpressFramework())
.build();

Despite its name, ApiGatewayV2Adapter can be used to support API Gateway V2 and function URLs.

caution

Response streaming currently is only available for Function URLs.

That's it :) Now you can use Function URLs and stream your content to the world!

Don't forget to enable the feature in your AWS Lambda function by changing BUFFERED TO RESPONSE_STREAM.

I NEED CODE!!!

Well, if you're the type of person who, like me, needs to see the code working, here's a repository with several example projects using this library: serverless-adapter-examples.

Beyond HTTP Requests

Furthermore, not only can you receive HTTP requests using Function URLs, but you can combine your SQS queue and use the same codebase to process everything.

I haven't spent a lot of time testing it, but so far, any AWS service that supports this library can be hooked up to your Lambda function with RESPONSE_STREAM enabled.

The only thing you need to know is: the answer didn't work as expected, I couldn't get the SQS Partial Response to work for example .

But you can give it a try anyway, share your results with me on twitter and I'll be happy to help if I can.

Conclusion

Well, I don't have much to say, but I hope you enjoy this new feature and use it to build amazing things.

I've spent the last 3 weeks trying to figure out how to make this work and I'm happy with the result.

If you're curious enough to learn more about how I implement it, you can see this PR with all my struggles and thoughts over the weeks.