Skip to main content

· 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/lib/handlers/aws';
import { DummyResolver } from '@h4ad/serverless-adapter/lib/resolvers/dummy';
import { ApiGatewayV2Adapter } from '@h4ad/serverless-adapter/lib/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!

rmation

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.

· 2 min read
Vinícius Lourenço

To the moon!

Now we have more Handlers, Frameworks and Adapters, let's see what's new.

From v2.3.2 to v2.6.0, compare the changes here.

Changes

42 commits, 6905 lines added, 601 lines deleted, that's the size of the changes since The Beginning.

I'm very proud of how things are going, I learned a lot by studying to implement these new things.

But, let's learn what's new in all these releases.

Azure and Firebase

You can now use this library to deploy your apps to Azure Functions and Firebase Functions.

More specifically, you can integrate with Http Trigger V4 on Azure and Http Events on Firebase.

These integrations are just to open the door of possibilities, in the future I want to add support for more triggers in these clouds.

Check out the Azure and Firebase docs for how to integrate.

I also added examples for the cloud in the serverless-adapter-examples repository.

tRPC

tRPC allows you to easily build & consume fully typesafe APIs, without schemas or code generation.

tRPC is a framework that brings a new way of thinking about APIs, instead of REST or GraphQL, you can build typesafe APIs and easily can integrate with the client, seems to be very promising.

So now you can deploy applications developed with tRPC to any cloud that this library supports, have a look at docs to learn more about how to use it.

That's all folks!

I have two more weeks to work in this library without worrying because I'm on vacation at the university, so probably my next efforts will be to bring more articles to this blog to show the full power of this library.

Giving some spoilers for those of you that make it this far, I'll start by showing you the benefits of using AWS Lambda integrated with API Gateway and SQS, I used it in a project of my company and I managed to reduce a lot of stress on the database and now we are able to process 500k votes in minutes without spending 15% CPU using a PostgreSQL database on a t2.micro instance.

That's all for today, thank you!

· 2 min read
Vinícius Lourenço

Hello, welcome to my new library to help you integrate your API with the serverless world.

The development

It took me almost 5 months to build this library, refactoring was easy and testing was challenging, but documenting this library was the hardest part.

It took me almost 2 weeks to refactor @vendia/serverless-express, about 1 and a half month to create tests with 99% coverage and the rest of the time I spent creating documentation for this library.

I currently added support for:

But it's just the beginning, I'm going to build more adapters to integrate with as much of the cloud as possible, just to be able to deploy my APIs on any cloud.

About me

I am a student at Facens university and I work for Liga, which is a sector within Facens that develops applications, websites, games and much more fun stuff.

I currently work on this library only in my spare time and I need to balance my Final Theses and my overtime projects so it was very challenging but I am happy with the end result of this library.

Inspiration

This library was originally created to help my company reduce costs with AWS SQS, but it has since turned into something I can spend my time developing and learning English because I'm not a native speaker (as typing problems might suggest) writing all the documentation in English.

Credits

I need to thank @vendia for developing @vendia/serverless-express, all logic and code I finished to refactor from the code I read on serverless-express. I also have many thanks to Chaguri, Liga and many other people who gave me time and insights to create this library.

You can use it right now!

See the Introduction section to know more about the library.