Call the Create API


#21

From @ramonpm on Fri Aug 11 2017 13:53:03 GMT+0000 (UTC)

I’m having the same CORS problem here, and my serverless.yml is the same of this https://github.com/AnomalyInnovations/serverless-stack-demo-api/blob/251d1defa682c70c7af770ba386b2069ef3eefda/serverless.yml#L42


#22

From @jayair on Fri Aug 11 2017 17:10:08 GMT+0000 (UTC)

@ramonpm Does calling the Create API directly work for you (like in this chapter - http://serverless-stack.com/chapters/test-the-apis.html)?


#23

From @ramonpm on Fri Aug 11 2017 17:20:16 GMT+0000 (UTC)

Yes @jayair, it works for me when I call it directly.

2017-08-11 14:10 GMT-03:00 Jay V notifications@github.com:

@ramonpm https://github.com/ramonpm Does calling the Create API
directly work for you (like in this chapter - http://serverless-stack.com/
chapters/test-the-apis.html)?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/AnomalyInnovations/serverless-stack-com/issues/48#issuecomment-321868080,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAM6dIIK9us52eK8h8HTwlNgsz-wI2jfks5sXIrxgaJpZM4M4OK7
.


Ramon de Paula Marques


#24

From @jayair on Fri Aug 11 2017 17:25:14 GMT+0000 (UTC)

@ramonpm The other thing to check would be if you are returning the proper headers like so - https://github.com/AnomalyInnovations/serverless-stack-demo-api/blob/master/libs/response-lib.js#L13.


#25

From @ramonpm on Fri Aug 11 2017 17:28:07 GMT+0000 (UTC)

@jayair, thanks for your reply.
Unfortunately my response-lib.js is exactly the same as well…


#26

From @jayair on Sat Aug 12 2017 22:13:48 GMT+0000 (UTC)

@ramonpm Can I see a screenshot of the error that you are seeing in the browser console?


#27

From @jonsaw on Sun Aug 13 2017 13:27:01 GMT+0000 (UTC)

I’m having the same issue too. Screenshot attached:


#28

From @jayair on Sun Aug 13 2017 18:35:19 GMT+0000 (UTC)

@jonsaw Can you check the CloudWatch logs for the create notes Lambda?


#29

From @jonsaw on Mon Aug 14 2017 01:27:49 GMT+0000 (UTC)

@jayair no logs from yesterday and today.


#30

From @jayair on Mon Aug 14 2017 01:46:45 GMT+0000 (UTC)

@jonsaw Can I see your serverless.yml? And the IAM role attached to the Cognito Identity Pool? Here are some instructions on how to look it up - https://github.com/AnomalyInnovations/serverless-stack-com/issues/112#issuecomment-317204545.


#31

From @jonsaw on Mon Aug 14 2017 02:30:25 GMT+0000 (UTC)

serverless.yml

service: notes-api

# Use serverless-webpack plugin to transpile ES6/ES7
plugins:
  - serverless-webpack

# Enable auto-packing of external modules
custom:
  webpackIncludeModules: true

provider:
  name: aws
  runtime: nodejs6.10
  stage: prod
  region: ap-southeast-1

  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:ap-southeast-1:*:*"

functions:
  create:
    handler: create.main
    events:
      - http:
          path: notes
          method: post
          cors: true
          authorizer: aws_iam
  get:
    handler: get.main
    events:
      - http:
          path: notes/{id}
          method: get
          cors: true
          authorizer: aws_iam
  list:
    handler: list.main
    events:
      - http:
          path: notes
          method: get
          cors: true
          authorizer: aws_iam
  update:
    handler: update.main
    events:
      - http:
          path: notes/{id}
          method: put
          cors: true
          authorizer: aws_iam
  delete:
    handler: delete.main
    events:
      - http:
          path: notes/{id}
          method: delete
          cors: true
          authorizer: aws_iam

Cognito Identity Pool IAM role:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "mobileanalytics:PutEvents",
        "cognito-sync:*",
        "cognito-identity:*"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:*"
      ],
      "Resource": [
        "arn:aws:s3:::elasticbeanstalk-ap-southeast-1-999999999999/${cognito-identity.amazonaws.com:sub}*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "execute-api:Invoke"
      ],
      "Resource": [
        "arn:aws:execute-api:ap-southeast-1:*:xxxxxxxxxx/*"
      ]
    }
  ]
}

Thanks for the quick response @jayair .


#32

From @jayair on Mon Aug 14 2017 17:28:30 GMT+0000 (UTC)

@jonsaw All this looks good. And I’m assuming you got through the Test the APIs chapter?

If so, then can you host your backend code so I can take a look?


#33

From @jonsaw on Tue Aug 15 2017 00:43:13 GMT+0000 (UTC)

Yes, got OK for the test APIs chapter.

Sure, backed code hosted here.


#34

From @jayair on Tue Aug 15 2017 17:42:30 GMT+0000 (UTC)

@jonsaw Just tried your backend with my react app and the CORS part worked. Of course the request itself failed because I don’t have your DB.

Can you host your frontend code as well and I’ll take a look?


#35

From @jonsaw on Wed Aug 16 2017 00:09:00 GMT+0000 (UTC)

@jayair I’ll post it. But in the meantime, I was looking at the console and I noticed I got the following response headers:

content-length:358
content-type:application/json
date:Wed, 16 Aug 2017 00:05:04 GMT
status:403
via:1.1 f8f44b9f631a9dc3326cc7f64bcd4863.cloudfront.net (CloudFront)
x-amz-cf-id:CMEkFLWhGjdjYzTH3CLkgDdmsJjoURPbHh1o2NB2yThH0z_oLRzyjw==
x-amzn-errortype:IncompleteSignatureException
x-amzn-requestid:8e228d97-8216-11e7-b477-3b952b608cf8
x-cache:Error from cloudfront

#36

From @jayair on Wed Aug 16 2017 17:05:22 GMT+0000 (UTC)

@jonsaw Yeah that’s a good clue. It seems like the signature for the request was not calculated properly. Let’s take a look at the frontend.


#37

From @dailenspencer on Wed Aug 23 2017 23:26:13 GMT+0000 (UTC)

Hey guys,

Great tutorial! I’m running into an ERR_NAME_NOT_RESOLVED error when initiating the invokeApig call to create a new note. Screenshot here

Any ideas would be helpful! Thanks


#38

From @jayair on Thu Aug 24 2017 00:06:42 GMT+0000 (UTC)

@dailenspencer That sounds like you are not able to hit that URL from your local machine. Did the step where you tested the APIs work for you?


#39

From @dailenspencer on Thu Aug 24 2017 01:05:48 GMT+0000 (UTC)

@jayair Thanks for the quick response. Just got back to my keyboard. Here is a screenshot of the test - you will see the data comes back empty; Not sure why this is.

Update: seemed to narrow down the issue. The above terminal command was executing my list.js lambda function for some reason, hence the empty array returned. Inside my serverless.yml I had the following

list:
   # Defines an HTTP API endpoint that calls the main function in list.js
   # - path: url path is /notes
   # - method: GET request
   handler: list.main
   events:
     - http:
         path: notes
         method: post
         cors: true
         authorizer: aws_iam

the method property was set to post which made it identical to the create method. My lambda ended up being confused and called the list.js function instead of the create.js function.


#40

From @jayair on Thu Aug 24 2017 18:27:32 GMT+0000 (UTC)

@dailenspencer I just noticed in your original screenshot that you are trying to connect to the API endpoint that we have in the tutorial. Replace that with the one you are using in the apig-test command and give it a try.