Reputation: 5491
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
Upvotes: 4
Views: 7014
Reputation: 1651
specify the location on package.json with --manifest:
sam build --template template.yaml --manifest package.json
Upvotes: 3
Reputation: 113
In my case, I just had to remove the CodeUri property from my serverless function. Even though the path looked correct, the sam build command failed witht he above message.
Upvotes: 0
Reputation: 4424
I also faced the same issue.
What fixed it is moving the package.json one level down i.e
In short, you should always stick to the below diagram for building your SAM project correctly
Upvotes: 2
Reputation: 1265
Each lambda is deployed with its own dependency stack. In most of the cases I've encountered, this is a good thing. Two lambdas with exactly the same dependencies indicates duplicated functionality or unused dependencies in one of the functions.
I'm getting the same error as you are, but because I don't have a package.json
in the root folder. The hello-world
sample project generated with sam init
doesn't have one in the root folder either, but somehow sam build
has no problem building that.
Upvotes: 2