Opensearch role for cloudwatch to opensearch

0

So i want to send cloudwatch data to opensearch, testing from the built in cloudwatch log group filter cloudwatch to opensearch this creates a lambda using nodejs.16, hardcodes the index to cwl and all works lovely.

I want to be able to specify the index name and not always have it go to cwl

I toke the working node function copied and altered it from:

        // index name format: cwl-YYYY.MM.DD
        var indexName = [
            'cwl-' + timestamp.getUTCFullYear(),              // year
            ('0' + (timestamp.getUTCMonth() + 1)).slice(-2),  // month
            ('0' + timestamp.getUTCDate()).slice(-2)          // day
        ].join('.');

to

        // index name format: INDEX_NAME-YYYY.MM.DD
        var indexName = [
            process.env.INDEX_NAME,
            '-' + timestamp.getUTCFullYear(),              // year
            ('0' + (timestamp.getUTCMonth() + 1)).slice(-2),  // month
            ('0' + timestamp.getUTCDate()).slice(-2)          // day
        ].join('.');

which now gives me the error:

2023-10-20T16:09:29.279Z    undefined   ERROR   Uncaught Exception  
{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'snapshot'\nRequire stack:\n- /var/runtime/index.mjs",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'snapshot'",
        "Require stack:",
        "- /var/runtime/index.mjs",
        "    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)",
        "    at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
        "    at async start (file:///var/runtime/index.mjs:1282:23)",
        "    at async file:///var/runtime/index.mjs:1288:1"
    ]
}
  • If i do a diff. that is the only change to the code, the AWS created lambda doesnt use layers or show any other files in the lambda function
  • They both use the same iam role

I cant see that being the problem? Any ideas what ive done wrong?

1 Answer
1

Thank you for reaching out to us. As you mentioned that the you copied the working function code to another lambda function and modified its code before it started giving errors. I would like to note that the error arises due to any missing dependencies which in your case is "snapshot" or incorrect handler settings specified. In your case, I checked there is no such module by name "snapshot.mjs" in nodejs so we can rule this out. Further, I would suggest verifying the handler settings of both the lambda functions. The working lambda function must have the handler file by the name snapshot while the lambda function created by you would have the file name as index.mjs by default. Additionally, check the handler function name inside the code which might also be different. Please refer to this document for more information related to creating a handler in nodejs https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

AWS
SUPPORT ENGINEER
answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions