Repository: serverless/serverless (JavaScript) — Bug Fix

Issue:
Cannot activate API gateway logs for shared API gateway 
# Bug Report

## Description
The API gateway log settings are not applied when using a shared API gateway as described [here](https://serverless.com/framework/docs/providers/aws/events/apigateway#easiest-and-cicd-friendly-example-of-using-shared-api-gateway-and-api-resources).

1. What did you do?
- Created `serverless.yml` my copying the example from the [docs](https://serverless.com/framework/docs/providers/aws/events/apigateway#easiest-and-cicd-friendly-example-of-using-shared-api-gateway-and-api-resources) and added 
```
logs:
    restApi: true
```
- Ran `sls deploy`
1. What happened?
- API Gateway logs are not enabled.
1. What should've happened?
- API Gateway logs should have been enabled.
1. What's the content of your `serverless.yml` file?
```
service: my-api
provider:
  name: aws
  runtime: nodejs12.x
  region: eu-central-1
  logs:
    restApi: true
resources:
  Resources:
    MyApiGW:
      Type: 'AWS::ApiGateway::RestApi'
      Properties:
        Name: MyApiGW
  Outputs:
    apiGatewayRestApiId:
      Value:
        Ref: MyApiGW
      Export:
        Name: MyApiGateway-restApiId
    apiGatewayRestApiRootResourceId:
      Value:
        'Fn::GetAtt':
        - MyApiGW
        - RootResourceId
      Export:
        Name: MyApiGateway-rootResourceId
```
1. What's the output you get when you use the `SLS_DEBUG=*` environment variable (e.g. `SLS_DEBUG=* serverless deploy`)
No difference. However, if I add a http function I can see that the AWS CLI is used to set the log settings. If no http function is defined, [updateStage.js](https://github.com/serverless/serverless/blob/master/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.js) is not called.
I tried to remove [this](https://github.com/serverless/serverless/blob/cd93739393c236be1bec61c4e51e1f848005bb41/lib/plugins/aws/package/compile/events/apiGateway/index.js#L78) line, but then I get
```
Serverless: [AWS apigateway 200 0.202s 0 retries] getRestApis({ position: undefined, limit: 500 })
 
  Serverless Error ---------------------------------------
 
  ServerlessError: Rest API id could not be resolved.
  This might be caused by a custom API Gateway configuration.
  
  In given setup stage specific options such as `tracing`, `logs` and `tags` are not supported.
```
I guess as a workaround I can define my own resources. However, I would gladly submit a PR to allow setting this with the log level properties.


Similar or dependent issues:

- There are several issues on API gateway logs but I did not find the exact same one.



Discussion:
Will be fixed with: https://github.com/serverless/serverless/pull/7034

Note, that for shared APIGW, logs need to be configured in place in which APIGW is configured (and not in services which refer to it)

> 
> Note, that for shared APIGW, logs need to be configured in place in which APIGW is configured (and not in services which refer to it)

Yes, that’s what I was trying, see the example above.

> Yes, that’s what I was trying, see the example above.

Sorry (I was fast reading) indeed it's a different case, and for your case, bug is here: https://github.com/serverless/serverless/blob/0b3f0213e7149456ccfa56ea1b13de34d362fc4c/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.js#L101

Let me prepare PR


Hi @medikoo , thanks for supplying this fix so soon. However, it's still not working.

There is still the issue, that [updateStage.js](https://github.com/serverless/serverless/blob/master/lib/plugins/aws/package/compile/events/apiGateway/lib/hack/updateStage.js) is not called unless I remove [this](https://github.com/serverless/serverless/blob/be8e64c9869c48a57a62f9facab6dcce4fb5582b/lib/plugins/aws/package/compile/events/apiGateway/index.js#L78) line. I can submit a PR that removes the line but I'm not sure of the implications ~~because it was explicitly added by [this commit](https://github.com/serverless/serverless/commit/98ef25bce192d5dcb054043ac363d75e8733a933#diff-a053d5364c6f7aa212359136bffe6880.)~~ .(has probably been there since beginning).

Even after removing the line, it does not immediately work. The issue is that initially there is no stage - so the log settings cannot be applied. If I deploy the API gateway, then the first service and then the API gateway again, it works. 

Do you prefer if I create a new issue for this or is it fine to track it in here?

@tinexw indeed, it shouldn't be like that. It's updateStage logic that should eventually discard changes if it finds no API GW configured and sees no http events.

PR to fix it, is very welcome


Resolving patch:
diff --git a/.travis.yml b/.travis.yml
index f55db13e741..169b15f563e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,7 +4,7 @@ git:
   # Minimize git history, but ensure to not break things:
   # - Merging multiple PR's around same time may introduce a case where it's not
   #   the last merge commit that is to be tested
-  depth: 10
+  depth: 30
 
 cache:
   # Not relying on 'npm' shortcut, as per Travis docs it's the only 'node_modules' that it'll cache
diff --git a/lib/plugins/aws/package/compile/events/apiGateway/index.js b/lib/plugins/aws/package/compile/events/apiGateway/index.js
index b89ca8d961d..ac904286928 100644
--- a/lib/plugins/aws/package/compile/events/apiGateway/index.js
+++ b/lib/plugins/aws/package/compile/events/apiGateway/index.js
@@ -75,10 +75,6 @@ class AwsCompileApigEvents {
         const getServiceState = require('../../../../lib/getServiceState').getServiceState;
 
         const state = getServiceState.call(this);
-        if (!this.serverless.utils.isEventUsed(state.service.functions, 'http')) {
-          return BbPromise.resolve();
-        }
-
         const updateStage = require('./lib/hack/updateStage').updateStage;
 
         this.state = state;


Test patch:
diff --git a/lib/plugins/aws/package/compile/events/apiGateway/index.test.js b/lib/plugins/aws/package/compile/events/apiGateway/index.test.js
index ca404283829..3009f8de19b 100644
--- a/lib/plugins/aws/package/compile/events/apiGateway/index.test.js
+++ b/lib/plugins/aws/package/compile/events/apiGateway/index.test.js
@@ -142,7 +142,7 @@ describe('AwsCompileApigEvents', () => {
         });
       });
 
-      it('should skip the updateStage step when no http events are found', () => {
+      it('should not skip the updateStage step when no http events are found', () => {
         getServiceStateStub.returns({
           service: {
             functions: {
@@ -154,7 +154,7 @@ describe('AwsCompileApigEvents', () => {
         });
 
         return awsCompileApigEvents.hooks['after:deploy:deploy']().then(() => {
-          expect(updateStageStub.calledOnce).to.equal(false);
+          expect(updateStageStub.calledOnce).to.equal(true);
         });
       });
     });