Repository: serverless/serverless (JavaScript) — Bug Fix

Issue:
Unable to create Authorizer using Object notation in serverless.yml
Serverless Framework Version: 1.0.0-beta.2

Operating System: Mac OS X 10.11.4

Expected Behavior: API Gateway Authorizer created

Actual Behavior: API Gateway Authorizer fails to create, with the following message:

```
  Serverless Error ---------------------------------------

     An error occurred while provisioning your cloudformation:
     The following resource(s) failed to update: [authorizeMessagingUserAuthorizer].


  Get Support --------------------------------------------
     Docs:          v1.docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
```

Description: When I attempt to use the Object notation for defining an Authorizer, Serverless fails to create the API Gateway Authorizer. The documented example that I am using is here: https://github.com/serverless/serverless/blame/85f4084e6b0fd4a6d763ace8cd0db82817bbc712/lib/plugins/aws/deploy/compile/events/apiGateway/README.md#L85

My `serverless.yml` file looks like this:

```

service: my-serverless-service

provider:
  name: aws
  runtime: nodejs4.3

functions:
  authorizeMessagingUser:
    handler: functions/authorizeMessagingUser.handler
    memorySize: 256
  sendMessage:
    handler: functions/sendMessage.handler
    memorySize: 256
    events:
      - http:
          path: applications/{applicationId}/messages
          method: post
          authorizer:
            name: authorizeMessagingUser
            identitySource: method.request.header.Authorization
  getMessage:
    handler: functions/getMessage.handler
    memorySize: 256
    events:
      - http:
          path: applications/{applicationId}/messages
          method: get
          authorizer:
            name: authorizeMessagingUser
            identitySource: method.request.header.Authorization
```

If I change one of the events to look like:

```
  sendMessage:
    handler: functions/sendMessage.handler
    memorySize: 256
    events:
      - http:
          path: applications/{applicationId}/messages
          method: post
          authorizer: authorizeMessagingUser
```

Then everything works just fine. It seems like something different is happening behind the scenes when I use the object notation.



Discussion:

Resolving patch:
diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js
index 19e14424139..b2c2f0a4bf4 100644
--- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js
+++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js
@@ -26,7 +26,7 @@ module.exports = {
                 .length - 1].split('-');
               authorizerName = splittedLambdaName[splittedLambdaName.length - 1];
             }
-            resultTtlInSeconds = '300';
+            resultTtlInSeconds = 300;
             identitySource = 'method.request.header.Auth';
           } else if (typeof authorizer === 'object') {
             if (authorizer.arn) {
@@ -43,7 +43,7 @@ module.exports = {
               throw new this.serverless.classes
                 .Error('Please provide either an authorizer name or ARN');
             }
-            resultTtlInSeconds = String(authorizer.resultTtlInSeconds) || '300';
+            resultTtlInSeconds = Number.parseInt(authorizer.resultTtlInSeconds, 10) || 300;
             identitySource = authorizer.identitySource || 'method.request.header.Auth';
           } else {
             const errorMessage = [
@@ -60,7 +60,7 @@ module.exports = {
             {
               "Type" : "AWS::ApiGateway::Authorizer",
               "Properties" : {
-                "AuthorizerResultTtlInSeconds" : "${resultTtlInSeconds}",
+                "AuthorizerResultTtlInSeconds" : ${resultTtlInSeconds},
                 "AuthorizerUri" : {"Fn::Join" : ["", [
                   "arn:aws:apigateway:",
                   {"Ref" : "AWS::Region"},


Test patch:
diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/authorizers.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/authorizers.js
index 42066dd60d9..d921e934927 100644
--- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/authorizers.js
+++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/authorizers.js
@@ -66,7 +66,7 @@ describe('#compileAuthorizers()', () => {
       expect(
         awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
           .Resources.authorizerAuthorizer.Properties.AuthorizerResultTtlInSeconds
-      ).to.equal('300');
+      ).to.equal(300);
 
       expect(
         awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
@@ -108,7 +108,7 @@ describe('#compileAuthorizers()', () => {
   it('should create authorizer with the given config object', () => {
     awsCompileApigEvents.serverless.service.functions.first.events[0].http.authorizer = {
       name: 'authorizer',
-      resultTtlInSeconds: '400',
+      resultTtlInSeconds: 400,
       identitySource: 'method.request.header.Custom',
       identityValidationExpression: 'regex',
     };
@@ -117,7 +117,7 @@ describe('#compileAuthorizers()', () => {
       expect(
         awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
           .Resources.authorizerAuthorizer.Properties.AuthorizerResultTtlInSeconds
-      ).to.equal('400');
+      ).to.equal(400);
 
       expect(
         awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
@@ -137,7 +137,7 @@ describe('#compileAuthorizers()', () => {
   it('should create authorizer with the given config object with ARN', () => {
     awsCompileApigEvents.serverless.service.functions.first.events[0].http.authorizer = {
       arn: 'sss:dev-authorizer',
-      resultTtlInSeconds: '400',
+      resultTtlInSeconds: 400,
       identitySource: 'method.request.header.Custom',
       identityValidationExpression: 'regex',
     };
@@ -146,7 +146,7 @@ describe('#compileAuthorizers()', () => {
       expect(
         awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate
           .Resources.authorizerAuthorizer.Properties.AuthorizerResultTtlInSeconds
-      ).to.equal('400');
+      ).to.equal(400);
 
       expect(
         awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate