Why is my CloudWatch canary failing with the error "Cannot find module '/opt/nodejs/node_modules/myfunction"?

Last updated: 2020-10-16

Why is my Amazon CloudWatch canary failing with the error "Cannot find module '/opt/nodejs/node_modules/myfunction"?

Resolution

If you don't use the structure required by CloudWatch when creating a .zip file of your function and dependencies, you receive an error. The error message reads: Cannot find module '/opt/nodejs/node_modules/myfunction.

You must create your .zip file under the nodejs/node_modules file path (such as nodejs/node_modules/myCanaryFilename.js). 

1.    Create the required structure.

mkdir -p /nodejs/node_modules/

2.    Copy your CloudWatch canary function to /nodejs/node_modules/.
Note: The handler naming convention is filename.handler.

cp CanaryFunction.js /nodejs/node_modules/
cp -R /lib /nodejs/node_modules/

3.    Zip your directory on the top of /nodejs.

zip -r Canary.zip ./nodejs/*

Note: When creating a Synthetics Canary from scratch, if you have multiple .js files or script dependencies, bundle all files into a single .zip file. Use the following folder structure: "nodejs/node_modules". For example, nodejs/node_modules/myCanaryFilename.js file and other folders and files.


Did this article help?


Do you need billing or technical support?