I'm using AWS SAM in Visual Studio Code to set up a number of Node.js lambda functions. I am using Windows 10. I'm only starting out with AWS SAM and I've generated the Sample Hello World App using the Command Palette and it works. I started expanding the app by adding a second function and restructured the directory structure to start adding more functions. My directory structure now looks as follows:
|--my-app
| |--event-handlers
| | |--hello-world
| | | |--app.js
| | |--hello-world-2
| | | |--app.js
| |--package.json
| |--template.yml
When I run sam build I get the error Could not install from ..\my-app\event-handlers\hello-world as it does not contain a package.json file. If I copy my package.json file into each of the subdirectories hello-world and hello-world-2 then sam build works fine. But it doesn't seem right that I need to specify a separate package.json file for each function because it breaks DRY principles. I was expecting that it should use the package.json from the root directory for all my lambda functions if I structure it like this.
Do I really need to duplicate the package.json file for each lambda function? What are best practices in terms of a directory structure for a SAM application that defines multiple lambda functions? Why is it not picking up my package.json file from my root directory?
For reference I am including a section of my template.yaml file too
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: event-handlers/hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
HelloWorldFunction2:
Type: AWS::Serverless::Function
Properties:
CodeUri: event-handlers/hello-world-2/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
MyScheduledEvent:
Type: Schedule
Properties:
Schedule: rate(2 minutes)
Name: app.lambdaHandler
Description: Interval at which node function should be called
Enabled: True

