Serverless Offline for testing on your local machine. Tutorial with DynamoDB

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • Playlist Available Here:
    • Production Ready Serve...
    In this video we'll be learning to set up serverless-local. This allows us to test that our code is working without having to deploy it onto AWS. This means you can test your changes faster so you end up building features quicker.
    Install the Java Runtime Engine (needed for Dynamo)
    www.oracle.com...
    Code Available Here:
    github.com/Sam...

ความคิดเห็น • 96

  • @hellaren
    @hellaren ปีที่แล้ว

    Thank you for your remarkable videos!

  • @jimishshah82
    @jimishshah82 4 ปีที่แล้ว +1

    You are a superstar, Thanks you very much for your videos.

  • @jimishshah82
    @jimishshah82 4 ปีที่แล้ว +3

    I was getting `Error: spawn java ENOENT` so had to do `sls dynamodb install` which fixed the issue

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว +1

      Glad that you figured out how to fix it :) well done

  • @nayeemurrehman4922
    @nayeemurrehman4922 3 ปีที่แล้ว +1

    Great work 👍

  • @CompleteCoding
    @CompleteCoding  4 ปีที่แล้ว +2

    If you get an error with this process, check the version of serverless-offline that you have installed. There has been a change in V6 that breaks this process. To resolve it, update to the latest version of serverless-offline

    • @joostschuur
      @joostschuur 3 ปีที่แล้ว

      Looks like in recent versions of serverless-offline, they've added process.env.IS_OFFLINE support back in.

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      @@joostschuur Yes I was amazed to see that

  • @xmelsky
    @xmelsky 3 ปีที่แล้ว

    @Complete Coding Hey, there is a small update. Actually IS_OFFLINE property has been restored. I've tried it - and it works just fine

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      Awesome to hear. I'll update the description

  • @costagiannakopoulos2575
    @costagiannakopoulos2575 ปีที่แล้ว

    Great video, I am using serverless offline for my gql lambda so I can run some automated tests against it. When running jest test’s sequentially they all pass but when I run them in parallel the offline lambda fails and shuts down. Do you have any tips on how to solve this? Thanks

  • @PeteWhelan
    @PeteWhelan 4 ปีที่แล้ว +2

    Hi sam - thanks for the great videos again I plan to work through the production-ready series this week. In this video, you have included a plugin called serverless-webpack to the code in the yml. I can't find where you put that into the program on any of the previous tutorials - can you point me in the right direction with a link to the tutorial that you introduce it, please?
    Thanks.

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว +5

      Hey Pete, Just looked through all of my videos and realised that I never uploaded my webpack video. I'll do that this week but for now heres a summary
      Short summary:
      We use webpack as by default serverless uploads all the code from the repo into every lambda. When you have lots of code (10-15 endpoints and lots of common code) you hit the upload limit of the lambda. Webpack works to only include the code that you need, reducing the upload size.
      Steps to setup:
      npm install --save-dev webpack serverless-webpack
      add serverless-webpack to the plugin list
      add this to your serverless.yml file
      package:
      individually: true
      Create a webpack.config.js file at the root of your repo containing this
      module.exports = {
      target: 'node',
      mode: 'none',
      };

    • @PeteWhelan
      @PeteWhelan 4 ปีที่แล้ว

      @@CompleteCoding thanks Sam I have that working in the code now but I can't get the offline dynamodb to start in the final step. Everything else works. The error seems to point to (!child.pid) - any idea where I'm going wrong? Could it be something to do with the Java developer kit? It's not clear in the video what role that plays.

    • @PeteWhelan
      @PeteWhelan 4 ปีที่แล้ว

      Error --------------------------------------------------
      Error: spawn java ENOENT
      at Process.ChildProcess._handle.onexit (internal/child_process.js:232:19)
      at onErrorNT (internal/child_process.js:407:16)
      at process._tickCallback (internal/process/next_tick.js:63:19)
      For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว +1

      @@PeteWhelan Have you installed the Java Runtime Engine?
      www.oracle.com/java/technologies/javase-jre8-downloads.html

    • @PeteWhelan
      @PeteWhelan 4 ปีที่แล้ว +2

      @@CompleteCoding Thanks Sam - the following steps solved this issue. I downloaded the java engine from the link, rebooted my computer. Deleted DynamoBd folder from the code and reinstalled DynamoDB PS on your video and Github code - you have "ProvisionedThoughput", I've assumed that is a typo and have replaced with "ProvisionedThroughput" in the player-score.json. Looking forward to the next video. :)

  • @alashish
    @alashish ปีที่แล้ว

    Hi, great video man i just have 1 issue now my vs code debugger don't work.

  • @theinnoverse
    @theinnoverse ปีที่แล้ว

    Hey Sam this is good but don't you think sls invoke is much easy? And what would you prefer? And why?

    • @CompleteCoding
      @CompleteCoding  ปีที่แล้ว +1

      I've changed my opinion on this since making the video.
      I mainly do end to end testing once it's been deployed. I either have a 'dev' environment or a temporary one.
      Offline is ok if your app is super simple ( just API and Dynamo) but once you add any other services, trying to mock things becomes too messy

  • @cristianecheverria3995
    @cristianecheverria3995 4 ปีที่แล้ว

    I was having issues running "serverless offline" with "webpack". Something saying "serverless offline ERROR in Entry module not found: Error: Can't resolve './src'" and wasn't possible to test locally using Postman. I found a solution with this code inside webpack.config.js. Because it seems like was looking for "./src" folder as the entry point.
    //webpack config
    const slsw = require("serverless-webpack")
    module.exports = {
    entry: slsw.lib.entries,
    target: "node",
    mode: "none",
    }
    I'm using
    "serverless-offline": "6.4.0"

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว

      Yes, this video was recorded back when it was serverless-offline 5.x and that didn't require this change. I'll add something to the description to outline this

  • @combinedaccount5774
    @combinedaccount5774 4 ปีที่แล้ว +1

    Hi, Can you make a video or explain about debugging in serverless-offline using VSCode?

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว

      That's a great idea. I'll add it to the list

  • @dularamalindu427
    @dularamalindu427 2 ปีที่แล้ว

    Hi, An amazing tutorial. I also checked the github code. but which branch should I check for this tutorial?

    • @CompleteCoding
      @CompleteCoding  2 ปีที่แล้ว

      Thanks
      Here's the link
      github.com/SamWSoftware/ServerlessTH-camSeries/tree/l19-serverless-offline

  • @ArturBanul
    @ArturBanul 3 ปีที่แล้ว +1

    Hi, I just watched you video. It was really helpful. One thing is not clear to me. Why did you created player-score.json file and added there table defininition which was already in serverless.yml? Why this table definition is duplicated?

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว +3

      You no longer need to create that file. Serverless now takes the definition straight from the serverless file

    • @usamashahid3172
      @usamashahid3172 2 ปีที่แล้ว

      @@CompleteCoding I have completely opposite issue, I dont want to define table in yml file, since it is already deployed through another service. Is it possible? or can I make serverless.yml to ignore the table while deploying and only use it for testing?

    • @CompleteCoding
      @CompleteCoding  2 ปีที่แล้ว +1

      @@usamashahid3172 There should be a way to do that but it's not simple and would be easier if you were using serverless.ts
      create a new js/ts file just for your dynamo config. In that file it needs to be a function that takes the serverless config as a parameter.
      That function should check if this is running as a deployment or running locally and return either no resources when deploying and the dynamo table resource when running locally.
      I've never had to do this but it should be possible

  • @madhavareddy3488
    @madhavareddy3488 2 ปีที่แล้ว

    Thanks! This helps!!

  • @gasparh1886
    @gasparh1886 3 ปีที่แล้ว

    Hello, when I run your example I get "error in dynamo write Error: connect ENETUNREACH" "'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1" I am stuck here, thanks

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      Have you set up an aws credentials profile on your computer? It appears to be looking for the correct AWS credentials to use.

  • @MrRobinkv
    @MrRobinkv 3 ปีที่แล้ว

    Very useful

  • @JeanYvesP
    @JeanYvesP 4 ปีที่แล้ว

    Awesome! Thanks!

  • @VasylHerman
    @VasylHerman 3 ปีที่แล้ว

    Hi, Sam! Do we need JDK or JRE and whick version ?

  • @MrHossamsaraya
    @MrHossamsaraya 3 ปีที่แล้ว

    Great guy

  • @tienlevan1817
    @tienlevan1817 3 ปีที่แล้ว

    Hi Sam,
    Thanks for the great video.
    I have a problem, I can see the response as in console.log. But i can't find the response in POST MAN app. Can you help?
    Thanks.

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      That's strange. I would check that you are calling the response function with all the data you want to be returning.

  • @abdoualgerian5396
    @abdoualgerian5396 3 ปีที่แล้ว

    Hi , why did you name the json file player-score.json ?

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      You can call it what you want. It could just be called 'player-points.json' or anything els

  • @JeanYvesP
    @JeanYvesP 4 ปีที่แล้ว +1

    Is there anyway to debug locally with debugger and breakpoints?

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว +1

      Yes you can. If you are using VS-Code then you can stick breakpoints wherever you like. You can then use the built in debugger to run all of your tests, stopping at the breakpoints so you can view the variables at that point.
      completecoding.io/content/images/2020/03/Screenshot-2020-03-06-at-07.29.36.png

    • @juanjoseramirezcalderon2618
      @juanjoseramirezcalderon2618 3 ปีที่แล้ว

      @@CompleteCoding do you have a video with the setup for vscode debugging?

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      @@juanjoseramirezcalderon2618 I do actually
      th-cam.com/video/xaBAZm2jfXQ/w-d-xo.html

  • @Passion4CoolMusic
    @Passion4CoolMusic 3 ปีที่แล้ว

    i get " Serverless command "offline" not found. Did you mean "config"? Run "serverless help" for a list of all available commands."

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      Have you installed the plugin and added it to the plugin list?
      www.serverless.com/plugins/serverless-offline

  • @pravallikak8794
    @pravallikak8794 2 ปีที่แล้ว

    Hi Sam @complete coding ,
    where do this data gets stored locally ?

    • @CompleteCoding
      @CompleteCoding  2 ปีที่แล้ว

      It runs a local dynamodb table on your machine. So the data is technically stored in the ram of your computer.

  • @udaysingh7748
    @udaysingh7748 3 ปีที่แล้ว

    Hi,Using express it is giving error {
    "currentRoute": "get - /api/user",
    "error": "Serverless-offline: route not found.",
    "existingRoutes": [
    "* - /api"
    ],
    "statusCode": 404
    }
    It is only with express!!!!!

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      I've never worked with Serverless and Express but I think I know the issue.
      If you run 'sls info' what urls do you get?
      If you don't get "someapi/{stage}/*" then you might need to change the paths in your function definition
      I'm assuming you have one function with all your express logic in? It should look something like this
      ExpressFunction:
      handler: src/ExpressFunction.main
      events:
      - http:
      path: *
      method: ANY
      cors: true

  • @ofirmagen4224
    @ofirmagen4224 2 ปีที่แล้ว

    Hi Sam .. Hopefully you're still following comments on this video. I just inherited a big serverless project that all of the dynamoDB tables were created on AWS directly (as opposed to having a resource in the serverless.yml file). Can I still run serverless-dynamodb-local w/out creating that json file (like your player-score.json)? If not, do you have an idea how I can grab the json version (keyschema, etc..) from an already running dynamoDB table?

    • @CompleteCoding
      @CompleteCoding  2 ปีที่แล้ว

      You would have to create a dyanmoDB table config for each of the tables you use to get it working with serverless dynamo local.
      You would then have to add some logic to the table so it doesn't actually try deploying it - as you would be trying to create a table that already exists. You can use "conditions" in cloudformation to do that.
      "Conditions": {
      "CreateDynamoResources": {
      "Fn::Equals": [
      {
      "Ref": "EnvType"
      },
      "test"
      ]
      }
      },
      "Resources": {
      "DynamoTable1": {
      "Type": "AWS::DynamoDB::Table",
      "Condition": "CreateDynamoResources",
      "Properties": {
      ...
      }
      },

    • @pravallikak7794
      @pravallikak7794 2 ปีที่แล้ว +1

      @@CompleteCoding can't we use serverless-dynamodb-local and write the code without AWS credentials?

  • @ivoneijr
    @ivoneijr 3 ปีที่แล้ว

    Awesome! thks!

  • @Saswatxp
    @Saswatxp 3 ปีที่แล้ว

    Hi Sam
    Do you have examples in java to configure serverless offline?

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      I've never worked in Java and I think that it might be more difficult as lambda doesn't natively support java. You could look into the new serverless containers (docker) to run your java code

  • @shwenansuwai2481
    @shwenansuwai2481 4 ปีที่แล้ว

    It is working normally at my friend's PC....
    I do not know what happened...
    At my side, I still receive this message [Serverless-Offline] Your lambda handler 'createPlayerScore' timed out after 30000ms. by testing with postman / insomnia / curl command ...
    Any advice is much appreciated on how to fix this?

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว +1

      Check the code in your lambda. Make sure that there isn't a loop or something in the code. If it is a request with Dynamo, make sure that you'e installed `sls dynamodb install`.
      There is also a change with v6 of the serverless offline plugin where process.env.IS_OFFLINE doesn't exist. If you add a console.log to the code and log out that variable you should see what it's value is. If it is undefined then you can resolve this by changing the version of serverless-offline (in you package.json) file to 5.12.1, deleting node modules and package-lock and running `npm install` again

    • @shwenansuwai2481
      @shwenansuwai2481 4 ปีที่แล้ว

      ​@@CompleteCoding just got it ... thank you very much ... you saved my life SAM ...

  • @BELLA-cf9md
    @BELLA-cf9md 3 ปีที่แล้ว

    Any video using Go Lang? instead of Node.js

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      I've worked in Go a little bit but not enough yet to be confident making videos on it. The serverless parts of my videos should be the same, just with a change in the actual lambda file

  • @ddsmax
    @ddsmax 3 ปีที่แล้ว

    Where's the offline folder in the code repo?

    • @CompleteCoding
      @CompleteCoding  2 ปีที่แล้ว

      You don't need a whole folder for offline, just adding "serverless-offline" as a plugin in the serverless.yml file.
      I've also used "serverless-dynamodb-local" in this video for running Dynamo local as well

    • @ddsmax
      @ddsmax 2 ปีที่แล้ว

      @@CompleteCoding You are missing the offline/migrations folder in the code repo for this video. The repo for the next video in this playlist has the folder.

    • @CompleteCoding
      @CompleteCoding  2 ปีที่แล้ว

      @@ddsmax You don't need that folder. Dynamo-local will automatically use the Dynamo config in your resources file to create the local table.
      If you are connecting to a dynamo table that you don't create in that repo you can use this migrations folder. I need to delete it from the next repo as it's not needed

  • @alifarah9
    @alifarah9 2 ปีที่แล้ว

    is there a way to set breakpoints?

    • @alifarah9
      @alifarah9 2 ปีที่แล้ว

      never mind you have a full video on this

    • @CompleteCoding
      @CompleteCoding  2 ปีที่แล้ว

      This is something that I've started to do a lot more in my lambda development

  • @dhanwadc
    @dhanwadc 4 ปีที่แล้ว +2

    Ist view

  • @devilssoccer1
    @devilssoccer1 4 ปีที่แล้ว

    IS_OFFLINE env variable is back with v6.3.0!
    github.com/dherault/serverless-offline/commit/8755d9da8d26cc61a4239d5edd14cc4387d46349

  • @nichenjie
    @nichenjie 4 ปีที่แล้ว

    IS_OFFLINE is removed in the recent versions github.com/dherault/serverless-offline/commit/331f5418d7c5c9811354a3438388bb098ef4cb38

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว

      I know, it is very frustrating. You can install version 5.12.1 of serverless offline which still works.
      I'll comment again when I have a solution to resolve this.

  • @RomeoMihalcea
    @RomeoMihalcea 3 ปีที่แล้ว

    Say serverless one more time. I double-dare you.

    • @CompleteCoding
      @CompleteCoding  3 ปีที่แล้ว

      Haha :P It's so hard to do a video on Serverless without saying it all the time

  • @leugimsimara
    @leugimsimara 4 ปีที่แล้ว

    Hey, thank you for the tutorial, pretty cool. Take a look at the httpie lib, it has saved me several hours: httpie.org, you can do things like `http post :15001/create-player-score/1234 name=Sam age:=23`. Cheers!

    • @CompleteCoding
      @CompleteCoding  4 ปีที่แล้ว

      That looks like a really smooth library. Do you know if there is any history of requests? One of the things I do most in postman is searching through old request to re-use one.