Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andymac4182/4837f722231ea493685f6b4699c939a1 to your computer and use it in GitHub Desktop.
Save andymac4182/4837f722231ea493685f6b4699c939a1 to your computer and use it in GitHub Desktop.
Log handler for all functions
{
"Type": "AWS::Logs::SubscriptionFilter",
"DependsOn": "LoggingLambdaPermission",
"Properties": {
"LogGroupName": "LogGroup",
"FilterPattern": "{ $.HaHaThisIsJson NOT EXISTS }",
"DestinationArn": {
"Fn::GetAtt": [
"LoghandlerLambdaFunction",
"Arn"
]
}
}
}
'use strict';
const _ = require('lodash');
const path = require('path');
class MyPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'after:deploy:compileFunctions': this.logServerless.bind(this)
}
}
logServerless() {
this.serverless.service.getAllFunctions().forEach((functionName) => {
if (functionName === 'loghandler') {
return
}
const normalizedFunctionName = functionName[0].toUpperCase() + functionName.substr(1);
const functionLogicalId = `${normalizedFunctionName}LambdaFunction`;
const functionObject = this.serverless.service.getFunction(functionName);
const cloudwatchLogsSubscriptionFilterTemplate = this.serverless.utils.readFileSync(
path.join(
this.serverless.config.servicePath,
'plugins',
'cloudwatch-logs-subscription-filter-template.json'
)
)
cloudwatchLogsSubscriptionFilterTemplate.Properties.LogGroupName = `/aws/lambda/${functionObject.name}`
const logGroup =
`
{
"Type" : "AWS::Logs::LogGroup",
"Properties" : {
"LogGroupName" : "${cloudwatchLogsSubscriptionFilterTemplate.Properties.LogGroupName}"
}
}
`
const newResources = {
[`${functionLogicalId}SubscriptionFilter`]: cloudwatchLogsSubscriptionFilterTemplate,
[`${functionLogicalId}LogGroup`]: JSON.parse(logGroup),
}
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newResources)
})
}
}
module.exports = MyPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment