Add a Create Note API

@benoram @jchastain Can you guys try running the command with the debug flag?

$ SLS_DEBUG=* serverless invoke local --function create --path mocks/create-event.json

@jayair , i ran into the same issue as @benoram and jchastain on windows .

see logs with set SLS_debug=*.

The only other thing that i thought i will highlight when i did ā€œnpm install urlā€ step. it showed 4 high vulnerable issues that i asked it to be fixed, Everything else looks right unless i (and others have done something silly)

D:\Learn\serverless\notes-app-api>serverless invoke local --function create --path mocks/create-event.json
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command create
Serverless: Load command install
Serverless: Load command package
Serverless: Load command deploy
Serverless: Load command deploy:function
Serverless: Load command deploy:list
Serverless: Load command deploy:list:functions
Serverless: Load command invoke
Serverless: Load command invoke:local
Serverless: Load command info
Serverless: Load command logs
Serverless: Load command login
Serverless: Load command logout
Serverless: Load command metrics
Serverless: Load command print
Serverless: Load command remove
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command slstats
Serverless: Load command plugin
Serverless: Load command plugin
Serverless: Load command plugin:install
Serverless: Load command plugin
Serverless: Load command plugin:uninstall
Serverless: Load command plugin
Serverless: Load command plugin:list
Serverless: Load command plugin
Serverless: Load command plugin:search
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command webpack
Serverless: Load command offline
Serverless: Load command offline:start
Serverless: Invoke invoke:local
Serverless: Invoke webpack:validate
Serverless: Invoke webpack:compile
Serverless: Bundling with Webpack...
Time: 776ms
Built at: 11/29/2018 12:53:50 AM
        Asset      Size  Chunks             Chunk Names
    create.js  7.35 KiB  create  [emitted]  create
create.js.map   7.2 KiB  create  [emitted]  create
Entrypoint create = create.js create.js.map
[./create.js] 2.26 KiB {create} [built]
[aws-sdk] external "aws-sdk" 42 bytes {create} [built]
[babel-runtime/core-js/json/stringify] external "babel-runtime/core-js/json/stringify" 42 bytes {create} [built]
[source-map-support/register] external "source-map-support/register" 42 bytes {create} [built]
[uuid] external "uuid" 42 bytes {create} [built]
Serverless: INVOKING INVOKE
{}

  Error --------------------------------------------------

  Exception encountered when loading D:\Learn\serverless\notes-app-api\.webpack\service\create

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Exception encountered when loading D:\Learn\serverless\notes-app-api\.webpack\service\create
    at AwsInvokeLocal.invokeLocalNodeJs (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\plugins\aws\invokeLocal\index.js:272:13)
    at AwsInvokeLocal.invokeLocal (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\plugins\aws\invokeLocal\index.js:125:19)
From previous event:
    at Object.invoke:local:invoke [as hook] (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\plugins\aws\invokeLocal\index.js:26:10)
    at BbPromise.reduce (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:390:55)
From previous event:
    at PluginManager.invoke (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:390:22)
    at PluginManager.run (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:421:17)
    at variables.populateService.then.then (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\Serverless.js:157:33)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
    at process.topLevelDomainCallback (domain.js:121:23)
From previous event:
    at Serverless.run (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\lib\Serverless.js:144:8)
    at serverless.init.then (C:\Users\d372426\AppData\Roaming\npm\node_modules\serverless\bin\serverless:43:50)

  Get Support --------------------------------------------
     Docs:          docs.serverless
     Bugs:          github/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information -----------------------------
     OS:                     win32
     Node Version:           10.13.0
     Serverless Version:     1.32.0

like others refactoring the code fixed it. Still wondering what the problem was ? Wondering if import of aws-sdk in create.js was creating a conflict with webpack !

I have the same issue and you and other say that refactoring works.
However, I have no idea what you mean by this.

Continue with next steps where the author introduces lib , asks to re write create.js.

