Comments for Test the APIs

From @jayair on Wed Jul 19 2017 19:48:19 GMT+0000 (UTC)

Link to chapter - http://serverless-stack.com/chapters/test-the-apis.html

Copied from original issue: https://github.com/AnomalyInnovations/serverless-stack-com/issues/112

From @yashg5 on Thu Jul 20 2017 08:57:00 GMT+0000 (UTC)

Can you give us example screen shots to test API with postman using AWS signature? thanks in advance

From @jayair on Thu Jul 20 2017 16:52:50 GMT+0000 (UTC)

@yashg5 I’m not too familiar with using Postman. Perhaps somebody else can give it a try.

From @marshallbunch on Thu Jul 20 2017 17:35:34 GMT+0000 (UTC)

First off, thanks a ton for your tutorials. I went through the previous and now the revised. You’re doing great work.

For some reason I’m getting an error when testing the apis:

Error: Invalid UserPoolId format.

at new CognitoUserPool (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\amazon-cognito-identity-js\lib\CognitoUserPool.js:56:13)
at authenticate (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\index.js:70:18)
at Object.<anonymous> (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\index.js:155:1)

I can’t really think of a reason why I would be experiencing this problem. I’ve had no problems for any of the tests I’ve done so far that used the same user-pool-id.

Any chance you could provide a sample apig-test call with actual values?

1 Like

From @jayair on Thu Jul 20 2017 17:45:00 GMT+0000 (UTC)

@marshallbunch Here is a sample one that I was using. I altered some of the values around but the format should be like so.

apig-test \
--username='admin@example.com' \
--password='Passw0rd!' \
--user-pool-id='us-east-1_YBhdXYvix' \
--app-client-id='29qwoeorti0ctjkur5h91lkw5g' \
--cognito-region='us-east-1' \
--identity-pool-id='us-east-1:565cc505-5486-4278-bf0f-9736p9apq8m5' \
--invoke-url='https://w5z1hluqmp5.execute-api.us-east-1.amazonaws.com/test' \
--api-gateway-region='us-east-1' \
--path-template='/notes' \
--method='POST' \
--body='{"content":"hello world","attachment":"hello.jpg"}'

From @marshallbunch on Thu Jul 20 2017 18:00:58 GMT+0000 (UTC)

No cigar. Won’t make it past the user-pool-id.

From @jayair on Thu Jul 20 2017 18:06:22 GMT+0000 (UTC)

@marshallbunch Hmm can I see the complete output from the command?

From @marshallbunch on Thu Jul 20 2017 18:19:02 GMT+0000 (UTC)

Sure:

C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\amazon-cognito-identity-js\lib\CognitoUserPool.js:56
      throw new Error('Invalid UserPoolId format.');
      ^

Error: Invalid UserPoolId format.
    at new CognitoUserPool (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\amazon-cognito-identity-js\lib\CognitoUserPool.js:56:13)
    at authenticate (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\index.js:70:18)
    at Object.<anonymous> (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\index.js:155:1)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)

From @manubhat90 on Thu Jul 20 2017 18:42:01 GMT+0000 (UTC)

For beginners it might be helpful to specify that YOUR_API_GATEWAY_URL should include the stage-name (prod in the example).

“https://ly55wbovq4.execute-api.us-east-1.amazonaws.com/prod”

From @marshallbunch on Thu Jul 20 2017 18:45:42 GMT+0000 (UTC)

did that as well.

From @jayair on Thu Jul 20 2017 18:56:43 GMT+0000 (UTC)

@marshallbunch Can you show me the full command you used as well? I’m not too familiar with the shell in Windows but let’s take a look.

@manubhat90 Good catch. It would work either way but that can be confusing.

From @marshallbunch on Thu Jul 20 2017 19:12:19 GMT+0000 (UTC)

@jayair It is most definitely a syntax problem for the shell in windows. I got past the “invalid UserPoolId” error by getting rid of all the \ and the = and '. The current error is that it doens’t like the json in the body content.

apig-test --username admin@example.com --password Passw0rd! --user-pool-id us-east-1_HwIVtcRh2 --app-client-id 34bb7b87t6rtc387foiletrldf --cognito-region us-east-1 --identity-pool-id us-east-1:285f61b7-8a95-4596-90ce-7f7e1848c2ed --invoke-url https://6xosj8ww6h.execute-api.us-east-1.amazonaws.com/prod/words --api-gateway-region us-east-1' --path-template /words' --method POST --body {"content":"hello world","attachment":"hello.jpg"}
Authenticating with User Pool
Getting temporary credentials
Making API request
C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\request.js:31
            throw err;
            ^

SyntaxError: Unexpected token c in JSON at position 1
    at JSON.parse (<anonymous>)
    at makeRequest (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\index.js:131:19)
    at C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\index.js:114:5
    at C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\credentials.js:123:23
    at Response.<anonymous> (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\credentials\cognito_identity_credentials.js:258:7)
    at Request.<anonymous> (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\request.js:364:18)
    at Request.callListeners (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\sequential_executor.js:105:20)
    at Request.emit (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\sequential_executor.js:77:10)
    at Request.emit (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\request.js:683:14)
    at Request.transition (C:\Users\proet\AppData\Roaming\npm\node_modules\aws-api-gateway-cli-test\node_modules\aws-sdk\lib\request.js:22:10)
