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

1 minute read
0

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:

  1. Create the required structure:

    mkdir -p /nodejs/node_modules/
  2. 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.

  3. 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

AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago