Build a RESTful API with the Serverless Framework

Sagar on October 19, 2018

Today, we’re going to implement serverless RESTful API services by using “Serverless Framework”. There are many cloud services provider offers serv... [Read Full]
markdown guide

Oh, and one other thing: Is it possible to use mongoose+MongoDB instead of mysql+MySql? Any downside there?

If you were to use GraphQL, would that be instead of mysql?


Magnus I'm working right now in a project using the serverless framework, you can use mongoose+MongoDB (Cloud Atlas in my case) without any problems, what I like a lot about the serverless framework features is that you can handle a lot of cases in my case I'm working in a public API so you can configure custom authorizers and CORS management / rules. its very flexible framework.


Great, thanks for sharing Erick. I look forward to trying it.


For simplicity I choose MySQL but you can use any NOSQL database. There is single downside that for aws doesn't provide mongodb. If want to use mongodb then you have run ec2 instance for the same.


Check out mlab.com where you can run MongoDB in Serverless fashion. There is a free tier for running a single node MongoDB.

Wow!! It's good. I know that AWS Aurora provides serverless SQL engine. If MongoDB, providing serverless then its too good. Thanks for information.

Lambda + Cloud Atlas is a pretty good stack.

Hey Erick, I have not tried Cloud Atlas before it will be good if you send an email any link or reference for the same.

Hey Sagar, Sure I'll sent you an email.

Erick, I haven't not received email. You can DM me.


In this article, I have used MySQL because of simplicity but you can try any database which you feel comfortable.

You can also try latest serverless database features.

Reference links:

  1. AWS Aurora Serverless
  2. MongoDB Stitch - Serverless

Hi Sagar First of all, thanks for the tutorial. It works perfect for me, but ... When I tried this code for the first time, I used my MySQL RDS variables and everything works fine, but when I use my localhost variables I get this error:

Serverless: GET /user (λ: testFunc)
Debug: internal, implementation, error
TypeError: Uncaught error: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
at Query. (...\kpoapi-base\user.js:20:19)
at bound (domain.js:415:14)
at Query.runBound as _callback
at Query.Sequence.end (...\kpoapi-base\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at ...\kpoapi-base\node_modules\mysql\lib\Pool.js:205:13
at Handshake.onConnect (...\kpoapi-base\node_modules\mysql\lib\Pool.js:58:9)
at bound (domain.js:415:14)
at Handshake.runBound as _callback
at Handshake.Sequence.end (...\kpoapi-base\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Handshake.ErrorPacket (...\kpoapi-base\node_modules\mysql\lib\protocol\sequences\Handshake.js:125:8)
at Protocol._parsePacket (...\kpoapi-base\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (...\kpoapi-base\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (...\kpoapi-base\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (...\kpoapi-base\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket. (...\kpoapi-base\node_modules\mysql\lib\Connection.js:91:28) at Socket. (...\kpoapi-base\node_modules\mysql\lib\Connection.js:522:10)
at Socket.emit (events.js:196:13)
at Socket.EventEmitter.emit (domain.js:494:23)
at addChunk (_stream_readable.js:290:12)
at readableAddChunk (_stream_readable.js:271:11)
at Socket.Readable.push (_stream_readable.js:226:10)
at TCP.onStreamRead (internal/stream_base_commons.js:166:17)

Obviously, I tried the connection using Workbench and I have no problem.
What could be happening?
I would appreciate any help from you. I hope you can help me.


Hi Carlos,thanks for your feedback. As per error stack, your trying to iterate over undefined.


Hi Sagar, thanks for your response.
The problem, finally, was the type of authentication configured in my localhost of MySQL. I changed it to "Use Legacy Authentication Method" and the problem was solved. Thank you very much, again for your contributions.


Hey, Thank you very much for the post!
I had a quick question - Considering lambdas are short lived (3 or 5 mins if I am not wrong) - do we benefit from keeping a ConnectionPool?

Once the lambda lifecycle ends, would we not need to create this connection pool again?

If we keep creating this connection pool, isn't the purpose of pooling defeated?



Hi Anirudh, if we create a connection pool for each lambda then we're not reusing pool code. For code reusability, I have defined dbConfig.js file.


Thanks, Sagar. Quick question. Isn't express used to create a web service so to say (I.e. the functionality of the server). Why do we need both Serverless and express? Probably a pretty basic question, it just confused me a bit.


Hey Magnus, good question.

We can create api in serverless without using express. But express is so powerful and creating api with express is so much simpler.

Lots of node developers are expert in express.


Hey, thank you for your article !
I'm also a bit confused with express. Does the express server starts everytime we receive a call ? Or is there something done with module.exports.handler = serverless(app) that does something that I don't understand yet ?


I noticed you are using string interpolating for your values in the SQL query, doesn’t this make you vulnerable to an injection attack?


Hi Trysm, you're absolutely right. To explains developer in the simplest way, I haven't included any best practices or validation. If it's production application then I will definitely use string interpolation and so many other stuff. Thanks for you're a suggestion.


Hey Sagar. Thanks in advance for the shared knowledge. I have a concern about it, you built a complete API in just one lambda function, I’m not sure if it’s a good practice but AWS recommends separating and dividing responsibilities inside a lambda funcion code. What do you think?


Hi Sagar, its an excellent sample. I just had a question, have you tried serving images while using serverless-http package. I am able to respond the images but i dont see the images being rendered. I thing there is some confusion with binary data. If possible can u share a sample with the image respose also.



Sagar! Hi! Its a wonderfull tutorial! Do you test it on AWS? Only need to change the RDS MySQL address, user & Pass to deploy? Thanks!


Can we implement this using Aurora DB Serverless?


In PUT route, Why you first write a SELECT query to get id?

code of conduct - report abuse