How to Build a Serverless REST API with Node.js, TypeScript, and AWS DynamoDB
HTML-код
- Опубликовано: 4 июл 2024
- In this video tutorial, you're going to learn how to build a Node.js REST API with DynamoDB serving as data storage, and how to deploy it quickly to AWS using Serverless Framework.
👍 Subscribe for more cloud and programming tutorials like this: / @tomasztarnowski4434
Getting Started with AWS Lambda and Serverless Framework (video):
• Getting started with A...
Getting Started with AWS Lambda and Serverless Framework (article):
blog.tomasztarnowski.com/gett...
AWS DynamoDB Node.js Documentation:
docs.aws.amazon.com/amazondyn...
Link to the code from the video: github.com/ttarnowski/serverl...
Social Media:
Twitter: / t_tarnowski
Linkedin: / tomasz-ta...
GitHub: github.com/ttarnowski
Software used in the video:
Visual Studio Code IDE: code.visualstudio.com/
Prettier Extension for automatic code formatting: marketplace.visualstudio.com/...
Thank you for watching and see you soon!
00:00 Intro
00:49 Creating new Serverless Project
03:13 Configuring auto-formatting with Visual Studio Code
04:10 Infrastructure: Creating DynamoDB as a Resource
07:53 Infrastructure: IAM Role Statements
09:27 Building Create(POST) and Read(GET) Endpoints
21:05 Deploying and Testing basic POST and GET Endpoints
23:55 Implementing Update(PUT) Endpoint
27:55 Removing Code Duplication by Error Handling Introduction
34:28 Delete(DELETE) and List(GET) Endpoints Implementation
37:47 Testing PUT, DELETE and List(GET) Endpoints
41:15 Fixing Content-Type Response Header Issue
44:11 Adding Request Body Validation
52:48 Testing REST API with the Input Validation
56:26 Ideas on what to next
57:15 The End - Наука
Thanks ! You validation part is very valuable ❤
Thank you .. this was a great video and got me started. great content will watch all other videos :)
glad to hear it, emi!
Thank you man, you saved me hours of study
Glad I could help!
@@tomasztarnowski4434 is there a way to scan dynamodb to get the list ordered by LIFO
Hey Solomon, there is a way but you need to either make the field you want to sort by a "Sort Key" or create a secondary index specifying that field again as a "Sort Key", then with the usage of Query method you can utilise ScanIndexForward parameter to get items in order
very simple and well explained (:
thank you, vatsal!
new sub, great video, your wallpaper is 10/10
Thank you, Martin!
Wow, thank you so much. Really love your tutorials. Would be really nice to create an authoriser for our API gateway!
Thank you mate!
Thank you, I'm glad you liked the video. Authentication in serverless will be most likely the next topic. There should be a new video about it soon.
Thank you so much.
Would be nice to have a second part with authentication and authorization, also with some fron end fro users to request acces. Just an idea ! Thanks fro the great video
great tutorial
Thank you, Sasha Louis!
thanks your tutorial
Very useful, thanks. Had to run "npm install" before deploy after adding uuid and then yup dependencies, or it would fail on the server once API is called since after yarn alone apparently packages are not being deployed.
Thanks for the great video.
I got the error: The CloudFormation template is invalid: Template error: instance of Fn::GetAtt references undefined resource ProductsTable.
How can I fix it?
Hey Luu,
Have you got "resources" section defined in serverless.yml with correct indentations like here:
github.com/ttarnowski/serverless-product-api/blob/main/serverless.yml
?
Can we see a similar video which follows a aTDD approach?
I'm a big fan of TDD myself and very often use this approach in my professional work, so yeah definitely there will be videos in the future where I build something with tests.
Can you explain why you use .promise() but don't set that promise to a variable to process fulfilled/rejected states? For example, in createProduct() you have:
await docClient
.put({
TableName: tableName,
Item: product,
})
.promise();
You add it at 15:30, but I don't really understand what the .promise() is doing
hey Matt, very good question ".promise()" method is a part of AWS SDK and it turns entire expression's result (in this case docClient.put(...)) into a javascript Promise object, so you can use either older .then(), .catch() syntax or new async await.
If I hadn't put .promise() after "put" method I wouldn't have been able to await for this operation to complete.
Here's also good explanation in AWS docs with older syntax hopefully it'll help you to grasp it:
docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html
And here how to rewrite .then(), .catch() to async-await:
developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await#rewriting_promise_code_with_asyncawait
@@tomasztarnowski4434 ah, ok! So if that async docClient.put() call fails, would the catch block (that eventually) wraps it catch the error? what would happen if it was never wrapped in a try/catch block? (thanks for your help!!)
if you want to catch that error and have some specific response sent from lambda you can put it in a try-catch block however if you don't in aws lambda environment it is going to fail, log an error in cloudwatch and respond with 500 status code and generic internal server error response body :)
each time I deploy using serverless deploy I get this error after some time loading
Serverless: Uploading service products-api.zip file to S3 (16.88 MB)...
Serverless: Recoverable error occurred (Your socket connection to the server was
not read from or written to within the timeout period. Idle connections will be closed.), sleeping for ~5 seconds. Try 1 of 4
I searched everywhere and no solution
Hey dude. This video was so useful. I have one question though.
Where's the DB located?
When I sign up to my IAM and go to DynaoDB I do not see ProductsTable.
Then where is it located?
Ohh Damn! The table is created in the AWS location specified in the config.
Thanks a ton :)
That is correct! I'm happy you've managed to figure this out yourself!
I can use that syntsx on the events within framework serverless? Mybresources on aws are deployed by terraform and just need yo creare the code for lambda function and tests
And my api is a api websockets
if your resources are deployed separately you need to link them in iamRoleStatements to give lambdas permissions to access them.
If your api is websockets it works differently to the one presented in the video. Although I'll have a new video out soon specifically about API Gateway Websockets. I hope it'll help :)
@@tomasztarnowski4434 I'm waiting for that
Hi bro, I have a company as AWS solutions architect, I am still in training, I dont have knowledge of html/css/js/node.js rest-api dynamo.db, is it necessary for me to learn all these then create a application and test in AWS ?
I got an error Stack [product-api-dev] does not exist
at which point do you get this error?
you're not using Express.js right?
Hey, no, for lambda function you don't need to use express as it's a simple function that takes a request object and is supposed to return a response object. However you can still use express.js syntax if you'd like by using this wrapper library: github.com/vendia/serverless-express.
Don't use aws-sdk, its outdated. Use the modular v3 SDK.
I'll be switching to v3 SDK since it's recommended by AWS now, although this example of an serverless app is still going to work just fine with v2
@@tomasztarnowski4434 now? It's been recommended for nearly 2 years!
I've always been very conservative with using the newest versions until they reach a proper stability.
@@tomasztarnowski4434 same here. V3 has been deemed stable for long enough now though.
Agree.
Amazon SDKs tend to get very ugly and abandoned in future
I wouldn't say it gets abandoned. They have a maintenance policy in place:
docs.aws.amazon.com/sdkref/latest/guide/maint-policy.html
also, there's a communication channel for any deprecation updates