Add a Create Note API


#122

@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

#123

@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

#124

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 !


#125

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


#126

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


#127

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?


#128

@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


#129

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.


#130

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?


#131

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?


#132

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

#133

Great! Thanks for reporting back.


#134

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)


#135

I’ll add a note to the chapter. Thanks for pointing it out.


#136

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


#137

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.


#138

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.


#139

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


#140

Same socket error when I deploy. Unfortunately I think you’re right…I’ll report back if I figure anything out.


#141

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?