I want to know why my Amazon CloudWatch canary fails with the error "Cannot find module '/opt/nodejs/node_modules/myfunction".
Resolution
When you create a .zip file of your function and dependencies and you don't use the structure that CloudWatch requires, you receive the following error:
"Cannot find module '/opt/nodejs/node_modules/myfunction".
You must create your .zip file under the nodejs/node_modules file path.
Complete the following steps:
-
Create the required structure:
mkdir -p /nodejs/node_modules/
-
Copy your CloudWatch canary function to /nodejs/node_modules/:
cp CanaryFunction.js /nodejs/node_modules/
cp -R /lib /nodejs/node_modules/
Note: The handler naming convention is filename.handler.
-
Zip your directory on the top of /nodejs:
zip -r Canary.zip ./nodejs/*
Note: When you write a CloudWatch Synthetics canary script that has multiple .js files or script dependencies, bundle all files into a single .zip file. Use the nodejs/node_modules folder structure. For example, you can use nodejs/node_modules/myCanaryFilename.js.
Related information
Packaging your Node.js canary files