2016-06-07 4 views
1

In Serverless, ich habe die folgende OrdnerstrukturWie lade ich einen lib-Ordner in aws Lambda?

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

Wenn ich versuche, util.js von function_1.js zu laden

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

mit es aus dem serverless CLI "serverless Funktion run function_1" funktioniert . Im lambda/api-Gateway kann lib/util.js jedoch nicht gefunden werden.

Dies ist der Fehler „Fehler: nicht Modul finden‚../lib/util‘“

Wie kann ich es beheben?

Antwort

4

So reparieren Sie. Im component_a/s-function.json

"handler": "handler.handler", 

ersetzen mit

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

in den function_1.js die util.js nennen wie

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

aus der Serverless Dokumentation

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.