Repository: serverless/serverless (JavaScript) — Bug Fix

Issue:
Serverless 1.26 doesn't set default API Gateway response template
<!--
1. If you have a question and not a bug/feature request please ask it at http://forum.serverless.com
2. Please check if an issue already exists so there are no duplicates
3. Check out and follow our Guidelines: https://github.com/serverless/serverless/blob/master/CONTRIBUTING.md
4. Fill out the whole template so we have a good overview on the issue
5. Do not remove any section of the template. If something is not applicable leave it empty but leave it in the Issue
6. Please follow the template, otherwise we'll have to ask you to update it
-->

# This is a Bug Report

## Description
After upgrading from Serverless 1.25.0 to Serverless 1.26.0, response templates are not set anymore in API Gateway.
* Here is a deployment made with 1.25.0: https://postimg.org/image/r6530fb1n/
* Here is the very same deployment made with 1.26.0: https://postimg.org/image/q3uwhvzy3/

You'll notice the difference in the bottom half: no _Integration Response_ and no _Method Response_.
I believe this is a bug because I found no mention of this change in the changelog.

* What went wrong?
 _Integration Response_ and _Method Response_ are not set. So when I invoke an endpoint, I get an error message "Internal server error", which in the API Gateway logs corresponds to "Execution failed due to configuration error: No match for output mapping and no default output mapping configured"

* What did you expect should have happened?
 _Integration Response_ and _Method Response_ should be set with sensible default values, like in Serverless 1.25.0

* What was the config you used?
Here it is, I removed most functions for the sake of briefness:
```
service: apiBO

# https://serverless.com/framework/docs/providers/aws/guide/version/
frameworkVersion: "^1.25.0"

# documentation about getting variables from CLI, file, etc.: https://github.com/serverless/serverless/pull/1834
custom:
  deployStage: ${opt:stage, self:provider.stage}
  envVarCommon: ${file(config/env-variables-common.json)}
  writeEnvVars:
    SERVERLESS_PROJECT_NAME: ${self:custom.envVarCommon.projectName}
    SERVERLESS_REGION: ${self:provider.region}
    SERVERLESS_STAGE: ${self:custom.deployStage}
    SERVERLESS_TABLE_NAME_PREFIX: [removed]-api-${self:custom.deployStage}-
    SERVERLESS_MANDRILL_KEY: ${self:custom.envVarCommon.mandrillKey}
    SERVERLESS_MANDRILL_KEY_TEST: ${self:custom.envVarCommon.mandrillKeyTest}

plugins:
  - serverless-plugin-write-env-vars

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: us-east-1
  memorySize: 128
  timeout: 30
  cfLogs: true

  iamRoleStatements:
    # IamPolicyDynamoDB
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:DeleteItem
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:BatchGetItem
        - dynamodb:BatchWriteItem
      Resource: arn:aws:dynamodb:us-east-1:[removed]:table/[removed]-api-${self:custom.deployStage}-*
    # IamPolicyDynamoDBStreams
    - Effect: Allow
      Action:
        - dynamodb:DescribeStream
        - dynamodb:GetRecords
        - dynamodb:GetShardIterator
        - dynamodb:ListStreams
      Resource: arn:aws:dynamodb:us-east-1:[removed]:table/[removed]-api-${self:custom.deployStage}-*
    # IamPolicyCognitoUnAuth
    - Effect: Allow
      Action:
        - "mobileanalytics:PutEvents"
        - "cognito-sync:*"
      Resource: "*"
    # IamPolicyCognitoAuth
    - Effect: Allow
      Action:
        - execute-api:Invoke
        - lambda:InvokeFunction
      Resource: "*"
    # To access S3 buckets
    - Effect: Allow
      Action:
        - "s3:GetObject"
        - "s3:PutObject"
      Resource: "*"

package:
  excludeDevDependencies: true
  exclude:
    - .npmignore
    - config/**
    - dts/**
    - package.json
    - package-lock.json
    - readme.md
    - slslocal.sh
    - tsconfig.json
    - events/**
    - '*.bat'
    - '*.ts'
    - '**/*.ts'
    - node_modules/.bin/**

functions:
  account:
    memorySize: 512
    timeout: 10
    handler: haccount.handler
    name: ${self:custom.envVarCommon.projectName}-${self:custom.deployStage}-account
    description: BO account things (login, logout...)
    tags:
      api: BO
    events:
      - http:
          path: login
          method: post
          integration: lambda
          cors:
            origins:
              - '*'
          request:
            template:
              application/json: ${file(config/apiGatewayRequestTemplate.txt)}
            passThrough: WHEN_NO_MATCH
            parameters:
              query:
                body: true
      - http:
          path: logout
          method: post
          integration: lambda
          cors:
            origins:
              - '*'
            headers:
              - sessionToken
              - Content-Type
          request:
            template:
              application/json: ${file(config/apiGatewayRequestTemplate.txt)}
            passThrough: WHEN_NO_MATCH
      - http:
          path: account/ping
          method: get
          integration: lambda
          request:
            template:
              application/json: ${file(config/apiGatewayRequestTemplate.txt)}
            passThrough: WHEN_NO_MATCH
      - http:
          path: mirror
          method: post
          integration: lambda
          cors:
            origins:
              - '*'
            headers:
              - sessionToken
              - Content-Type
          request:
            template:
              application/json: ${file(config/apiGatewayRequestTemplate.txt)}
            passThrough: WHEN_NO_MATCH

resources:
  Resources:
    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: ${self:service}
        Description: API back-office (${self:custom.deployStage} stage)
```

* What stacktrace or error message from your provider did you see?
No particular error, here is the deployment log with `SLS_DEBUG=*`:
```
$ sls deploy --aws-s3-accelerate                                                                                                       
Serverless: Load command run                                                                                                           
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 emit                                                                                                          
Serverless: Load command config                                                                                                        
Serverless: Load command config:credentials                                                                                            
Serverless: Load command rollback                                                                                                      
Serverless: Load command rollback:function                                                                                             
Serverless: Invoke deploy                                                                                                              
Serverless: Invoke package                                                                                                             
Serverless: Invoke aws:common:validate                                                                                                 
Serverless: Invoke aws:common:cleanupTempDir                                                                                           
Serverless: Wrote .env file to [path]\[removed]\API\apiBO\.env                                                                             
Serverless: Packaging service...                                                                                                       
Serverless: Excluding development dependencies...                                                                                      
Serverless: Deleted .env file from [path]\[removed]\API\apiBO\.env                                                                         
Serverless: Invoke aws:package:finalize                                                                                                
Serverless: Invoke aws:common:moveArtifactsToPackage                                                                                   
Serverless: Invoke aws:common:validate                                                                                                 
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: WARNING: Function [removed] has timeout of 60 seconds, however, it's attached to API Gateway so it's automatically limited to 
30 seconds.                                                                                                                            
Serverless: Invoke aws:deploy:deploy                                                                                                   
Serverless: Uploading CloudFormation file to S3...                                                                                     
Serverless: Using S3 Transfer Acceleration Endpoint...                                                                                 
Serverless: Uploading artifacts...                                                                                                     
Serverless: Uploading service .zip file to S3 (318.64 KB)...                                                                           
Serverless: Using S3 Transfer Acceleration Endpoint...                                                                                 
Serverless: Validating template...                                                                                                     
Serverless: Updating Stack...                                                                                                          
Serverless: Checking Stack update progress...                                                                                          
..................                                                                                                                     
Serverless: Stack update finished...                                                                                                   
Serverless: Invoke aws:info                                                                                                            
Service Information                                                                                                                    
service: apiBO                                                                                                                         
stage: dev                                                                                                                             
region: us-east-1                                                                                                                      
stack: apiBO-dev                                                                                                                       
api keys:                                                                                                                              
  None                                                                                                                                 
endpoints:                                                                                                                             
  POST - https://[removed].execute-api.us-east-1.amazonaws.com/dev/login                                                              
  POST - https://[removed].execute-api.us-east-1.amazonaws.com/dev/logout                                           
  GET - https://[removed].execute-api.us-east-1.amazonaws.com/dev/account/ping       
  [I removed the rest]  
functions:                                                                                                                
  [removed]: apiBO-dev-[removed]       
  [I removed the rest]    
Serverless: Invoke aws:deploy:finalize                                                                                                 
Serverless: Removing old service versions...                                                                                           
```

Similar or dependent issues:
I haven't found any

## Additional Data

* ***Serverless Framework Version you're using***: 1.26.0 (works with 1.25.0)
* ***Operating System***: Windows 7/10 x64, Node 8.9.4 LTS
* ***Stack Trace***: cf above
* ***Provider Error messages***: as mentioned above: "Execution failed due to configuration error: No match for output mapping and no default output mapping configured"



