This is similar to https://github.com/aws-amplify/amplify-cli/issues/4479
But perhaps this is expected since we're supposed to put compiled code in the lambda layer dir?
But perhaps this is expected since we're supposed to put compiled code in the lambda layer dir?
Correct, this is the intended behavior. I'll mark this as an enhancement.
So for now are supposed to check in node_modules that is in the layer, this is usually in people's .gitignore, but I guess for this case we need to actually commit the node_modules to the repo?
If your layer is sufficiently small it's probably easiest to add it to the repo. If not, you have to run npm install in the layer's /lib/nodejs directory each time after the repo is cloned.
adding a few lines in amplify.yml in the prebuild section to cd into the layer's nodejs folder and then running npm install should work, right? So that you don't have to commit node_modules to the repo.
adding a few lines in amplify.yml in the prebuild section to cd into the layer's nodejs folder and then running npm install should work, right? So that you don't have to commit node_modules to the repo.
@djsmedes did you try this - did things work for you?
I think I did but I don't remember unfortunately. We ended up needing to pull our layer out of amplify and manage it manually because one of the packages in it is incompatible with the outdated node version that the AWS Linux 2 build image has installed. Since we are only using one layer at the moment, managing it manually was easier than trying to mess with the build images.
A few weeks later - we switched back to using the layer managed as part of amplify, and I can indeed confirm that running an install as part of the prebuild will work.
We have a shell script that is called by amplify.yml and it includes these lines:
cd amplify/backend/function/nameOfLayer/lib/nodejs
yarn install --ignore-engines # amplify's build image uses node 10.16.0 at time of writing, which is super old and incompatible with one of our packages. We're going to be running this code in a lambda with node 12.x runtime, so we ignore the error
cd $DIR # this variable stores the root directory
This results in us not needing to check in the node_modules folder, but it still is built before amplify zips up the contents of that directory in preparation for upload, so everything works.
Now we can't get it to detect changes and create a new layer version, but that's probably best discussed on a separate issue.
I was able to resolve this by updating amplify.yml to build lambda layer before calling amplifyPush
Example of build script.
build:
commands:
- '# Execute module build for lambda layers'
- cd ./amplify/backend/function/viewerlayer/lib/nodejs
- yarn install
- echo "# Back to root folder"
- cd ../../../../../..
- '# Execute Amplify CLI with the helper script'
- amplifyPush --simple
Most helpful comment
A few weeks later - we switched back to using the layer managed as part of amplify, and I can indeed confirm that running an install as part of the prebuild will work.
We have a shell script that is called by
amplify.ymland it includes these lines:This results in us not needing to check in the
node_modulesfolder, but it still is built before amplify zips up the contents of that directory in preparation for upload, so everything works.Now we can't get it to detect changes and create a new layer version, but that's probably best discussed on a separate issue.