Unable to use lambda layer in lambda function (using aws cdk )

0

I am using aws cdk with typescript to develop, and I want to use axios in my lambda function

I encoutered error

{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'opt/nodejs/axios'\nRequire stack:\n- /var/task/dbcall.js\n- /var/runtime/index.mjs", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module 'opt/nodejs/axios'", "Require stack:", "- /var/task/dbcall.js", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:1061:17)", " at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)", " at async start (file:///var/runtime/index.mjs:1256:23)", " at async file:///var/runtime/index.mjs:1262:1" ] }

This is my stack code

export class CddStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);


    const axiosLayer = new LayerVersion(this, 'axiosLayer', {
      compatibleRuntimes: [
        Runtime.NODEJS_16_X,
      ],
      code: Code.fromAsset('./resources/layers/axios'),
      description: "axios",

    })

    //define an aws lambda resource 
    const dbcall = new lambda.Function(this, 'dbCall', {
      runtime: lambda.Runtime.NODEJS_16_X,
      code: lambda.Code.fromAsset('lambda'),
      handler: 'dbcall.handler',
      timeout: Duration.seconds(300),

      layers: [axiosLayer],
    })
    const rule = new events.Rule(this, 'Schedule Rule', {
      schedule: events.Schedule.cron({ minute: '10', hour: '0' })
    })
    rule.addTarget(new targets.LambdaFunction(dbcall))
  }
}

This is my folder structure Enter image description here

my lambda function code

const { axios } = require('opt/nodejs/axios');

const { hash, getKey } = require('./hash')
exports.handler = async function (event) {
....
}

The code inside axios.ts

// @ts-nocheck
export { axios } from 'axios'

my tsconfig.json set up

"paths": { "opt/nodejs/axios": ["resources/layers/axios/nodejs/axios.ts"] },

RECJ
已提問 7 個月前檢視次數 535 次
1 個回答
1
已接受的答案

The error you're encountering suggests that AWS Lambda is unable to find the axios module, and it's looking for it in a directory that does not exist, such as 'opt/nodejs/axios'. This issue might be related to how you've bundled and deployed your Lambda function.

To resolve this issue, you can follow these steps:

  1. Check Dependencies: Ensure that axios is properly added to your Lambda function's deployment package. In your AWS CDK project, make sure that axios is listed as a dependency in your package.json file.

  2. Bundling: When you deploy a Lambda function with dependencies, it's essential to bundle the function with its dependencies. You can use tools like AWS SAM, Serverless Framework, or AWS CDK to help with bundling. Make sure your bundling process includes the node_modules directory.

  3. Build Lambda Function: Before deploying, ensure that you build your Lambda function with the bundled dependencies and package it correctly.

Shubham
已回答 7 個月前
profile pictureAWS
專家
已審閱 7 個月前
  • I found out if I remove the node_modules folder from git_ignore file, everything will work..., but it is a big folder, Can I really ignore it ?

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南