Add a Create Note API

I was wondering why do we use PUT in order to create entry in Dynamo. Why did not we use POST? Do they differ ?

As far as I know, generally, we use POST for creating new entries, and for the PUT to update the existing entries.

If you are referring to HTTP POST vs PUT that doesn’t apply to DynamoDB. We use the put method in the DynamoDB Client SDK - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property.

Hi, using windows. No idea what i’ve done wrong, i’ve just copied and pasted for the most part, can you please assist:

PS D:\Shadow\Desktop\notes-app-api> serverless invoke local --function create --path mocks/create-event.json

  Syntax Error -------------------------------------------

  D:\Shadow\Desktop\notes-app-api\node_modules\serverless-offline\src\ServerlessOffline.js:157
      );
      ^

  SyntaxError: Unexpected token )
      at createScript (vm.js:56:10)
      at Object.runInThisContext (vm.js:97:10)
      at Module._compile (module.js:542:28)
      at Object.Module._extensions..js (module.js:579:10)
      at Module.load (module.js:487:32)
      at tryModuleLoad (module.js:446:12)
      at Function.Module._load (module.js:438:3)
      at Module.require (module.js:497:17)
      at require (internal/module.js:20:19)
      at Object.<anonymous> (D:\Shadow\Desktop\notes-app-api\node_modules\serverless-offline\src\index.js:3:18)
      at Module._compile (module.js:570:32)
      at Object.Module._extensions..js (module.js:579:10)
      at Module.load (module.js:487:32)
      at tryModuleLoad (module.js:446:12)
      at Function.Module._load (module.js:438:3)
      at Module.require (module.js:497:17)
      at require (internal/module.js:20:19)
      at plugins.forEach.error (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:107:18)
      at Array.forEach (native)
      at PluginManager.loadPlugins (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:102:13)
      at PluginManager.loadServicePlugins (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:147:10)
      at PluginManager.loadAllPlugins (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\lib\classes\PluginManager.js:97:10)
      at pluginManager.loadConfigFile.then.then (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\lib\Serverless.js:78:35)
      at tryCatcher (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\util.js:16:23)
      at Promise._settlePromiseFromHandler (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:517:31)
      at Promise._settlePromise (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:574:18)
      at Promise._settlePromise0 (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:619:10)
      at Promise._settlePromises (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:699:18)
      at Promise._fulfill (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:643:18)
      at Promise._resolveCallback (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:437:57)
      at Promise._settlePromiseFromHandler (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:529:17)
      at Promise._settlePromise (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:574:18)
      at Promise._settlePromise0 (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:619:10)
      at Promise._settlePromises (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:699:18)
      at Promise._fulfill (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:643:18)
      at PromiseArray._resolve (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise_array.js:126:19)
      at PromiseArray._promiseFulfilled (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise_array.js:144:14)
      at Promise._settlePromise (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:579:26)
      at Promise._settlePromise0 (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:619:10)
      at Promise._settlePromises (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:699:18)
      at Promise._fulfill (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:643:18)
      at Promise._resolveCallback (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:437:57)
      at Promise._settlePromiseFromHandler (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:529:17)
      at Promise._settlePromise (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:574:18)
      at Promise._settlePromise0 (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:619:10)
      at Promise._settlePromises (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:699:18)
      at Promise._fulfill (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:643:18)
      at PropertiesPromiseArray.PromiseArray._resolve (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise_array.js:126:19)
      at PropertiesPromiseArray._promiseFulfilled (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\props.js:78:14)
      at Promise._settlePromise (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:579:26)
      at Promise._settlePromise0 (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:619:10)
      at Promise._settlePromises (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:699:18)
      at Promise._fulfill (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:643:18)
      at Promise._resolveCallback (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:437:57)
      at Promise._settlePromiseFromHandler (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:529:17)
      at Promise._settlePromise (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:574:18)
      at Promise._settlePromise0 (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:619:10)
      at Promise._settlePromises (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\promise.js:695:18)
      at _drainQueueStep (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\async.js:138:12)
      at _drainQueue (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\async.js:131:9)
      at Async._drainQueues (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\async.js:147:5)
      at Immediate.Async.drainQueues (C:\Users\Shadow\AppData\Roaming\npm\node_modules\serverless\node_modules\bluebird\js\release\async.js:17:14)
      at runCallback (timers.js:672:20)
      at tryOnImmediate (timers.js:645:5)
      at processImmediate [as _immediateCallback] (timers.js:617:5)

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

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

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              6.11.2
     Framework Version:         1.52.0
     Plugin Version:            2.0.0
     SDK Version:               2.1.1

Seems my node version was simply too old, updated and now fixed. Maybe a warning of the minimum node could be checked too?

So it is working now after you updated your Node version?

When a give the command
serverless invoke local --function create --path mocks/create-event.json
nothing happens

To debug this try:

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

