2016-06-07 1 views
1

En Serverless, j'ai la structure de dossier suivantComment charger un dossier lib dans aws lambda?

/component_a/function_1/function_1.js 
/component_a/lib/util.js 

Lorsque je tente de charger util.js de function_1.js en utilisant

u = require('../lib/util.js') 

cela fonctionne de la CLI Serverless fonction "serverless run function_1" . Cependant, dans lambda/api-gateway, il ne peut pas trouver lib/util.js.

Ceci est l'erreur « Erreur: Impossible de trouver le module « ../lib/util » »

Comment puis-je résoudre ce problème?

Répondre

4

Voici comment résoudre ce problème. Dans le component_a/s-function.json remplacer

"handler": "handler.handler", 

avec

"handler": "component_a/handler.handler", 

dans les function_1.js appellent les util.js comme

u = require('../lib/util') 

de la documentation Serverless

The handler property gives you the ability to share code between your functions. By default the handler property is handler.handler, that means it's only relative to the function folder, so only the function folder will be deployed to Lambda.

If however you want to include the parent subfolder of a function, you should change the handler to be like this: functionName/handler.handler As you can see, the path to the handler now includes the function folder, which means that the path is now relative to the parent subfolder, so in that case the parent subfolder will be deployed along with your function. So if you have a lib folder in that parent subfolder that is required by your function, it'll be deployed with your function.

This also gives you the ability to handle npm dependencies however you like. If you have a package.json and node_modules in that parent subfolder, it'll be included in the deployed lambda. So the more parent folders you include in the handler path, the higher you go in the file tree.