1 Like

From @jayair on Thu Jul 20 2017 19:15:49 GMT+0000 (UTC)

@marshallbunch Yeah I think we need to figure out how to format a string as an argument in the command line. And possibly escape the characters properly.

From @tsdorsey on Fri Jul 21 2017 22:56:33 GMT+0000 (UTC)

Hey guys. Great walkthrough. Everything went smoothly until it was time to test the API. I’m getting 502 bad gateway back from API Gateway. I looked in the CloudWatch logs and found that it can’t import modules.

Unable to import module 'create': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/create.js:76:18)
at __webpack_require__ (/var/task/create.js:20:30)
at Object.<anonymous> (/var/task/create.js:171:20)

I dug into this more by running the get lambda from the API gateway console dashboard using the test command. I got this:

Endpoint response body before transformations: 
{
  "errorMessage": "Cannot find module 'babel-runtime/regenerator'",
  "errorType": "Error",
  "stackTrace": [
    "require (internal/module.js:20:19)", 
    "Object.<anonymous> (/var/task/list.js:76:18)", 
    "__webpack_require__ (/var/task/list.js:20:30)", 
    "Object.<anonymous> (/var/task/list.js:176:20)", 
    "__webpack_require__ (/var/task/list.js:20:30)", 
    "/var/task/list.js:63:18", 
    "Object.<anonymous> (/var/task/list.js:66:10)"
  ]
}

This led me to look into the file being uploaded to S3 by serverless deploy and found that they didn’t include webpack. I’ve attached a screenshot of the unzipped file sent to S3 and the files built by using serverless webpack. As you can see, the node_modules folder is very different between them.

Some of the results of serverless deploy:

$ serverless deploy
Serverless: Bundling with Webpack...
Time: 1698ms
     Asset     Size  Chunks             Chunk Names
 create.js  7.54 kB       0  [emitted]  create
 update.js  7.41 kB       1  [emitted]  update
   list.js  7.11 kB       2  [emitted]  list
    get.js  7.06 kB       3  [emitted]  get
 delete.js  6.84 kB       4  [emitted]  delete
handler.js  3.35 kB       5  [emitted]  handler
   [0] external "babel-runtime/core-js/json/stringify" 42 bytes {0} {1} {2} {3} {4} {5} [not cacheable]
   [1] external "babel-runtime/regenerator" 42 bytes {0} {1} {2} {3} {4} [not cacheable]
   [2] external "babel-runtime/helpers/asyncToGenerator" 42 bytes {0} {1} {2} {3} {4} [not cacheable]
   [3] ./lib/dynamodb-lib.js 652 bytes {0} {1} {2} {3} {4} [built]
   [4] external "aws-sdk" 42 bytes {0} {1} {2} {3} {4} [not cacheable]
   [5] ./lib/response-lib.js 721 bytes {0} {1} {2} {3} {4} [built]
   [6] ./create.js 2.99 kB {0} [built]
   [7] external "uuid" 42 bytes {0} [not cacheable]
   [8] ./delete.js 2.37 kB {4} [built]
   [9] ./get.js 2.58 kB {3} [built]
  [10] ./handler.js 696 bytes {5} [built]
  [11] ./list.js 2.61 kB {2} [built]
  [12] ./update.js 2.9 kB {1} [built]
Serverless: Packing external modules: babel-runtime@6.23.0, uuid@3.1.0
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (14.28 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
......................................
Serverless: Stack update finished...
Service Information
service: notes-app-api
stage: prod
region: us-west-2
api keys:
  None
endpoints:
  ---
functions:
  create: notes-app-api-prod-create
  list: notes-app-api-prod-list
  get: notes-app-api-prod-get
  update: notes-app-api-prod-update
  delete: notes-app-api-prod-delete

Any help would be very much appreciated.

From @jayair on Fri Jul 21 2017 23:19:33 GMT+0000 (UTC)

@tsdorsey Thanks for the details. As a quick sanity check, can I see your package.json and serverless.yml?

From @tsdorsey on Sat Jul 22 2017 01:13:48 GMT+0000 (UTC)

@jayair I should have done this before I posted.

I have serverless-dynamodb-local and serverless-offline in my package file but I’m not using them at the moment. I planned to do that later.

From @marshallbunch on Sat Jul 22 2017 01:39:06 GMT+0000 (UTC)

@tsdorsey Good catch. It appears I’m experiencing the same issue.

From @tsdorsey on Sat Jul 22 2017 03:00:28 GMT+0000 (UTC)

@marshallbunch and @jayair I’m newish to webpack. Do you think you could point me in a direction so I could try to help figure this out? Got any gut feelings?

From @tsdorsey on Sat Jul 22 2017 03:24:15 GMT+0000 (UTC)

Ok, so I watched the folders as I ran serverless package and the .webpack folder is created with the dependencies in the node_modules folder as expected. Then the zip file is made and the .webpack folder is cleaned up. I have been reading through the package plugin; I’m suspect of the excludes directive but the aren’t logged… :frowning:

From @navinkumarr on Sat Jul 22 2017 11:52:12 GMT+0000 (UTC)

Getting 403 response

Authenticating with User Pool
Getting temporary credentials
Making API request
{ status: 403,
  statusText: 'Forbidden',
  data: { message: 'Forbidden' } }