-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridgify.ts
42 lines (40 loc) · 1.38 KB
/
bridgify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const zlib = require('zlib');
const util = require('util');
const gunzip = util.promisify(zlib.gunzip);
import { Handler, CloudWatchLogsEvent } from 'aws-lambda';
import {
EventBridgeClient,
PutEventsCommand,
} from '@aws-sdk/client-eventbridge';
const eventBridgeClient = new EventBridgeClient({
region: process.env.AWS_REGION,
});
export const handler: Handler = async (event: CloudWatchLogsEvent, context) => {
const payload = Buffer.from(event.awslogs.data, 'base64');
const unzippedEvent = await gunzip(payload);
const parsedEvent = JSON.parse(unzippedEvent.toString('utf8'));
if (parsedEvent.messageType === 'DATA_MESSAGE') {
for (const logEvent of parsedEvent.logEvents) {
const message = new PutEventsCommand({
Entries: JSON.parse(
logEvent.message.substring(logEvent.message.indexOf('{') - 1)
).Entries,
});
let result = await eventBridgeClient.send(message);
for (const entry of result.Entries!) {
if (entry.EventId) {
console.log(
`Successfully sent event to EventBridge: ${entry.EventId}`
);
} else {
console.log(
`Failed to send event to EventBridge: ${entry.ErrorCode}: ${entry.ErrorMessage}`
);
}
}
if (result.FailedEntryCount! > 0) {
throw new Error('Not all events were sent to EventBridge');
}
}
}
};