Created
September 13, 2016 22:51
-
-
Save andymac4182/4837f722231ea493685f6b4699c939a1 to your computer and use it in GitHub Desktop.
Log handler for all functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Type": "AWS::Logs::SubscriptionFilter", | |
"DependsOn": "LoggingLambdaPermission", | |
"Properties": { | |
"LogGroupName": "LogGroup", | |
"FilterPattern": "{ $.HaHaThisIsJson NOT EXISTS }", | |
"DestinationArn": { | |
"Fn::GetAtt": [ | |
"LoghandlerLambdaFunction", | |
"Arn" | |
] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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