简体   繁体   中英

How to import a JSON object from an AWS Lambda Layer?

This may seem simple but I have had a hard time trying to figure it out. I could not seem to find the solution on the web too.

//nodejs/models.js:
// City = ...
// ...
module.exports = {City, Country, Coupon, Player, Pollfish, Tree};

After zipping nodejs, I uploaded the zip file as an AWS Layer and added the layer to my Lambda function.

When I tried to retrieve the objects in my Lambda function:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const {Player} = require('./models.js');

It resulted in an error:

2023-01-06T09:19:49.469Z    undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'models.js'\nRequire stack:\n- /var/task/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'models.js'","Require stack:","- /var/task/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1000:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)","    at async start (file:///var/runtime/index.mjs:1200:23)","    at async file:///var/runtime/index.mjs:1206:1"]}

So what should be the proper way to do it?

I got it: It should be:

const {Player} = require('/opt/nodejs/models.js');

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