Allright, I though you guys where doing something in your editors (there is for instance a ā€œrefactorā€ command in VisualStudio Code). I guess the problem is with the function callback(null,params) ? What is the definition of callback?

@ravin018 Thanks for trying to debug it. I donā€™t know if itā€™s the aws-sdk because all we did was move it around. But Iā€™ll try it out and see what is going on.

@KDkeml Here is some more info on the callback function.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

I re-ran through from the beginning, and this time it worked. The only difference Iā€™m aware of between the 1st and 2nd tries is that the first time I ran an npm audit fix, the second time I did not.

1 Like

Hi,

Thanks for the great tutorial!

I have no response wit the call.

With the debug Iā€™ve got thisā€¦

SLS_DEBUG=* sls invoke local --function create --path mocks/create-event.json 

Serverless: Load command config

Serverless: Load command config:credentials

Serverless: Load command create

Serverless: Load command install

Serverless: Load command package

Serverless: Load command deploy

Serverless: Load command deploy:function

Serverless: Load command deploy:list

Serverless: Load command deploy:list:functions

Serverless: Load command invoke

Serverless: Load command invoke:local

Serverless: Load command info

Serverless: Load command logs

Serverless: Load command login

Serverless: Load command logout

Serverless: Load command metrics

Serverless: Load command print

Serverless: Load command remove

Serverless: Load command rollback

Serverless: Load command rollback:function

Serverless: Load command slstats

Serverless: Load command plugin

Serverless: Load command plugin

Serverless: Load command plugin:install

Serverless: Load command plugin

Serverless: Load command plugin:uninstall

Serverless: Load command plugin

Serverless: Load command plugin:list

Serverless: Load command plugin

Serverless: Load command plugin:search

Serverless: Load command config

Serverless: Load command config:credentials

Serverless: Load command rollback

Serverless: Load command rollback:function

Serverless: Load command webpack

Serverless: Load command offline

Serverless: Load command offline:start

Serverless: Invoke invoke:local

Serverless: Invoke webpack:validate

Serverless: Invoke webpack:compile

Serverless: Bundling with Webpack...

Time: 419ms

Built at: 2018-12-07 16:12:13

Asset Size Chunks Chunk Names

create.js 7.42 KiB create [emitted] create

create.js.map 7.14 KiB create [emitted] create

Entrypoint create = create.js create.js.map

[./create.js] 2.33 KiB {create} [built]

[aws-sdk] external "aws-sdk" 42 bytes {create} [built]

[babel-runtime/core-js/json/stringify] external "babel-runtime/core-js/json/stringify" 42 bytes {create} [built]

[source-map-support/register] external "source-map-support/register" 42 bytes {create} [built]

[uuid] external "uuid" 42 bytes {create} [built]

Any idea?

Did you try the suggestion that the others had? Try completing the chapter and seeing if it fixes it?

Edit:

I tried out the steps with Serverless Framework version 1.34.1 and it seemed fine. Which version are you using?

Thanks @jayair, my versions were outdatedā€¦ with this versions everything works as expectedā€¦

  • npm 6.4.1
  • node v11.4.0
  • sls 1.34.1
1 Like

Great! Thanks for reporting back.

Suggestion, perhaps update the serverless.yml code with a comment, as I ran into this problem.

For the serverless.yml, in the iamroleStatements section, the region needs to be the region that the dynamoDB was created in.

Example, for me, it needed to be Resource: ā€œ;arn:aws:dynamodb:us-east-2::ā€

Did no give me any errors/issues till a much later chapter (test the APIs)

1 Like

Iā€™ll add a note to the chapter. Thanks for pointing it out.

Why did you use cognitoIdentityId and not a userā€™s sub for userId in table?

The cognitoIdentityId is what is provided to you by the Lambda function. The User Pool user id is a bit more complicated. We talk about it in the chapter and link to it as well.

Iā€™m getting an odd error here upon testing, both before and after I refactor the code.

Before:

notes-app-api master āœ— 1h28m ā—’ āžœ serverless invoke local --function create --path mocks/create-event.json
Serverless: Bundling with Webpack...
Time: 427ms
Built at: 01/24/2019 7:21:08 PM
        Asset      Size  Chunks             Chunk Names
    create.js  7.16 KiB  create  [emitted]  create
create.js.map  6.87 KiB  create  [emitted]  create
Entrypoint create = create.js create.js.map
[./create.js] 2.08 KiB {create} [built]
[aws-sdk] external "aws-sdk" 42 bytes {create} [built]
[babel-runtime/core-js/json/stringify] external "babel-runtime/core-js/json/stringify" 42 bytes {create} [built]
[source-map-support/register] external "source-map-support/register" 42 bytes {create} [built]
[uuid] external "uuid" 42 bytes {create} [built]
{
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true
    },
    "body": "{\"userId\":\"USER-SUB-1234\",\"noteId\":\"e0300100-2047-11e9-89e0-934ea59836fa\",\"content\":\"hello world\",\"attachment\":\"hello.jpg\",\"createdAt\":1548382868752}"
}
Error: socket hang up
    at createHangUpError (_http_client.js:330:15)
    at TLSSocket.socketOnEnd (_http_client.js:433:23)
    at TLSSocket.emit (events.js:193:15)
    at TLSSocket.EventEmitter.emit (domain.js:439:20)
    at endReadableNT (_stream_readable.js:1129:12)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)

After I reformat the code, I only get the ā€œsocket hang upā€ error:

notes-app-api master āœ— 1h30m ā—’ āžœ serverless invoke local --function create --path mocks/create-event.json
Serverless: Bundling with Webpack...
Time: 491ms
Built at: 01/24/2019 7:24:00 PM
        Asset      Size  Chunks             Chunk Names
    create.js    10 KiB  create  [emitted]  create
create.js.map  7.08 KiB  create  [emitted]  create
Entrypoint create = create.js create.js.map
[./create.js] 2.32 KiB {create} [built]
[./libs/dynamodb-lib.js] 468 bytes {create} [built]
[./libs/response-lib.js] 762 bytes {create} [built]
[aws-sdk] external "aws-sdk" 42 bytes {create} [built]
[babel-runtime/core-js/json/stringify] external "babel-runtime/core-js/json/stringify" 42 bytes {create} [built]
[babel-runtime/helpers/asyncToGenerator] external "babel-runtime/helpers/asyncToGenerator" 42 bytes {create} [built]
[babel-runtime/regenerator] external "babel-runtime/regenerator" 42 bytes {create} [built]
[source-map-support/register] external "source-map-support/register" 42 bytes {create} [built]
[uuid] external "uuid" 42 bytes {create} [built]
Error: socket hang up
    at createHangUpError (_http_client.js:330:15)
    at TLSSocket.socketOnEnd (_http_client.js:433:23)
    at TLSSocket.emit (events.js:193:15)
    at TLSSocket.EventEmitter.emit (domain.js:439:20)
    at endReadableNT (_stream_readable.js:1129:12)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)

In both cases the test file is created and shows up in DynamoDB. However, when I got on to create and test get.js in the next section, I get another ā€œsocket hang upā€ error like that last one, and it doesnā€™t get the test file. Up to this point, Iā€™ve copied and pasted all the code directly from the tutorial, so itā€™s exactly the same (and I double checked). The indentation in serverless.yml is perfect, and the folder structure matches the example on Github. I also made sure my region matched throughout the project.

Iā€™m stumped.

Thatā€™s really weird. Have you tried deploying this? I think the error is just with your local environment.

Same socket error when I deploy. Unfortunately I think youā€™re rightā€¦Iā€™ll report back if I figure anything out.

You mean the serverless deploy command also failed? Definitely seems like something is up on your local environment. Try updating serverless, node, and npm maybe?