简体   繁体   中英

AWS Lambda - Not able to a access JS file bundled in Layer (Node.js)

I have created a lambda function on node.js and attached a layer with it. But when I execute my lambda function, it is not reading JS bundled in layer. Please let me know if I am making any mistake here.

Followed this post and created a layer 'my-utility' and uploaded nodejs.zip.

AWS Console > Lambda > Layers > Create Layer

Layer Structure

my-utility
   - nodejs
     - node_modules   
     - myutil.js
     - package.json
     - package-lock.json
   - nodejs.zip 

myutil.js

function myFun(name) {
     console.log("Hello.. " + name);
}

Lambda Code (Node.js 10.x)

const myutil = require('/opt/nodejs/myutil.js');
exports.handler = async (event) => {

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };

    console.log('Layer Code :', myutil);

    return response;
};

After testing above lambda, it gives the below result:

Response:
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}

Request ID:
"5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0"

Function Logs:
START RequestId: 5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0 Version: $LATEST
2019-06-18T18:35:35.125Z    5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0    INFO    Layer Code :  {}
END RequestId: 5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0
REPORT RequestId: 5dbfd4ab-04d2-47eb-89dd-c9c6a650cbb0  Duration: 133.56 ms Billed Duration: 200 ms     Memory Size: 128 MB Max Memory Used: 26 MB

If you notice, when I am trying to print 'myutil' constant, it is printed as empty. That means, layer code is not injected during lambda execution.

INFO Layer Code : {}

If you are trying to access static files inside a Lambda layer using serverless, make sure they are getting packaged by downloading the .zip of the layer from the AWS Layers section, and if they are there you can output the contents of the /opt folder to your CloudWatch log to make sure your files are there.

console.log('/opt/');
fs.readdirSync('/opt/').forEach(file => {
  console.log(file);
});

I reached to AWS support team and get this issue resolved. I have to code function like below in myutil.js. This link also helped.

module.exports = {
  myFun:function (name) {
     console.log("Hello.. " + name);
     return 'narendra';
  }
};

I could see lambda called js function from layer and printed return value properly.

Lambda Function Logs

START RequestId: 39bfa864-9a31-4c0c-b9d3-ce7c2b3d1aaf Version: $LATEST
2019-06-18T21:28:06.505Z    39bfa864-9a31-4c0c-b9d3-ce7c2b3d1aaf    INFO    Hello.. narendra
2019-06-18T21:28:06.505Z    39bfa864-9a31-4c0c-b9d3-ce7c2b3d1aaf    INFO    Layer Code :  narendra
END RequestId: 39bfa864-9a31-4c0c-b9d3-ce7c2b3d1aaf
REPORT RequestId: 39bfa864-9a31-4c0c-b9d3-ce7c2b3d1aaf  Duration: 85.38 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 26 MB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM