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
  • НаукаНаука

Комментарии • 47

  • @nipunchathuranga3807
    @nipunchathuranga3807 4 месяца назад

    Thanks ! You validation part is very valuable ❤

  • @emiremark2889
    @emiremark2889 Год назад +1

    Thank you .. this was a great video and got me started. great content will watch all other videos :)

  • @solomontemitayo3772
    @solomontemitayo3772 2 года назад +3

    Thank you man, you saved me hours of study

    • @tomasztarnowski4434
      @tomasztarnowski4434  2 года назад +2

      Glad I could help!

    • @solomontemitayo3772
      @solomontemitayo3772 2 года назад +1

      @@tomasztarnowski4434 is there a way to scan dynamodb to get the list ordered by LIFO

    • @tomasztarnowski4434
      @tomasztarnowski4434  Год назад +1

      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

  • @vatsalpatel3669
    @vatsalpatel3669 Год назад +1

    very simple and well explained (:

  • @martindanielperezpuchuri3112
    @martindanielperezpuchuri3112 2 года назад +2

    new sub, great video, your wallpaper is 10/10

  • @ol1175
    @ol1175 Год назад +1

    Wow, thank you so much. Really love your tutorials. Would be really nice to create an authoriser for our API gateway!
    Thank you mate!

    • @tomasztarnowski4434
      @tomasztarnowski4434  Год назад +1

      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.

  • @rakeshrockb
    @rakeshrockb 9 месяцев назад

    Thank you so much.

  • @jordivallverdu6200
    @jordivallverdu6200 Год назад +1

    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

  • @louissasha
    @louissasha Год назад +1

    great tutorial

  • @khoahoc8879
    @khoahoc8879 10 месяцев назад

    thanks your tutorial

  • @nftsasha
    @nftsasha 4 месяца назад

    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.

  • @luuquanghung
    @luuquanghung 2 года назад +1

    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?

    • @tomasztarnowski4434
      @tomasztarnowski4434  2 года назад

      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
      ?

  • @nwigweuzochukwu6089
    @nwigweuzochukwu6089 2 года назад +1

    Can we see a similar video which follows a aTDD approach?

    • @tomasztarnowski4434
      @tomasztarnowski4434  2 года назад

      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.

  • @mattriese8143
    @mattriese8143 2 года назад +2

    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

    • @tomasztarnowski4434
      @tomasztarnowski4434  2 года назад +3

      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

    • @mattriese8143
      @mattriese8143 2 года назад

      @@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!!)

    • @tomasztarnowski4434
      @tomasztarnowski4434  2 года назад +1

      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 :)

  • @spacetium1312
    @spacetium1312 Год назад

    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

  • @sciency_stuff
    @sciency_stuff Год назад +1

    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?

    • @sciency_stuff
      @sciency_stuff Год назад +1

      Ohh Damn! The table is created in the AWS location specified in the config.
      Thanks a ton :)

    • @tomasztarnowski4434
      @tomasztarnowski4434  Год назад +1

      That is correct! I'm happy you've managed to figure this out yourself!

  • @eR1cK92
    @eR1cK92 2 года назад +1

    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

    • @tomasztarnowski4434
      @tomasztarnowski4434  2 года назад

      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 :)

    • @eR1cK92
      @eR1cK92 2 года назад +1

      @@tomasztarnowski4434 I'm waiting for that

  • @M.ojo.M
    @M.ojo.M 5 месяцев назад

    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 ?

  • @tosinolugbenga2904
    @tosinolugbenga2904 Год назад +1

    I got an error Stack [product-api-dev] does not exist

  • @TCErnesto
    @TCErnesto Год назад +1

    you're not using Express.js right?

    • @tomasztarnowski4434
      @tomasztarnowski4434  Год назад +1

      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.

  • @lordpablo1985
    @lordpablo1985 Год назад +1

    Don't use aws-sdk, its outdated. Use the modular v3 SDK.

    • @tomasztarnowski4434
      @tomasztarnowski4434  Год назад

      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

    • @lordpablo1985
      @lordpablo1985 Год назад

      @@tomasztarnowski4434 now? It's been recommended for nearly 2 years!

    • @tomasztarnowski4434
      @tomasztarnowski4434  Год назад +1

      I've always been very conservative with using the newest versions until they reach a proper stability.

    • @lordpablo1985
      @lordpablo1985 Год назад +1

      @@tomasztarnowski4434 same here. V3 has been deemed stable for long enough now though.

    • @tomasztarnowski4434
      @tomasztarnowski4434  Год назад

      Agree.

  • @browaruspierogus2182
    @browaruspierogus2182 2 года назад

    Amazon SDKs tend to get very ugly and abandoned in future

    • @tomasztarnowski4434
      @tomasztarnowski4434  2 года назад

      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