0

I'm confused regarding how the lambda functions work with the API Gateway. There are many frameworks to use, the one I keep hearing about is serverless and serverless-express. Is serverless like Express but for Lambda functions with API Gateway?

For example, what if I wanted to define multiple routes in my lambda like I would do in an Express app?

Can someone explain me what is the best way to create routes in node.js for api gateway invocations, perhaps this is best done with a certain framework or is it plain like that in the lambda?

1 Answer 1

3

Let me try to clarify your confusion, I can understand it 😉 However, take care that there are multiple tools/projects called serverless-express or similar and they have different meanings (see the end of my answer). I'm assuming below that you want to use Express within a Lambda function.

Explaining the different terms/tools

Serverless is a tool/framework to ease the infrastructure deployment process for services like AWS Lambda (but there are other Function-as-a-Service providers or tools supported). This means the Serverless framework can help you writing functions as Infrastructure as Code and hence, you can easily connect Lambda functions with other services like API Gateway, SQS, SNS, and more.

API Gateway is a service to define endpoints or routes and forward an HTTP request to another destination. This can be a Lambda function but also a completely different HTTP endpoint.

The main purpose of Express is to manage routes and some middlewares, so you can implement your business logic for specific endpoints. Express starts an HTTP server and then forwards the requests to your business logic.

Understanding the differences

You could say to some extent that in general an Express app is like a combination of API Gateway and AWS Lambda because it knows how to route requests (= what API Gateway is doing) to your business logic (= what AWS Lambda functions are doing). The only difference is that a typical Express app runs on its own server whereas API Gateway and AWS Lambda are separated services that need to be connected together. This is what you can do with Infrastructure as Code. (You can also do it manually but it's not recommended for larger projects) Then, you "only" need to implement your business logic inside your Lambda functions.

How Express apps run on AWS Lambda

However, it's possible to run an Express app on AWS Lambda since you can run almost any kind of Node.js program on AWS Lambda. In such a case you still need to combine API Gateway and AWS Lambda in order to receive HTTP events in your Lambda function. This is necessary because AWS Lambda alone can not receive any HTTP request, it can just receive 'events' of any structure. Instead of directly receiving HTTP requests in Lambda, the API Gateway is forwarding all HTTP requests to your Lambda function (= you can configure to forward all requests, independent of the url path or HTTP method) and converts them to an HTTP event that can be handled by your Lambda function. Then, the Express app in your Lambda function uses this HTTP event so it knows where to route the request/event within your code. In the end it means you just use AWS Lambda as a way to run your Express app, instead of using a separate server.

Tools to achieve it

As mentioned in the beginning, there are several projects or tools that call themselves serverless-express or similar. For example, there is https://github.com/vendia/serverless-express which contains some glue code to connect an HTTP event in your Lambda function with the Express app code. But this project is not using the Serverless framework. Instead, it's using the Serverless Application Model (SAM) which is a CLI tool by AWS directly. However, there's also Serverless Express that tries to achieve the same thing but in a slightly different way using the Serverless framework.

Reasons why you should reconsider your approach running Express on AWS Lambda

Although it might sound like a nice idea to run Express apps on AWS Lambda, I can definitely not recommend it. You duplicate most of the routing stuff as you might have recognized by my explanations above. If you want to know more reasons why you shouldn't do it, I can recommend reading this blog post which goes into more details.

Sign up to request clarification or add additional context in comments.

11 Comments

@Arturo You're welcome! The Serverless framework won't be included inside an AWS Lambda function. It just helps you deploying a Lambda function to AWS. A Lambda function is just any kind of Node.js file that exports a handler function and implements a (event, context, callback) function (or async (event, context) => {} in more modern ways). You don't need to bundle the Serverless framework within Lambda (and you should not do this).
With bundle I meant that you don't need to include the Serverless framework as a dependency in the Lambda function artifact that you upload to AWS Lambda. You can just zip your Lambda function code together with node_modules folder and upload all of that to AWS Lambda (either manually in the AWS Console or using some script). But the node_modules folder does not need to include the Serverless dependency. (At least that's what I thought is confusing you as well)
However, take care that the Serverless framework is doing exactly that for you: bundling your code and uploading it to AWS Lambda - you don't have to do it manually.
And in case you want to use existing HTTP APIs or DynamoDB tables, you can just reference them in your Serverless template file using their ARNs. (I don't see a good reason why you'd want to do this - rather make sure that you create all resources like HTTP APIs or tables within your template and then deploy the whole template)
Yeah I agree, there are so many different tools and services and it takes a while to understand everything ;) Yes, this is recommended to write everything in templates using Infrastructure as Code. However, there might definitely be reasons why you want to reference other resources using e.g. their ARN and don't include them in your template. But for your current use case I don't see why it's necessary ;) So, if my answer helped you, feel free to accept it :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.