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개 답변
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
지원 엔지니어
답변함 7달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인