Hello,
I have a aws lambda function which reads some log files from S3 and does some transformation on these and then creates a new file which I want to gzip and save to an FTP location
Here is the code I have:
'use strict'
const fs = require('fs')
const path = require('path')
const aws = require('aws-sdk')
const s3 = new aws.S3()
const zlib = require('zlib')
const s3stream = require('s3-streams')
const R = require('ramda')
const readline = require('readline')
const namedRegExp = require('named-regexp-groups')
const moment = require('moment')
const s3Upload = require('./s3Upload')
const elbExp = R.always(/([0-9-]+T[0-9:]+\.\S+)\s+\S+\s+(\S+):\d+\s+(\S+):\d+\s+\S+\s+\S+\s+(\S+)\s+(\S+)\s+\S+\s+\S+\s+(\S+)\s+\"(\S+)\s+\w+:\/\/([\w\-\.]*):\S+(\/([^?]+)\S+\s+)([^\"]+)+\"\s+\"([^\"]+)\"\s+\S+\s+\S+/)
const elbRegExp = new namedRegExp(elbExp())
const splitElbEntry = (elbLogEntry) => elbRegExp.exec(elbLogEntry)
const elbToNginx = R.map((val) => val, splitElbEntry)
const dateToNginx = (date) => moment(date).format('DD/MMM/YYYY:HH:mm:ss ZZ')
const nginxTimestamp = (timestamp) => dateToNginx(timestamp)
const elbToNginxLog = (nginxMap) => `${nginxMap[1]}, ${nginxMap[2]} -- [${nginxTimestamp(nginxMap[0])}] "${nginxMap[6]} ${nginxMap[8]} ${nginxMap[10]}" "-" ${nginxMap[4]} ${nginxMap[5]} "${nginxMap[7]}" "${nginxMap[11]}" "-"`
const appendLog = (log) => fs.appendFile('/tmp/abc.log', log, (err) => {
console.log(log)
if (err) throw err
console.log(`The log was updated! ${log}`)
})
module.exports = function s3Listing(...args) {
const parsedList = args[0].map( record => {
const params = {
Bucket: process.env.BUCKET,
Key: record.Key
};
const isGzip = path.extname(params.Key) === ".gz"
let readStream = s3.getObject(params).createReadStream()
readStream = isGzip ? readStream.pipe(zlib.createGunzip()) : readStream
let lineReader = readline.createInterface({
input: readStream
});
lineReader.on('line', (elbLogEntry) => {
// console.log(elbLogEntry)
// if(R.test(/^__utm.gif/, elbLogEntry)){
if(elbLogEntry.includes("__utm.gif")){
const elb = elbToNginx(elbLogEntry)
const nginxMap = elb.groups
appendLog(elbToNginxLog(nginxMap))
}
})
lineReader.on('close', () => {
console.log("end and close stream")
s3Upload('/tmp/abc.log')
});
})
}
I get a Error: ENOENT: no such file or directory, open '/tmp/abc.log'
error
What am I missing?
Any advice is much appreciated