Discussion:
I had this problem as well.
I faced this problem too. 
This issue may not caused by serverless. I tried to downgrade serverless to 1.25.0, 1.24.1, 1.23.0. both not working.
Below is the deploy logs:
``` log
Serverless: Load command run
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 emit
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command offline
Serverless: Load command offline:start
Serverless: Invoke deploy
Serverless: Invoke package
Serverless: Invoke aws:common:validate
Serverless: Invoke aws:common:cleanupTempDir
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Invoke aws:package:finalize
Serverless: Invoke aws:common:moveArtifactsToPackage
Serverless: Invoke aws:common:validate
Serverless: Invoke aws:deploy:deploy
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (38.28 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
......................................................................................................................................................
Serverless: Stack update finished...
Serverless: Invoke aws:info
Service Information
service: vehicle-api-V1-1-24-1-second
stage: test
region: ap-southeast-2
stack: vehicle-api-V1-1-24-1-second-test
api keys:
  vehicle-api-V1-1-24-1-second-test-token: am0Dal3Tzq5J5MCVIzck91gOfRZGK4Is3GaK2ema
endpoints:
  PUT - https://iv19diaxob.execute-api.ap-southeast-2.amazonaws.com/test/V1-1-24-1-second/vehicle/updateContact
  GET - https://iv19diaxob.execute-api.ap-southeast-2.amazonaws.com/test/V1-1-24-1-second/vehicle/types
  GET - https://iv19diaxob.execute-api.ap-southeast-2.amazonaws.com/test/V1-1-24-1-second/vehicle/types/{type}/makes
  GET - https://iv19diaxob.execute-api.ap-southeast-2.amazonaws.com/test/V1-1-24-1-second/vehicle/types/{type}/makes/{make}/models
  GET - https://iv19diaxob.execute-api.ap-southeast-2.amazonaws.com/test/V1-1-24-1-second/vehicle/types/{type}/makes/{make}/models/{model}/years
  GET - https://iv19diaxob.execute-api.ap-southeast-2.amazonaws.com/test/V1-1-24-1-second/vehicle/types/{type}/makes/{make}/models/{model}/years/{year}/colors
functions:
  updateContact: vehicle-api-V1-1-24-1-second-test-updateContact
  getTypes: vehicle-api-V1-1-24-1-second-test-getTypes
  getMakes: vehicle-api-V1-1-24-1-second-test-getMakes
  getModels: vehicle-api-V1-1-24-1-second-test-getModels
  getYears: vehicle-api-V1-1-24-1-second-test-getYears
  getColors: vehicle-api-V1-1-24-1-second-test-getColors
Serverless: Invoke aws:deploy:finalize
```
Below is  the serverless.yml:
``` yaml
service: vehicle-api-${self:custom.version}

provider:
  name: aws
  runtime: nodejs6.10
  region: ap-southeast-2
  memorySize: 128
  timeout: 15
  environment:
    NODE_ENV: ${opt:stage}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - ec2:CreateNetworkInterface
        - ec2:DescribeNetworkInterfaces
        - ec2:DeleteNetworkInterface
      Resource: "*"
  vpc:
      securityGroupIds:
        - Ref: vehicleApiSecurityGroup
      subnetIds:
        "Fn::Split":
          - ","
          - ${opt:subnetId}
  apiKeys:
    - ${self:service}-${opt:stage}-token
resources:
  Resources:
    vehicleApiSecurityGroup:
      Type: AWS::EC2::SecurityGroup
      Properties:
        GroupDescription: Security group which allows access on ports 80 but only to machines of this security group.
        VpcId: ${opt:vpcId}
        SecurityGroupIngress:
          -
            IpProtocol: TCP
            CidrIp: 0.0.0.0/0
            FromPort: 80
            ToPort: 80

package:
 exclude:
   - .gitignore
   - package.json
   - test/**
   - developmentServer.js
   - .git
   - .vscode/
   - node_modules/.bin/**

custom:
  version: V1
  serviceName:  ${self:service} 

functions:
  updateContact:
    handler: handler.updateContact
    events:
      - http:
          path: /${self:custom.version}/vehicle/updateContact
          method: put
          integration: lambda
          response:
            headers:
              Content-Type: "'application/JSON'"
  getTypes:
    handler: handler.search
    events:
      - http:
          path: /${self:custom.version}/vehicle/types
          method: get
          private: true
          integration: lambda
  getMakes:
    handler: handler.search
    events:
      - http:
          path: /${self:custom.version}/vehicle/types/{type}/makes
          method: get
          private: true
          integration: lambda
  getModels:
    handler: handler.search
    events:
      - http:
          path: /${self:custom.version}/vehicle/types/{type}/makes/{make}/models/
          method: get
          private: true
          integration: lambda
  getYears:
    handler: handler.search
    events:
      - http:
          path: /${self:custom.version}/vehicle/types/{type}/makes/{make}/models/{model}/years
          method: get
          private: true
          integration: lambda
  getColors:
    handler: handler.search
    events:
      - http:
          path: /${self:custom.version}/vehicle/types/{type}/makes/{make}/models/{model}/years/{year}/colors
          method: get
          private: true
          integration: lambda

plugins:
  - serverless-offline

```
Downgrading to 1.25.0 worked for me, but I had to `sls remove` the old version first
@philharton 
I tried to use the authorization mechanism with AWS Cognito and this same bug occurred.
You definitely do not have to `sls remove`. The following steps should suffice if you have serverless installed globally:
```
npm uninstall -g serverless
npm install -g serverless@1.25.0
```
Then run your deploy. I've tested this in a staging environment. 
@ztmdsbt can you ensure that the global version of serverless is being downgraded?
What does this output?
```
serverless -v
```
I had the same problem and just downgrading serverless resolved it.  I didn't have to do `sls remove`
@lucasklaassen  you are right, I forgot remove global installation. after uninstall and install 1.25.0 the issue has been fixed.

Resolving patch:
diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js
index bbafbb75163..936931cdbe8 100644
--- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js
+++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js
@@ -78,6 +78,11 @@ module.exports = {
             http.request = this.getRequest(http);
             http.request.passThrough = this.getRequestPassThrough(http);
             http.response = this.getResponse(http);
+            if (http.integration === 'AWS' && _.isEmpty(http.response)) {
+              http.response = {
+                statusCodes: DEFAULT_STATUS_CODES,
+              };
+            }
           } else if (http.integration === 'AWS_PROXY' || http.integration === 'HTTP_PROXY') {
             // show a warning when request / response config is used with AWS_PROXY (LAMBDA-PROXY)
             if (http.request) {


Test patch:
diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js
index a2132f048e0..4e481c9d8b5 100644
--- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js
+++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js
@@ -1728,4 +1728,54 @@ describe('#validate()', () => {
     expect(validated.events).to.be.an('Array').with.length(1);
     expect(validated.events[0].http.request.passThrough).to.equal(undefined);
   });
+
+  it('should set default statusCodes to response for lambda by default', () => {
+    awsCompileApigEvents.serverless.service.functions = {
+      first: {
+        events: [
+          {
+            http: {
+              method: 'GET',
+              path: 'users/list',
+              integration: 'lambda',
+              integrationMethod: 'GET',
+            },
+          },
+        ],
+      },
+    };
+
+    const validated = awsCompileApigEvents.validate();
+    expect(validated.events).to.be.an('Array').with.length(1);
+    expect(validated.events[0].http.response.statusCodes).to.deep.equal({
+      200: {
+        pattern: '',
+      },
+      400: {
+        pattern: '[\\s\\S]*\\[400\\][\\s\\S]*',
+      },
+      401: {
+        pattern: '[\\s\\S]*\\[401\\][\\s\\S]*',
+      },
+      403: {
+        pattern: '[\\s\\S]*\\[403\\][\\s\\S]*',
+      },
+      404: {
+        pattern: '[\\s\\S]*\\[404\\][\\s\\S]*',
+      },
+      422: {
+        pattern: '[\\s\\S]*\\[422\\][\\s\\S]*',
+      },
+      500: {
+        pattern:
+        '[\\s\\S]*(Process\\s?exited\\s?before\\s?completing\\s?request|\\[500\\])[\\s\\S]*',
+      },
+      502: {
+        pattern: '[\\s\\S]*\\[502\\][\\s\\S]*',
+      },
+      504: {
+        pattern: '([\\s\\S]*\\[504\\][\\s\\S]*)|(^[Task timed out].*)',
+      },
+    });
+  });
 });