Last active
December 16, 2022 13:13
-
-
Save homaily/a432af95e5c3e72d3a584d435544bee0 to your computer and use it in GitHub Desktop.
Lambda download and cache gzipped file from s3
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
'use strict'; | |
console.log('// loading function'); | |
const aws = require('aws-sdk'); | |
const s3 = new aws.S3({apiVersion: '2006-03-01'}); | |
const gzip = require('zlib').createGunzip(); | |
const fs = require('fs'); | |
let isInitialised = false; | |
exports.handler = function(event, context, callback) { | |
if (!isInitialised) | |
return exports.init(event, context, callback); | |
// Do stuff here | |
} | |
exports.init = function(event, context, callback) { | |
const localFileName = '/tmp/chrome'; | |
const localFile = fs.createWriteStream(localFileName); | |
// first check if the file have been downloaded before | |
if (fs.existsSync(localFileName)) { | |
isInitialised = true; | |
return exports.handler(event, context, callback); // call handler function to resume process | |
} | |
s3.getObject({ | |
Bucket: process.env.BUCKET_NAME, | |
Key: process.env.FILE_NAME, | |
}) | |
.createReadStream() | |
.pipe(gzip) // decompress compress file on the fly. No need for this line if file already decompressed | |
.pipe(localFile) // write to local /tmp directory | |
.on('end', function() { | |
isInitialised = true; | |
exports.handler(event, context, callback); // call handler function to resume process | |
}) | |
.on('err', function(err) { | |
callback(err); | |
}) | |
} |
Once the function run for the first time it will execute exports.init
. Afterwards isInitialised
will be cached in memory as true
.
how does puppeteer know your chrome is in /tmp/chrome?
how does puppeteer know your chrome is in /tmp/chrome?
puppeteer.launch({executablePath: '/tmp/chrome'})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to define those environmental variables in your lambda function
In the sample above the file will be downloaded to /tmp/chrome