I followed the tutorial pretty easily until I got to getting a response

  message: 'One or more parameter values were invalid: Missing the key userid in the item',
  code: 'ValidationException',
  time: 2019-10-29T17:17:03.743Z,
  requestId: 'B53FTSNP519K6KN7NPLADF4SNRVV4KQNSO5AEMVJF66Q9ASUAAJG',

Copied the codes exactly, I’m in the us-east-1 region, all my AWS credentials are set up correctly.

Is the create-event.json wrong? Aren’t you supposed to specify userid when writing to the table? Am I literally supposed to use USER-SUB-1234 as the cognito ID or an actual ID from my Cognito pool?

Just a heads up we use userId instead of userid in the tutorial. So make sure you are using the right one.

I have a problem with serverless invoke local --function create --path mocks/create-event.json as well.

I get the output:

Serverless: DOTENV: Could not find .env file.

  Serverless Error ---------------------------------------

  Function "create" doesn't exist in this Service

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

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              12.13.0
     Framework Version:         1.56.1
     Plugin Version:            3.2.1
     SDK Version:               2.2.0
     Components Core Version:   1.1.2
     Components CLI Version:    1.4.0

I was also getting

Serverless Error ---------------------------------------
Function "create" doesn't exist in this Service

Am running in Windows. I got past this error by removing the comments from functions part of the serverless.yml file.

functions:
  create:
    handler: create.main
    events:
      - http:
          path: notes
          method: post
          cors: true
          authorizer: aws_iam

Now onto troubleshooting the next issue! status code 500 (rofl)

1 Like

Yeah, usually these errors are caused by indenting issues in the serverless.yml.

Make sure you are in the right directory when running the command. Happened to me, then realized I was running it from the libs directory. You have to run it from the root directory based on your --path parameter. I know this is a late reply, but in case this happens to any one else. It will run the command, and then just go to a new line in the command prompt/terminal with no error if you are not running the command at the right directory level.

Hi all what have I done wrong that I’m getting eslint strict errors? There’s a time and place for these nitpicks but not while I’m exploring and they’re holding me up. Any guidance as to what I did wrong / how to fix it would be most appreciated :slight_smile:

/Users/julian/learn/serverless-stack/notes-app-api/create.js
   3:8   error  A space is required after '{'            object-curly-spacing
   3:25  error  A space is required before '}'           object-curly-spacing
   4:1   error  More than 1 blank line not allowed       no-multiple-empty-lines
  19:28  error  Strings must use singlequote             quotes
  20:5   error  Expected space(s) after "return"         keyword-spacing
  21:5   error  Expected space(s) after "catch"          keyword-spacing
  24:1   error  Trailing spaces not allowed              no-trailing-spaces
  25:1   error  Block must not be padded by blank lines  padded-blocks

✖ 8 problems (8 errors, 0 warnings)

I tried to soften the rules in an eslintConfig section inside package.json but it doesn’t seem to be respecting it:

  "eslintConfig": {
    "extends": "eslint-config-serverless",
    "rules": {
      "whatever": "blah",
      "semi": "warn",
      "space-before-function-paren": "warn",
      "no-trailing-spaces": "warn"
    }

As an interim to this I looked at the configuration for serverless-bundle and that helped – I disabled eslint for now by adding this into serverless.yml

custom:
  # for serverless-bundle -- webpack wrapper
  bundle:
    sourcemaps: true      # Enable source maps
    caching: false        # While in development don't cache
    stats: false          # Don't print out any Webpack output
    linting: false        # Disable linting as a part of the build process

Good call. Glad you figured it out.

You saved me man, I literally was wrecking my head about why such a simple command wouldn’t work and would simply go to a new line almost like it was executed but not outputting anything. (Some old issues even pointed to a serverless version not being up to date ). And when I read this and when I realized that somehow I was outside of the scope of my root folder I did a strong facepalm. Thanks a lot :slight_smile:

1 Like

Thanks for that, helped me out, as I was having problems due to eslint formatting rules :slight_smile: This solved it :slight_smile:

1 Like

I keep getting statusCode 500 with ‘Requested resource not found’. I didn’t notice the ‘env example’ file, I’ve renamed it to ‘.env’, added

tableName=notes

but that doesn’t seem to solve the problem as I keep getting ‘{ ResourceNotFoundException: Requested resource not found’

Here’s the content of my .env.

tableName=notes

This is what the serverless.yaml contains:

    environment:
        tableName: notes

I’ve also checked the table in the DB and it’s named ‘notes’

UPDATE:

for some reason I had my table and everything else created on the region:us-east-2 and all the settings in serverless.yml were using us-east-1 :slight_smile: changed it to 2 and it worked :wink:

also, it’d be good to mention in the tutorial that you NEED to create .env and add there tableName=nameOfYourTableInDynamoDB

I know that there’s a README, but when I followed the tutorial, I didn’t focus on exploring the files. perhaps my bad!

Glad you figured it out!