[proposal] Rest API with multiple lambda functions
See original GitHub issueOverview
The current state of the framework deploy the entire project into a single lambda function instance. But for larger projects this can be a problem for several reasons:
- Monitor individual lambda routes integration (number of invocations, errors, etc).
- Controlling individually the lambda functions (permissions, throttling, etc).
- For optimize the lambda function deployment package (.zip).
- If there is an error with one function, the other ones will not be affected.
Register Handler class
The routes will be registered from each handler that will be implemented separately. Below is an example of the hello handler:
Example of usage: app.py
from chalice import Chalice
app = Chalice(app_name="helloworld")
app.registrate_route(
name='hello_world',
path='/helloworld',
handler_class=HelloHandler,
)
hello_world.py
from chalice import LambdaHandler
class HelloWorldHandler(LambdaHandler):
def get(self):
return {"hello": "world"}
To make packaging possible, the project directory must be structured in a specific way. as follows:
<project-dir>
├── helloworld
│ ├── functionlib # function specific modules
│ │ ├── __init__.py
│ │ └── utils.py
│ ├── __init__.py
│ └── helloworld.py
├── chalicelib # project shared modules
│ ├── __init__.py
│ └── generics.py
├── app.py
├── requirements.txt
└── etc ...
Issue Analytics
- State:
- Created 4 years ago
- Reactions:45
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Tutorial: Create a Calc REST API with two AWS service ...
Tutorial: Create a Calc REST API with two AWS service integrations and one Lambda non-proxy integration · Create an AWS account · Create...
Read more >How to Setup a Basic Serverless REST API with AWS Lambda ...
In the search field, input 'lambda', and then select Lambda from the list of services displayed. Click the create function button on the...
Read more >aws api gateway & lambda: multiple endpoint/functions vs ...
I started to head down the path of multiple endpoints pointing to one Lambda function that could treat each endpoint different by accessing...
Read more >5 Create Rest API using AWS Lambda (ServerLess ... - YouTube
In this video, I have explained how to create and deploy Rest APIs using AWS Lambda functions (ServerLess Service) + API Gateway +...
Read more >How to create a Serverless REST API with Python and AWS ...
Python and AWS Lambda – Overview · 1 — Define a REST API · 2 — Set Up the Serverless Infrastructure · 3...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @jamesls, having one lambda function per route might take us from not having enough lambdas to having too many of them.
It’s ok to have multiple methods in a single lambda function and it actually makes sense. But it would be nice to have the ability to organize your api using different lambdas.
Maybe we can use the resource approach, but make it a new input for the route decorator:
Because we have example_resource_1 and example_resource_2 we would end up with 2 lambda functions.
As far as the general idea of breaking out your rest API into multiple lambda functions, I am in favor of supporting this. I think it makes a lot of sense. There’s a few guidelines I’d like to place on proposals being considered:
Just to throw another idea out there, what if there was no specific API for this? What if it was just a config option in
.chalice/config.json
(e.g"single_api_lambda": false
). We would then create lambda functions with each new@app.route
decorator representing a new lambda function. You can already configure which HTTP methods you want routed to it so it gives you the flexibility to group your lambda functions how you want. So for example:The above app would create 4 lambda functions because there’s 4
@app.route
decorators. It also aligns more closely with how the existing@app.*
decorators work, in that each decorator creates a new lambda function.Implementation-wise this is going to be pretty challenging (a lot of stuff assumes
APIHandler
), but I’d rather the complication be in the implementation rather than in the user-facing API.Thoughts?
cc @stealthycoin