Today, we’re going to implement serverless RESTful API services by using “Serverless Framework”. There are many cloud services provider offers serv...
For further actions, you may consider blocking this person and/or reporting abuse
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.
Hey you can delete this one now.
Hi, Sagar, i was trying to implement Nodejs App with Mysql to build it serverless following your article, i get this error.
Serverless: Configuration warning at 'provider.runtime': should be equal to one of the allowed values [dotnetcore2.1, dotnetcore3.1, go1.x, java11, java8, java8.al2, nodejs10.x, nodejs12.x, provided, provided.al2, python2.7, python3.6, python3.7, python3.8, ruby2.5, ruby2.7]
i use below versions
Angular:- 10.1.2
Node:- 12.18.0
Mysql:- 8.0.17
please help me with the above error, i have followed same steps as mentioned in your article. (excepts for db config password)
basically, you need to provide runtime in the serverless.yml file.
Hi Sagar,
Thank you for sharing, upon changing serverless.yml runtime to nodejs12.x, i am not getting the error i mentioned in previous comment.
I am attaching screenshots of calling API in postman i dont get response as shown in your article, and get pokemon by id API doesnt work at all.
Please let me knoew how to get data of API correctly.
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...
Also, I tried to debug by putting console statements in index.js file API's but console statements are not printed in BE.
app.get('/pokemon/', (req, res) => {
const query = 'SELECT * FROM pokemon_tb'
pool.query(query, (err, results, fields) => {
if (err) {
const response = { data: null, message: err.message, }
console.log('***********', response);
res.send(response)
}
})
please tell me why ?
IDK exact reason behind this. you need crosscheck function with the article mention code blocks and also you've to debug the root cause of this.
Hi, i had crossed checked function code blocks with article code blocks, its the same.
Can please tell me why console statements arent printing, it should print right ?
Great walkthrough! I've covered this topic in more detail with several different databases and scenarios. Have a look if you're interested.
I'd love some feedback!
Thank you 🙏🙇
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.
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:
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?
Thanks!
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 ?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.
Thanks!
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 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.
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!
Does this require our own AWS account?
Yes 😃☺
Can we implement this using Aurora DB Serverless?