Last active
January 8, 2021 14:22
-
-
Save gioragutt/1f581e60d73c9da57816b56123fe8ba5 to your computer and use it in GitHub Desktop.
Script for setting up layer for a lambda containing node.js dependencies
This file contains hidden or 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
... |
This file contains hidden or 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
{ | |
"name": "my-lambda", | |
"private": true, | |
"version": "1.0.0", | |
"scripts": { | |
"prepare": "node prepare_layer.js", | |
"package": "yarn prepare && yarn sls package --stage production" | |
}, | |
"dependencies": { | |
"//": "...", | |
"@google-cloud/language": "^4.2.1" | |
} | |
} |
This file contains hidden or 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
const { mkdirSync, writeFileSync, unlinkSync, renameSync } = require('fs'); | |
const { execSync } = require('child_process'); | |
const { join } = require('path'); | |
const { externals = [] } = require('./webpack.config'); | |
const { dependencies = {} } = require('./package.json'); | |
const layerDir = join(__dirname, '.google-cloud-deps'); | |
const outputDir = join(layerDir, 'nodejs'); | |
// Prepare Output Directories | |
console.log('Preparing output directories'); | |
execSync(`rm -rf ${layerDir}`); | |
mkdirSync(outputDir, { recursive: true }); | |
// Write package.json for layer dependencies | |
console.log('Writing package.json with external dependencies'); | |
const dependenciesToPackage = Object.fromEntries( | |
Object.entries(dependencies).filter(([name]) => externals.includes(name)) | |
); | |
const outputPackageJson = join(layerDir, 'package.json'); | |
const outputPackageJsonContent = JSON.stringify({ dependencies: dependenciesToPackage }, null, 2); | |
console.log(outputPackageJsonContent); | |
writeFileSync(outputPackageJson, outputPackageJsonContent); | |
// Install dependencies and organize layer files | |
console.log('Installing dependencies and organizing layer structure'); | |
console.log(execSync('yarn --no-lockfile', { cwd: layerDir }).toString()); | |
renameSync(join(layerDir, 'node_modules'), join(outputDir, 'node_modules')); | |
// Cleanup files | |
console.log('Cleaning up output folder'); | |
unlinkSync(outputPackageJson); | |
// Voila! | |
console.log('Layer files ready!'); |
This file contains hidden or 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
# ... | |
layers: | |
GoogleCloudDeps: | |
path: .google-cloud-deps | |
compatibleRuntimes: | |
- nodejs12.x | |
functions: | |
my-lambda: | |
# ... | |
layers: | |
- Ref: GoogleCloudDepsLambdaLayer | |
# ... |
This file contains hidden or 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
const baseWebpack = require('../../webpack.config'); | |
module.exports = { | |
...baseWebpack, | |
externals: [...baseWebpack.externals, '@google-cloud/language'], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment