Created
December 14, 2023 07:24
-
-
Save ubinix-warun/4eafb6e26d3ed1dc5a42bfec47e48e7f to your computer and use it in GitHub Desktop.
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
import * as cdk from 'aws-cdk-lib'; | |
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; | |
import * as lambda from 'aws-cdk-lib/aws-lambda'; | |
import * as logs from 'aws-cdk-lib/aws-logs'; | |
import * as destinations from 'aws-cdk-lib/aws-logs-destinations'; | |
import { Construct } from 'constructs'; | |
export class CloudwatchLineNotifyStack extends cdk.Stack { | |
public readonly watchFn: lambda.Function; | |
constructor(scope: Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
const watchFn = new NodejsFunction(this, 'watchLambda', { | |
entry: 'lambda/index.ts', | |
handler: 'handler', | |
runtime: lambda.Runtime.NODEJS_18_X, | |
}); | |
//link an AWS CloudWatch log group for receiving logs | |
const logGroup = logs.LogGroup.fromLogGroupArn(this, '<>', | |
'<>'); | |
// fields @timestamp, @message, @logStream, @log | |
// | filter ___ | |
// | sort @timestamp desc | |
// | limit 20 | |
// | filter (message == "Successfully produced a new block") | |
// | filter (level == "Error") | |
new logs.SubscriptionFilter(this, 'WatchSubscription', { | |
logGroup, | |
destination: new destinations.LambdaDestination(watchFn), | |
filterPattern: logs.FilterPattern.any( | |
logs.FilterPattern.stringValue("$.message","=","Successfully produced a new block") | |
// logs.FilterPattern.stringValue("$.level","=","Error") | |
), | |
filterName: 'ProducedNewBlock', | |
}); | |
} | |
} |
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
#!/usr/bin/env node | |
import 'source-map-support/register'; | |
import * as cdk from 'aws-cdk-lib'; | |
import { CloudwatchLineNotifyStack } from '../lib/cloudwatch-line-notify-stack'; | |
const app = new cdk.App(); | |
new CloudwatchLineNotifyStack(app, 'CloudwatchLineNotifyStack', { | |
env: { | |
account: '<AWS_ACCOUNT>', | |
region: '<AWS_REGION>' | |
}, | |
}); |
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
import { Callback, | |
CloudWatchLogsDecodedData, | |
CloudWatchLogsEvent, | |
CloudWatchLogsHandler, | |
Context, Handler } from 'aws-lambda'; | |
import { Notify } from 'line-api' | |
import * as zlib from "zlib"; | |
const notify = new Notify({ | |
token: "<LINE_NOTIFY_TOKEN>" | |
}) | |
export const handler: CloudWatchLogsHandler = ( | |
event: CloudWatchLogsEvent, | |
_context: Context, | |
callback: Callback | |
): void => { | |
console.log('EVENT: \n' + JSON.stringify(event, null, 2)); | |
const decoded = Buffer.from(event.awslogs.data, "base64"); | |
zlib.gunzip(decoded, (e, result) => { | |
if (e) { | |
callback(e, null); | |
} else { | |
const json: CloudWatchLogsDecodedData = JSON.parse( | |
result.toString("ascii") | |
); | |
json.logEvents.forEach(async (event) => { | |
const jsonlog = JSON.parse(event.message); | |
console.log("DETAIL: (json log)", JSON.stringify(jsonlog, null, 2)); | |
// "metadata": { | |
// "breadcrumb": { | |
// "validated_transition": { | |
// "data": { | |
// "protocol_state": { | |
// "body": { | |
// "consensus_state": { | |
const consensus_state = jsonlog.metadata.breadcrumb.validated_transition.data.protocol_state.body.consensus_state | |
const msg = `${jsonlog.message} - Block height: ${consensus_state.blockchain_length} Epoch: ${consensus_state.epoch_count}`; | |
try { | |
const resp = await notify.send({ | |
message: msg, | |
}); | |
console.log(resp); | |
} catch (error) { | |
console.log(error); | |
} | |
}); | |
callback(null); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment