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...
Thank you for your remarkable videos!
You are a superstar, Thanks you very much for your videos.
Glad you like them!
I was getting `Error: spawn java ENOENT` so had to do `sls dynamodb install` which fixed the issue
Glad that you figured out how to fix it :) well done
Great work 👍
Thank you so much 😀
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
Looks like in recent versions of serverless-offline, they've added process.env.IS_OFFLINE support back in.
@@joostschuur Yes I was amazed to see that
@Complete Coding Hey, there is a small update. Actually IS_OFFLINE property has been restored. I've tried it - and it works just fine
Awesome to hear. I'll update the description
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
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.
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',
};
@@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.
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.
@@PeteWhelan Have you installed the Java Runtime Engine?
www.oracle.com/java/technologies/javase-jre8-downloads.html
@@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. :)
Hi, great video man i just have 1 issue now my vs code debugger don't work.
Hey Sam this is good but don't you think sls invoke is much easy? And what would you prefer? And why?
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
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"
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
Hi, Can you make a video or explain about debugging in serverless-offline using VSCode?
That's a great idea. I'll add it to the list
Hi, An amazing tutorial. I also checked the github code. but which branch should I check for this tutorial?
Thanks
Here's the link
github.com/SamWSoftware/ServerlessTH-camSeries/tree/l19-serverless-offline
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?
You no longer need to create that file. Serverless now takes the definition straight from the serverless file
@@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?
@@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
Thanks! This helps!!
Gald it's useful
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
Have you set up an aws credentials profile on your computer? It appears to be looking for the correct AWS credentials to use.
Very useful
Glad you think so!
Awesome! Thanks!
I'm glad you enjoyed it
Hi, Sam! Do we need JDK or JRE and whick version ?
The link is in the description
Great guy
Thanks :)
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.
That's strange. I would check that you are calling the response function with all the data you want to be returning.
Hi , why did you name the json file player-score.json ?
You can call it what you want. It could just be called 'player-points.json' or anything els
Is there anyway to debug locally with debugger and breakpoints?
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
@@CompleteCoding do you have a video with the setup for vscode debugging?
@@juanjoseramirezcalderon2618 I do actually
th-cam.com/video/xaBAZm2jfXQ/w-d-xo.html
i get " Serverless command "offline" not found. Did you mean "config"? Run "serverless help" for a list of all available commands."
Have you installed the plugin and added it to the plugin list?
www.serverless.com/plugins/serverless-offline
Hi Sam @complete coding ,
where do this data gets stored locally ?
It runs a local dynamodb table on your machine. So the data is technically stored in the ram of your computer.
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!!!!!
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
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?
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": {
...
}
},
@@CompleteCoding can't we use serverless-dynamodb-local and write the code without AWS credentials?
Awesome! thks!
No problem!
Hi Sam
Do you have examples in java to configure serverless offline?
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
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?
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
@@CompleteCoding just got it ... thank you very much ... you saved my life SAM ...
Any video using Go Lang? instead of Node.js
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
Where's the offline folder in the code repo?
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
@@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.
@@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
is there a way to set breakpoints?
never mind you have a full video on this
This is something that I've started to do a lot more in my lambda development
Ist view
Well done Dhanwa
IS_OFFLINE env variable is back with v6.3.0!
github.com/dherault/serverless-offline/commit/8755d9da8d26cc61a4239d5edd14cc4387d46349
Awesome to hear
IS_OFFLINE is removed in the recent versions github.com/dherault/serverless-offline/commit/331f5418d7c5c9811354a3438388bb098ef4cb38
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.
Say serverless one more time. I double-dare you.
Haha :P It's so hard to do a video on Serverless without saying it all the time
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!
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.