How To: Create Subscription Filter in CloudWatch using serverless

Recently, I worked on a task which need to collect all CloudWatch logs to a Kinesis stream. The project is using Serverless for deployment. There are some plugins to create CloudWatch Log subscription filter, but none of them using Kinesis as the destination.

Then by using the serverless-scriptable-plugin, I’m able to do this very easily. The following code find out all CloudWatch LogGroups, and create a SubscriptionFilter for each of them.

Create a file at build/serverless/add-log-subscriptions.js

const resources = serverless.service.provider.compiledCloudFormationTemplate.Resources;
const logSubscriptionDestinationArn = serverless.service.provider.logSubscriptionDestinationArn;

Object.keys(resources)
  .filter(name => resources[name].Type === 'AWS::Logs::LogGroup')
  .forEach(logGroupName => resources[`${logGroupName}Subscription`] = {
      Type: "AWS::Logs::SubscriptionFilter",
      Properties: {
        DestinationArn: logSubscriptionDestinationArn,
        FilterPattern: ".",
        LogGroupName: { "Ref": logGroupName }
      }
    }
  );

Change the serverless.yml file to invoke the script at ‘after:deploy:compileEvents’:

plugins:
  - serverless-scriptable-plugin

custom:
  scriptHooks:
    after:deploy:compileEvents: build/serverless/add-log-subscriptions.js

Leave a Reply

Your email address will not be published. Required fields are marked *