Skip to content

Instantly share code, notes, and snippets.

@koladilip
Created June 21, 2018 10:26
Show Gist options
  • Save koladilip/780643318b5fba48abc026d9992d8e36 to your computer and use it in GitHub Desktop.
Save koladilip/780643318b5fba48abc026d9992d8e36 to your computer and use it in GitHub Desktop.
Cloudwatch Logs to S3 exporter AWS Lambda function code
const AWS = require('aws-sdk');
const cloudwatchLogs = new AWS.CloudWatchLogs();
function getDatePath(date) {
date = date || new Date();
const dateString = date.toISOString().substring(0, 10);
return dateString.replace(/-/g, '/');
}
function getLogPathForS3(logGroupName) {
const logPathParts = logGroupName.split('/');
const mainParts = logPathParts[0].split('-');
let logPath = mainParts.slice(1).join('/');
if(logPathParts.length > 1) {
logPath = logPath + '-' + logPathParts.slice(1).join('-');
}
return logPath;
}
function getYesterdayDate() {
const date = new Date();
date.setUTCDate(date.getUTCDate() - 1);
return date;
}
function getDayStartTimestamp(date) {
date = date || new Date();
date.setUTCHours(0);
date.setUTCMinutes(0);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date.getTime();
}
exports.getLogGroupNames = (prefix, nextToken) => {
return cloudwatchLogs.describeLogGroups({
nextToken: nextToken,
logGroupNamePrefix: prefix,
limit: 1
}).promise();
};
function describeExportTask(taskId) {
return cloudwatchLogs.describeExportTasks({taskId}).promise();
}
function waitForExportTaskToComplete(taskId) {
return new Promise((resolve, reject) => {
describeExportTask(taskId).then((response) => {
const task = response.exportTasks[0];
const taskStatus = task.status.code;
if(taskStatus === 'RUNNING' || taskStatus.indexOf('PENDING') === 0) {
console.log("Task is still running for", task.logGroupName);
setTimeout(() => {
waitForExportTaskToComplete(taskId).then(resolve).catch(reject);
}, 1000);
} else {
console.log("Task is completed for", task.logGroupName, "with status", taskStatus);
resolve();
}
}).catch(reject);
});
}
exports.createExportTask = (logGroupName, s3Bucket, logFolderName) => {
const yesterdayDate = getYesterdayDate();
const logPathForS3 = getLogPathForS3(logGroupName);
return cloudwatchLogs.createExportTask({
destination: s3Bucket,
destinationPrefix: `${logFolderName}/${logPathForS3}/${getDatePath(yesterdayDate)}`,
logGroupName,
from: getDayStartTimestamp(yesterdayDate),
to: getDayStartTimestamp()
}).promise().then((response) => {
console.log("Task is started for", logGroupName);
return waitForExportTaskToComplete(response.taskId);
});
};
const cloudwatchLogUtils = require('./cloudwatch_logs');
exports.handler = (event, context, callback) => {
const nextToken = event.nextToken;
const logGroupNamePrefix = event.logGroupNamePrefix;
cloudwatchLogUtils.getLogGroupNames(logGroupNamePrefix, nextToken)
.then((response) => {
event.nextToken = response.nextToken;
event.continue = event.nextToken !== undefined;
const logGroupName = response.logGroups[0].logGroupName;
return cloudwatchLogUtils.createExportTask(logGroupName, event.s3Bucket, event.logFolderName);
}).then(() => {
console.log("Successfully created export task");
return callback(null, event);
}).catch((err) => {
console.error(err);
callback(err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment