Created
April 3, 2017 04:06
-
-
Save prestomation/e4a66f32ff839528cfd5b5ee9bd36235 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Resources: | |
CrashingLambda: | |
Type: AWS::Lambda::Function | |
Properties: | |
Code: | |
ZipFile: > | |
exports.handler = function(event, context) { | |
FunctionDoesntExist(); | |
}; | |
Handler: index.handler | |
Runtime: nodejs4.3 | |
Role: | |
Fn::GetAtt: | |
- "LoggingRole" | |
- "Arn" | |
LoggingLambda: | |
Type: AWS::Lambda::Function | |
Properties: | |
Code: | |
ZipFile: > | |
var zlib = require('zlib'); | |
exports.handler = function(event, context, callback) { | |
var payload = new Buffer(event.awslogs.data, 'base64'); | |
zlib.gunzip(payload, function(err, res) { | |
var parsed = JSON.parse(res.toString('utf8')); | |
var logGroup = parsed.logGroup; | |
var logStream = parsed.logStream; | |
var time = new Date(parsed.logEvents[0].timestamp).toISOString(); | |
var loggingLink = "<https://console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEventViewer:group=LOG_GROUP;stream=LOG_STREAM;start=START|Logging>"; | |
loggingLink = loggingLink.replace("LOG_GROUP", logGroup); | |
loggingLink = loggingLink.replace("LOG_STREAM", logStream); | |
loggingLink = loggingLink.replace("START", time); | |
var msg = "ERROR! In logGroup" + logGroup + ": " + loggingLink; | |
console.log(msg); | |
//sendToSlack(msg); //Pretend you have a method to send to your slack instance | |
callback(null, msg); | |
}); | |
}; | |
Environment: | |
Variables: | |
Test : Test | |
Handler: index.handler | |
Runtime: nodejs4.3 | |
Role: | |
Fn::GetAtt: | |
- "LoggingRole" | |
- "Arn" | |
CrashingLogGroup: | |
Type: "AWS::Logs::LogGroup" | |
Properties: | |
LogGroupName: | |
Fn::Join: | |
- '' | |
- - /aws/lambda/ | |
- !Ref CrashingLambda | |
LoggingSubscription: | |
Type: "AWS::Logs::SubscriptionFilter" | |
Properties: | |
DestinationArn: | |
Fn::GetAtt: | |
- "LoggingLambda" | |
- "Arn" | |
FilterPattern: "Process exited before completing request" | |
LogGroupName: !Ref CrashingLogGroup | |
DependsOn: LambdaInvokePermission | |
LambdaInvokePermission: | |
Type: "AWS::Lambda::Permission" | |
Properties: | |
FunctionName: !Ref LoggingLambda | |
Action: "lambda:InvokeFunction" | |
Principal: "logs.us-west-2.amazonaws.com" | |
SourceAccount: | |
Ref: "AWS::AccountId" | |
LoggingRole: | |
Type: "AWS::IAM::Role" | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: "2012-10-17" | |
Statement: | |
- | |
Effect: "Allow" | |
Principal: | |
Service: | |
- "lambda.amazonaws.com" | |
Action: | |
- "sts:AssumeRole" | |
ManagedPolicyArns: | |
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment