Repository: serverless/serverless (JavaScript)
Date: 2019-09-10 01:13:11+00:00
Task type: Bug Fix

Issue:
AWS API GW WebSocket logs does not respect logRetentionInDays
# This is a Bug Report

## Description

- What went wrong?
Log retentions days not set for CloudWatch log group for websocket.

- What did you expect should have happened?
Log retension days should be set, as same as for lambda and rest APIs.

- What was the config you used?
```yaml
service: logs

provider:
  name: aws
  runtime: nodejs10.x
  region: us-east-1
  logRetentionInDays: "3"
  logs:
    restApi: true
    websocket: true

functions:
  hello:
    handler: handler.hello
    events:
      - http: GET hello
  helloWebSocket:
    handler: handler.hello
    events:
      - websocket: "$connect"
```
- What stacktrace or error message from your provider did you see?
No stack trace.

Similar or dependent issues:

- None

## Additional Data

- **_Serverless Framework Version you're using_**: 1.51.0
- **_Operating System_**: Ubuntu 18.04
- **_Stack Trace_**: None
- **_Provider Error messages_**: None



Issue discussion:

Solution patch:
diff --git a/lib/plugins/aws/package/compile/events/websockets/lib/stage.js b/lib/plugins/aws/package/compile/events/websockets/lib/stage.js
index 0c21ca85cb6..c959d6b3099 100644
--- a/lib/plugins/aws/package/compile/events/websockets/lib/stage.js
+++ b/lib/plugins/aws/package/compile/events/websockets/lib/stage.js
@@ -60,7 +60,7 @@ module.exports = {
       });
 
       Object.assign(cfTemplate.Resources, {
-        [logGroupLogicalId]: getLogGroupResource(service, stage),
+        [logGroupLogicalId]: getLogGroupResource(service, stage, this.provider),
       });
 
       return ensureApiGatewayCloudWatchRole(this.provider);
@@ -68,11 +68,16 @@ module.exports = {
   },
 };
 
-function getLogGroupResource(service, stage) {
-  return {
+function getLogGroupResource(service, stage, provider) {
+  const resource = {
     Type: 'AWS::Logs::LogGroup',
     Properties: {
       LogGroupName: `/aws/websocket/${service}-${stage}`,
     },
   };
+  const logRetentionInDays = provider.getLogRetentionInDays();
+  if (logRetentionInDays) {
+    resource.Properties.RetentionInDays = logRetentionInDays;
+  }
+  return resource;
 }


Test patch:
diff --git a/lib/plugins/aws/package/compile/events/websockets/lib/stage.test.js b/lib/plugins/aws/package/compile/events/websockets/lib/stage.test.js
index e4f9e1c1196..9afc94c923e 100644
--- a/lib/plugins/aws/package/compile/events/websockets/lib/stage.test.js
+++ b/lib/plugins/aws/package/compile/events/websockets/lib/stage.test.js
@@ -134,6 +134,24 @@ describe('#compileStage()', () => {
         });
       }));
 
+    it('should set a RetentionInDays in a Log Group if provider has logRetentionInDays', () => {
+      awsCompileWebsocketsEvents.serverless.service.provider.logRetentionInDays = 42;
+
+      return awsCompileWebsocketsEvents.compileStage().then(() => {
+        const resources =
+          awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
+            .Resources;
+
+        expect(resources[logGroupLogicalId]).to.deep.equal({
+          Type: 'AWS::Logs::LogGroup',
+          Properties: {
+            LogGroupName: '/aws/websocket/my-service-dev',
+            RetentionInDays: 42,
+          },
+        });
+      });
+    });
+
     it('should ensure ClousWatch role custom resource', () => {
       return awsCompileWebsocketsEvents.compileStage().then(() => {
         const resources =