[英]index.handler is undefined or not exported when testing Lambda function
我有一个 JS 脚本index.js
,它接受一个变量输入ipAddress
但它给了我以下错误:
{
"errorType": "Runtime.HandlerNotFound",
"errorMessage": "index.handler is undefined or not exported",
"trace": [
"Runtime.HandlerNotFound: index.handler is undefined or not exported",
" at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1137:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)",
" at Module.load (internal/modules/cjs/loader.js:985:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:878:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)",
" at internal/main/run_main_module.js:17:47"
]
}
解压缩后,我的压缩存档看起来像这样:
.
├── index.js
├── node_modules/
└── package.json
该脚本在本地工作:
> node index.js 158.140.127.123
Found ipAddress match
Bucket uploaded successfully at /blocklist-158.140.127.123
但不是在将 zip 导入到 Lambda 时
更新:index.js 内容:
// Usage: index.js x.x.x.x
'use strict';
const AWS = require('aws-sdk');
AWS.config.update({
region: "us-east-1"
});
// Get ipAddress from user argument (will eventually get input from sns)
var ipAddress = process.argv[2]
// Get geolocation from ipAddress
function getIpAddressGeo(ipAddress) {
const geoip = require('geoip-lite');
var geo = geoip.lookup(ipAddress);
return geo;
};
async function getIpMatchBool() {
const request = require('request');
const promise = require('promise');
var lookupUrl = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/bi_sip_0_1d.ipset";
// This should only create a bucket if getIpMatchBool returns true
function createS3IpListing(ipAddress) {
var date = Date.now();
const s3 = new AWS.S3({
apiVersion: '2006-03-01',
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
const bucketParams = {
Bucket: `blocklist-${ipAddress}`
};
s3.createBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log(`Bucket uploaded successfully at ${data.Location}`);
}
});
// createFile not returning - working on this now
const createFile = (geo = getIpAddressGeo()) => {
const fs = require('fs');
fs.readFile(date, (err, data) => {
if (err) throw err;
const params = {
Bucket: bucketParams.Bucket,
Key: date,
Body: JSON.stringify(data, geo)
};
s3.upload(params, function(s3Err, data) {
if (s3Err) throw s3Err
console.log(`File uploaded successfully at ${data.Location}`)
});
});
return createFile(geo)
};
};
// This RegEx does not account for the differance between 10 & 10.x.x.x - need to fix
var re = new RegExp(ipAddress.replace(/\./g, '\\.'), 'gi');
const issueRequest = () => {
return new promise(() => {
request(lookupUrl, function(error, response, body) {
if (!error && response.statusCode == 200) {
if (body.search(re) == -1) {
console.log("No ipAddress match");
} else {
createS3IpListing(ipAddress);
console.log("Found ipAddress match");
}
}
})
});
}
return await issueRequest();
}
getIpMatchBool(ipAddress)
我不认为你可以通过 process.arg 访问你的变量。 尝试将 exports.handler 与事件参数一起使用。 处理程序是 lambda function 默认返回/执行的内容。 您可以在此处找到文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.