sst.dev icon indicating copy to clipboard operation
sst.dev copied to clipboard

Comments: Deploy the APIs

Open jayair opened this issue 9 years ago • 122 comments

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

jayair avatar Apr 10 '17 01:04 jayair

I got {"message":"Unauthorized"} error.

I changed the OUR_COGNITO_USER_POOL_ID and YOUR_COGNITO_APP_CLIENT_ID and got the IdToken. I replaced the IdToken with mine. I also changed the endpoint to my own API gateway endpoint.

Any ideas?

liu-cong avatar Apr 12 '17 02:04 liu-cong

Could you post the functions section in your serverless.yml of the endpoint you are trying to invoke?

fwang avatar Apr 12 '17 18:04 fwang

I think it has the problem with your keys..Check whether u copied whole keys...

On 12-Apr-2017 7:36 AM, "Cong Liu" [email protected] wrote:

I got {"message":"Unauthorized"} error.

I changed the OUR_COGNITO_USER_POOL_ID and YOUR_COGNITO_APP_CLIENT_ID and got the IdToken. I replaced the IdToken with mine. I also changed the endpoint to my own API gateway endpoint.

Any ideas?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/AnomalyInnovations/serverless-stack-com/issues/28#issuecomment-293451509, or mute the thread https://github.com/notifications/unsubscribe-auth/AMRdwdflSnmW6BX0YLcqNs9lX500b4kHks5rvDGmgaJpZM4M4OEX .

JUSTPERFECT avatar Apr 12 '17 18:04 JUSTPERFECT

Below is the functions section in my serverless.yml. I doubled checked my keys and I don't think I made any copy/paste mistakes.

BTW, I can use the UI to create/update notes.

functions:
  # Defines an HTTP API endpoint that calls the main function in create.js
  # - path: url path is /notes
  # - method: POST request
  # - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
  #     domain api call
  # - authorizer: authenticate the api via Cognito User Pool. Update the 'arn'
  #     with your own User Pool ARN
  create:
    handler: create.main
    events:
      - http:
          path: notes
          method: post
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:us-east-1:099521359077:userpool/us-east-1_CuWTSHcJb

  get:
      # Defines an HTTP API endpoint that calls the main function in get.js
      # - path: url path is /notes/{id}
      # - method: GET request
      handler: get.main
      events:
        - http:
            path: notes/{id}
            method: get
            cors: true
            authorizer:
              arn: arn:aws:cognito-idp:us-east-1:099521359077:userpool/us-east-1_CuWTSHcJb

  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: get
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:us-east-1:099521359077:userpool/us-east-1_CuWTSHcJb

  update:
    # Defines an HTTP API endpoint that calls the main function in update.js
    # - path: url path is /notes/{id}
    # - method: PUT request
    handler: update.main
    events:
      - http:
          path: notes/{id}
          method: put
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:us-east-1:099521359077:userpool/us-east-1_CuWTSHcJb

  delete:
    # Defines an HTTP API endpoint that calls the main function in delete.js
    # - path: url path is /notes/{id}
    # - method: DELETE request
    handler: delete.main
    events:
      - http:
          path: notes/{id}
          method: delete
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:us-east-1:099521359077:userpool/us-east-1_CuWTSHcJb

liu-cong avatar Apr 13 '17 01:04 liu-cong

The React app (UI) calls the APIs the exact same way as the bash curl. If you inspect the HTTP request in the browser, you should see the IdToken used in the Authorization header and the JSON request body. Try curling with them.

fwang avatar Apr 13 '17 19:04 fwang

After running 'serverless deploy -v' I received the response below. (Unable to parse HTTP response content.) All API's passed their tests and now I'm suddenly stuck on this error. Any ideas?

david@DESKTOP-U1APQB8 MINGW64 /d/Projects/Serverless/notes-app-api
$ serverless deploy -v
Serverless: Bundling with Webpack...
Time: 3501ms
     Asset     Size  Chunks             Chunk Names
 create.js   5.8 kB       0  [emitted]  create
 delete.js  5.66 kB       1  [emitted]  delete
    get.js  5.88 kB       2  [emitted]  get
handler.js  2.28 kB       3  [emitted]  handler
   list.js   5.9 kB       4  [emitted]  list
 update.js   6.2 kB       5  [emitted]  update
Serverless: Packing external modules: babel-runtime@^6.23.0, uuid@^3.0.1
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading function .zip files to S3...
Serverless: Uploading service .zip file to S3 (1.03 MB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - notes-app-api-prod
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - ListLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - 2UnderscoreJLN0gaaY4LambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - UpdateLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - CreateLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - GetLogGroup
CloudFormation - CREATE_FAILED - AWS::Lambda::Permission - 2UnderscoreJLN0gaaY4LambdaPermissionApiGateway
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - ListLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - CreateLogGroup
CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - UpdateLogGroup
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - CreateLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - DeleteLogGroup
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - ListLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - GetLogGroup
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - UpdateLogGroup
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - GetLogGroup
CloudFormation - CREATE_FAILED - AWS::Logs::LogGroup - DeleteLogGroup
CloudFormation - CREATE_FAILED - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - UPDATE_ROLLBACK_IN_PROGRESS - AWS::CloudFormation::Stack - notes-app-api-prod
CloudFormation - UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - notes-app-api-prod
CloudFormation - DELETE_IN_PROGRESS - AWS::Logs::LogGroup - GetLogGroup
CloudFormation - DELETE_IN_PROGRESS - AWS::Logs::LogGroup - CreateLogGroup
CloudFormation - DELETE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - DELETE_IN_PROGRESS - AWS::Logs::LogGroup - UpdateLogGroup
CloudFormation - DELETE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - DELETE_IN_PROGRESS - AWS::Logs::LogGroup - ListLogGroup
CloudFormation - DELETE_COMPLETE - AWS::Lambda::Permission - 2UnderscoreJLN0gaaY4LambdaPermissionApiGateway
CloudFormation - DELETE_COMPLETE - AWS::Logs::LogGroup - GetLogGroup
CloudFormation - DELETE_COMPLETE - AWS::Logs::LogGroup - DeleteLogGroup
CloudFormation - DELETE_COMPLETE - AWS::Logs::LogGroup - CreateLogGroup
CloudFormation - DELETE_COMPLETE - AWS::ApiGateway::RestApi - ApiGatewayRestApi
CloudFormation - DELETE_COMPLETE - AWS::Logs::LogGroup - UpdateLogGroup
CloudFormation - DELETE_COMPLETE - AWS::Logs::LogGroup - ListLogGroup
CloudFormation - DELETE_COMPLETE - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - UPDATE_ROLLBACK_COMPLETE - AWS::CloudFormation::Stack - notes-app-api-prod
Serverless: Deployment failed!

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

     An error occurred while provisioning your stack: 2UnderscoreJLN0gaaY4LambdaPermissionApiGateway
     - Unable to parse HTTP response content.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                 win32
     Node Version:       7.2.0
     Serverless Version: 1.11.0

dmurraykauai avatar Apr 18 '17 00:04 dmurraykauai

I did receive the same Error as above once deployed. On Stack they saying we must provide statusCode along with response...as we did in tutorial..but perhaps we must add it on API Gateway level also...i dont know at the moment...trying to fix it http://stackoverflow.com/questions/37137601/unable-to-parse-http-response-content-when-creating-apigatewayresource

PatrykTies avatar Apr 18 '17 12:04 PatrykTies

@dmurraykauai @PatrykTies A couple of things could cause the "Unable to parse HTTP response content" error.

Let's do a quick sanity check. Could you share your serverless.yml file?

fwang avatar Apr 18 '17 17:04 fwang

service: notes-app 

plugins:
  - serverless-webpack

custom:
  webpackIncludeModules: true


provider:
  name: aws
  runtime: nodejs6.10
  stage: prod
  region: eu-west-1


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

functions:

  create:
    handler: create.main
    events:
      - http:
          path: notes
          method: post
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:eu-west-1:421235352293:userpool/eu-west-1_xxxxxxx
  get:
    
    handler: get.main
    events:
      - http:
          path: notes/{id}
          method: get
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:eu-west-1:421235352293:userpool/eu-west-1_xxxxxxx
  list:
    
    handler: list.main
    events:
      - http:
          path: notes
          method: get
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:eu-west-1:421235352293:userpool/eu-west-1_xxxxxxxx
  update:
  
    handler: update.main
    events:
      - http:
          path: notes/{id}
          method: put
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:eu-west-1:421235352293:userpool/eu-west-1_xxxxxxxx
  delete:
  
    handler: delete.main
    events:
      - http:
          path: notes/{id}
          method: delete
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:eu-west-1:421235352293:userpool/eu-west-1_xxxxxxxx

PatrykTies avatar Apr 18 '17 18:04 PatrykTies

Pasting the code in between the back ticks doesn't render too good. I've attached serverless.yml.txt to this post. Thanks for the assist!


service: notes-app-api

plugins:
  - serverless-webpack

custom:
  webpackIncludeModules: true
[serverless.yml.txt](https://github.com/AnomalyInnovations/serverless-stack-com/files/930746/serverless.yml.txt)

provider:
  name: aws
  runtime: nodejs6.10
  stage: prod
  region: us-west-2

  # 'iamRoleStatement' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:us-west-2:*:*"

functions:
  # Defines an HTTP API endpoint that calls the main function in create.js
  # - path: url path is /notes
  # - method: POST request
  # - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
  #     domain api call
  # - authorizer: authenticate the api via Cognito User Pool. Update the 'arn'
  #     with your own User Pool ARN
  create:
    handler: create.main
    events:
      - http:
          path: notes
          method: post
          cors: true
          authorizer:
            arn: aws:cognito-idp:us-west-2:939466294100:userpool/us-west-2_JLN0gaaY4
  get:
      # Defines an HTTP API endpoint that calls the main function in get.js
      # - path: url path is /notes/{id}
      # - method: GET request
      handler: get.main
      events:
        - http:
            path: notes/{id}
            method: get
            cors: true
            authorizer:
              arn: aws:cognito-idp:us-west-2:939466294100:userpool/us-west-2_JLN0gaaY4
  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: get
            cors: true
            authorizer:
              arn: aws:cognito-idp:us-west-2:939466294100:userpool/us-west-2_JLN0gaaY4
  update:
      # Defines an HTTP API endpoint that calls the main function in update.js
      # - path: url path is /notes/{id}
      # - method: PUT request
      handler: update.main
      events:
        - http:
            path: notes/{id}
            method: put
            cors: true
            authorizer:
              arn: aws:cognito-idp:us-west-2:939466294100:userpool/us-west-2_JLN0gaaY4
  delete:
      # Defines an HTTP API endpoint that calls the main function in delete.js
      # - path: url path is /notes/{id}
      # - method: DELETE request
      handler: delete.main
      events:
        - http:
            path: notes/{id}
            method: delete
            cors: true
            authorizer:
              arn: aws:cognito-idp:us-west-2:939466294100:userpool/us-west-2_JLN0gaaY4

dmurraykauai avatar Apr 18 '17 18:04 dmurraykauai

@dmurraykauai the authorizer arn should start with arn: and the authorizer line should look something like

authorizer:
  arn: arn:aws:cognito-idp:us-east-1:xxxxxxxxxxxx:userpool/us-east-1_XXXXXxxxx

fwang avatar Apr 18 '17 18:04 fwang

ok, that was a bit confusing... arn: arn:aws: .... thought I tried w/ the double arn: already but let me try again. Thank you!

dmurraykauai avatar Apr 18 '17 19:04 dmurraykauai

Beautiful, works fine now. Thanks again @fwang ! Much appreciated.

dmurraykauai avatar Apr 18 '17 19:04 dmurraykauai

@PatrykTies hmmm.. it looks fine. Could you double check a few things:

  • both Identity pool and User pool are created inside eu-west-1 region;
  • userpool id is usually 9 characters long, ensure the authorizer arn is not missing any character at the end arn: arn:aws:cognito-idp:eu-west-1:421235352293:userpool/eu-west-1_xxxxxxx

fwang avatar Apr 18 '17 19:04 fwang

Hi,

Thanks for your super guide :sparkles:

I'm struggling with cognito and serverless, I'm having the same problem as @dmurraykauai I guess.

I'm declaring my function as follows:

searchname:
  handler: handler.searchname
  events:
    - http:
        path: search/name/{value}
        method: get
        private: true
        cors: true
        authorizer:
          arn: ${self:custom.poolArn}
    - stream: ${self:custom.dynamoArn} 

All my services are running on ap-northeast-2 and my poolArn variable is equal to arn:aws:cognito-idp:ap-northeast-2:XXXXXXXXXXXX:userpool/ap-northeast-2_xxxxxxx.

When I try to deploy my service, I get the following logs:

CloudFormation - CREATE_FAILED - AWS::Lambda::Permission - 2UnderscorexxxxxxxLambdaPermissionApiGateway
CloudFormation - CREATE_FAILED - AWS::ApiGateway::Authorizer - 2UnderscorexxxxxxxApiGatewayAuthorizer

Depending on AWS settings (e.g., switching Authorization to AWS_IAM), the process ends with one of these error messages:

- Unable to parse HTTP response content.
- Invalid lambda function.

Everything works fine if I comment the authorizer lines.

I've set manually IAM permissions for lambda:

{
    "Action": [
        "lambda:*"
    ],
    "Resource": "*",
    "Effect": "Allow"
},

I also had a look at many related issues, I saw you answered one on serverless forums. However, their official doc is really light regarding cognito as an authorizer... Note that I'm just using Cognito user pool, without Cognito identity pool.

How did you get your example working? Are there extra settings to deal with on the dashboard?

Thanks


~~Edit 1: I found a solution that solves the following alert:~~

CloudFormation - CREATE_FAILED - AWS::ApiGateway::Authorizer - 2UnderscorexxxxxxxApiGatewayAuthorizer

jtrollia avatar Apr 19 '17 09:04 jtrollia

Yo,

Never mind, I was using Serverless 1.4.0, I had a look at changelogs and figured out I should update to the latest version.

jtrollia avatar Apr 20 '17 04:04 jtrollia

@Malivuk thanks for reporting back. It'll help anybody else that might run into this.

jayair avatar Apr 20 '17 18:04 jayair

Yes I can confirm, my issue above was also caused by using 1.4 , now once updated to 1.11 all works fine. Cheers.

PatrykTies avatar Apr 20 '17 20:04 PatrykTies

@Malivuk couldn't have timed your response any more perfectly 😉

Or maybe @PatrykTies had it figured out before he saw your message.

jayair avatar Apr 20 '17 22:04 jayair

Hello,

I couldn't get to the actual deploying. I followed the tutorial by the book. Got this error:

Serverless: Bundling with Webpack...
Time: 771ms
    Asset     Size  Chunks             Chunk Names
create.js  5.75 kB       0  [emitted]  create
delete.js  5.41 kB       1  [emitted]  delete
   get.js   5.5 kB       2  [emitted]  get
  list.js  5.44 kB       3  [emitted]  list
update.js  5.78 kB       4  [emitted]  update
Serverless: Packing external modules: babel-runtime@^6.23.0, uuid@^3.0.1
Serverless: Packaging service...

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

TypeError: Cannot read property 'path' of null
    at AwsCompileApigEvents.getHttpPath (/usr/local/lib/node_modules/serverless/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js:145:20)
    at _.forEach (/usr/local/lib/node_modules/serverless/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js:46:28)
    at arrayEach (/usr/local/lib/node_modules/serverless/node_modules/lodash/lodash.js:537:11)
    at Function.forEach (/usr/local/lib/node_modules/serverless/node_modules/lodash/lodash.js:9344:14)
    at _.forEach (/usr/local/lib/node_modules/serverless/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js:42:9)
    at /usr/local/lib/node_modules/serverless/node_modules/lodash/lodash.js:4970:15
    at baseForOwn (/usr/local/lib/node_modules/serverless/node_modules/lodash/lodash.js:3020:24)
    at /usr/local/lib/node_modules/serverless/node_modules/lodash/lodash.js:4939:18
    at Function.forEach (/usr/local/lib/node_modules/serverless/node_modules/lodash/lodash.js:9344:14)
    at AwsCompileApigEvents.validate (/usr/local/lib/node_modules/serverless/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js:41:7)
    at Object.deploy:compileEvents [as fn] (/usr/local/lib/node_modules/serverless/lib/plugins/aws/deploy/compile/events/apiGateway/index.js:42:31)
    at BbPromise.reduce (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:210:55)
From previous event:
    at PluginManager.invoke (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:210:22)
    at PluginManager.run (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:225:17)
    at Serverless.run (/usr/local/lib/node_modules/serverless/lib/Serverless.js:97:31)
    at serverless.init.then (/usr/local/lib/node_modules/serverless/bin/serverless:23:50)

  Your Environment Information -----------------------------
     OS:                 darwin
     Node Version:       7.9.0
     Serverless Version: 1.11.0

I tried using older versions of aws-sdk and serverless and got the same error. I compared my codebase to the one of the examples and it's exactly the same.

Anyone been through this?

ericorruption avatar Apr 21 '17 09:04 ericorruption