Starting AWS SAM folder structure (NodeJS)
When I started putting together my first AWS SAM project, I was confused with the project structure - as always, when I’m starting new project with new technology. You can easily make a bloated project where code is duplicated in each lambda function.
I had a few goals to achieve:
- simple and general structure which is a good starting point for smaller applications
- each lambda function has its own dependencies
- common code is shared in layers
- no external dependencies in the repository - every dependency is defined in package.json
In the project folder, I created folder Lambdas. Each lambda is in its own folder with package.json. The package.json contains dependencies only required by this function.
Folder Layers will contain folders with layers. Because the structure is just a starting point, I will have only two layers. One called SharedInternalDependencies with my own library code which I want to share and one called SharedExternalDependencies with third-party libraries used in lambdas and in internal shared dependencies.
SharedExternalDependencies just contain package.json. Dependencies will be downloaded by SAM during the build process. Yes, SAM can build not only lambdas but also layers. But you should use the newest version of SAM, because this feature wasn’t always there.
SharedInternalDependencies is not built by SAM, so we have to create a valid folder structure for Node. The correct structure is nodejs/node_modules/my_module. Or nodejs/node_modules/my_module.js. If you use a different language, the path has to be different. You can find the correct paths in the AWS documentation.
The final structure will look like this:
- MySAMProject
- template.yml
- Lambdas
- Lambda-1
- lambda-1.js
- package.json
- Lamda-2
- lambda-2.js
- package.json
- Lambda-1
- Layers
- SharedInternalDependencies
- nodejs
- node_modules
- my-library-module.js
- node_modules
- nodejs
- SharedExternalDependencies
- package.json
- SharedInternalDependencies
|
Notice the definition of external layer has Metadata. The selected BuildMethod will build the layer. If you open .aws-sam, you can notice that the correct path nodejs/node_modules with downloaded modules was created.
Author: Luděk Novotný