15

Can I write one Lambda Function to Handle Multiple REST API Requests. I have my data in Dynamo DB

Flow: API Gateway-->Lambda Function-->Dynamo DB Example:

Request1:GET Method-Need to pull data from Table1
/device/{device_id}/start/{start_date}/end/{end_date}/events


Request2:GET Method-Need to pull data from Table2
/device/{device_id}/start/{start_date}/end/{end_date}/event_count

Request3:POST Method-Need to put data from Table3
/device/{device_id}/start/{start_date}/end/{end_date}/fault_events

What is the best solution should I write 3 different lambda functions to handle 3 different requests or can I handle all the 3 requests in one BIG Lambda Function.

1

5 Answers 5

16

Yes, you can have one Lambda function that handle more than one API. The question is why?

Doing this is considered (almost) an anti-pattern as you won't be able to scale independently the various scenario.

These are two slides from the link pasted above from Chris Munns talk at last re:invent and I strongly agree.

enter image description here enter image description here

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

2 Comments

Just to add to this, there are pros/cons to each approach. The slide mentions cons for putting everything in one lambda, there are pros as well I can think of: decreased maintenance overhead - if you are using cloud formation each lambda adds to build / deploy times and you have to deal with a larger template (you could argue this makes the code less readable though so potentially a con). Another pro is that less functions will decrease number of cold starts since the same container will be reused. In general tho I think the cons outweigh pros, but it very much depends on YOUR use case.
As the previous person commented, a single lambda can be pretty good for cold start, as only one lambda needs to be initialized, and all the other responses come from the same lambda. This is especially true for small services with not too many requests. There is no magic bullet when it comes to system design.
6

While I don't know enough about your use case to recommend using 1 or multiple Lambda, I can explain one way of working with all queries inside one function.

You can pass in parameters from the lambda event, which come from the AWS API parameters and then use these to determine following logic. An example would look like this -

def lambda_handler(event, context):
    try:
        type_query = str(event['queryStringParameters']['type_query'])
        if type_query == 'x':
            ...do the things
        elif type_query == 'y':
            ...do the other things
        elif type_query == 'z':
            ...do the 3rd thing
    except:
        return {
            'body': "Invalid Params"
        }

Hope this helps!

Comments

4

You have to find the right balance based on what your application/service does. In general I like to break things down into pretty small pieces, but you need to be careful you aren't going too far, and creating nano-services, which many consider an anti-pattern due to the maintenance overhead you end up creating.

Comments

1

If your motivation for wanting to group different APIs into the same lambda is complexity of the codebase, one option is to have a single codebase but with multiple endpoints defined for different lambda functions.

Comments

0

As many pointed out, it really depends by your use case. In general it should be fine to handle multiple endpoints in one Lambda function. One approach you can use, if you are using Node.JS, is wrapping an express web server inside a Lambda function. That way you will be able to both test the function locally (by running the web server) and use it in lambda (by wrapping the server inside the Lambda proxy). But again, it really depends on your use case, and as Jason mentioned you should also be careful to not split them too much

Comments

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.