Skip to content

Instantly share code, notes, and snippets.

@digitalkaoz
Created December 20, 2017 21:32
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save digitalkaoz/94933c246ba67032a1507083e2605a30 to your computer and use it in GitHub Desktop.
Save digitalkaoz/94933c246ba67032a1507083e2605a30 to your computer and use it in GitHub Desktop.
gatsby in aws lambda
const AWS = require("aws-sdk");
const {link} = require("linkfs");
const mock = require('mock-require');
const fs = require('fs');
const tmpDir = require('os').tmpdir();
exports.handler = (event, context) => {
rewriteFs();
invokeGatsby(context);
}
const invokeGatsby = (context) => {
const gatsby = require("gatsby/dist/commands/build");
gatsby({
directory: __dirname,
})
.then(uploadFilesToS3)
.then(invalidateCloudFront)
.then(context.succeed)
.catch(context.fail);
};
//TODO replace with in-memory-fs ?!
const rewriteFs = () => {
// redirect paths
const linkedFs = link(fs, [
[`${__dirname}/.cache`, tmpDir+"/.cache"],
[`${__dirname}/public`, tmpDir+"/public"]
]);
// those are missing in linkfs
linkedFs.ReadStream = fs.ReadStream;
linkedFs.WriteStream = fs.WriteStream;
// replace fs with linkfs globally
mock('fs', linkedFs);
}
@housser
Copy link

housser commented Sep 26, 2018

Do you have this idea as part of a functional project, or is it just a concept?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment